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

Automatically connect to replicated peers #339

Merged
merged 1 commit into from
Mar 28, 2020
Merged
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
51 changes: 49 additions & 2 deletions src/ssb.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ const flotilla = require("@fraction/flotilla");
const ssbTangle = require("ssb-tangle");
const debug = require("debug")("oasis");
const path = require("path");
const pull = require("pull-stream");
const lodash = require("lodash");

const socketPath = path.join(ssbConfig.path, "socket");
const publicInteger = ssbConfig.keys.public.replace(".ed25519", "");
Expand All @@ -21,8 +23,6 @@ ssbConfig.connections.incoming.unix = [
{ scope: "device", transform: "noauth" }
];

const server = flotilla(ssbConfig);

const log = (...args) => {
const isDebugEnabled = debug.enabled;
debug.enabled = true;
Expand Down Expand Up @@ -66,11 +66,58 @@ const createConnection = config => {
}
log("Initial connection attempt failed");
log("Starting Scuttlebutt server");

const server = flotilla(ssbConfig);
server(config);

const inProgress = {};
const maxHops = lodash.get(
config,
"friends.hops",
lodash.get(ssbConfig, "friends.hops", 0)
);

const add = address => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

It looks to me like ssb-conn's (ssb-conn-hub's) connect function already handles this (connect() returns false if there's already a connection open or in progress), so inProgress and add probably aren't necessary.

Copy link
Member Author

Choose a reason for hiding this comment

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

Nice catch! I noticed that the same person was showing up multiple times in my peer list, but maybe one was just { state: 'connecting' }.

inProgress[address] = true;
return () => {
inProgress[address] = false;
};
};

const connectOrRetry = () => {
rawConnect()
.then(ssb => {
log("Retrying connection to own server");
ssb.friends.hops().then(hops => {
pull(
ssb.conn.stagedPeers(),
pull.drain(x => {
x.filter(([address, data]) => {
const notInProgress = inProgress[address] !== true;

const key = data.key;
const haveHops = typeof hops[key] === "number";
const hopValue = haveHops ? hops[key] : Infinity;
// Negative hops means blocked
const isNotBlocked = hopValue >= 0;
const withinHops = isNotBlocked && hopValue <= maxHops;

return notInProgress && withinHops;
}).forEach(([address, data]) => {
const done = add(address);
debug(
`Connecting to staged peer at ${
hops[data.key]
}/${maxHops} hops: ${address}`
);
ssb.conn
.connect(address, data)
.then(done)
.catch(done);
});
})
);
});
resolve(ssb);
})
.catch(e => {
Expand Down