Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Staltz feedback #16

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 25 additions & 47 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@
"dotenv": "^16.0.3",
"express": "^4.18.1",
"graphql": "^15.0.0",
"pull-flatmap": "^0.0.1",
"pull-paramap": "^1.2.2",
"pull-stream": "^3.6.14",
"run-waterfall": "^1.1.7",
"secret-stack": "^6.4.1",
"ssb-about-self": "^1.1.0",
"ssb-blobs": "^2.0.1",
"ssb-caps": "^1.1.0",
Expand Down
48 changes: 36 additions & 12 deletions src/ssb-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const ssbKeys = require('ssb-keys')
const { join } = require('path')
const waterfall = require('run-waterfall')
const pull = require('pull-stream')
const flatMap = require('pull-flatmap')

const peers = require('./peers')
const DB_PATH = join(__dirname, '../db')
Expand All @@ -28,8 +29,19 @@ module.exports = function SSB (opts = {}) {

.use(require('ssb-friends'))
.use(require('ssb-lan'))
.use(require('ssb-conn'))
.use(require('ssb-ebt'))
// .use(require('ssb-conn'))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: we see that turning off the default scheduler works amazingly for initial sync,
but not sure what the implication is long term. I think we need to make our own scheduler to make sure we have some connections live, but not too many, and cycle through them

.use(require('ssb-conn/core'))
.use(require('ssb-conn/compat')) // for ssb.gossip
// use a minimal ssb-conn, exlcuding the scheduler,
// which would otherwise prune connections down to 3

// Staltz recommends writing own scheduler, see README for template
// simple idea could be:
// 1. bootstrap with hosts above
// 2. live query to discover 'pub' messages with hosts
// 3. cycle through a series of different connections to discover more messages
// outstanding question: getStatus progress stuck?
.use(require('ssb-replication-scheduler'))
.use(require('ssb-blobs'))
.use(require('ssb-serve-blobs'))
Expand All @@ -45,16 +57,24 @@ module.exports = function SSB (opts = {}) {
ssb.lan.start()
pull(
ssb.conn.peers(),
pull.map(update => update.map(ev => [ev[1].key, ev[1].state])),
pull.log()
pull.through(() => console.log('>')),
flatMap(evs => evs),
pull.asyncMap((ev, cb) => {
const feedId = ev[1].key
ssb.aboutSelf.get(feedId, (err, details) => {
cb(null, {
state: ev[1].state,
name: err ? feedId : (details.name || feedId)
})
})
}),
pull.drain(({ state, name }) => {
const output = `${state.padStart(13, ' ')} - ${name}`
state === 'connected'
? console.log(green(output))
: console.log(output)
})
)
ssb.db.onMsgAdded(m => {
if (m.kvt.value.author !== ssb.id) return
console.log(
m.kvt.value.sequence,
JSON.stringify(m.kvt.value.content, null, 2)
)
})

peers.forEach(({ name, id, host, invite }) => {
if (id) {
Expand All @@ -67,8 +87,8 @@ module.exports = function SSB (opts = {}) {
},
(data, cb) => {
if (invite) ssb.invite.use(invite, cb)
else cb(null, null)
},
else cb(null, null)
},
(data, cb) => {
if (host) ssb.conn.connect(host, cb)
else cb(null)
Expand Down Expand Up @@ -106,3 +126,7 @@ module.exports = function SSB (opts = {}) {

return ssb
}

function green (string) {
return '\x1b[32m' + string + '\x1b[0m'
}
Loading