Merge pull request #403 from nickwynja/sync

Introduces sync endpoint
This commit is contained in:
Christian Bundy 2020-04-14 10:39:01 -07:00 committed by GitHub
commit 1b6c559115
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 86 additions and 1 deletions

View File

@ -514,7 +514,7 @@ router
const theme = ctx.cookies.get("theme") || config.theme;
const getMeta = async ({ theme }) => {
const status = await meta.status();
const peers = await meta.peers();
const peers = await meta.connectedPeers();
const peersWithNames = await Promise.all(
peers.map(async ([key, value]) => {
value.name = await about.name(value.key);
@ -755,6 +755,10 @@ router
await meta.connStop();
ctx.redirect("/settings");
})
.post("/settings/conn/sync", koaBody(), async (ctx) => {
await meta.sync();
ctx.redirect("/settings");
})
.post("/settings/conn/restart", koaBody(), async (ctx) => {
await meta.connRestart();
ctx.redirect("/settings");

View File

@ -288,6 +288,21 @@ module.exports = ({ cooler, isPublic }) => {
);
});
},
connectedPeers: async () => {
const peers = await models.meta.peers();
return peers.filter(([address, data]) => {
if (data.state === "connected") {
return [address, data];
}
});
},
disconnect: async () => {
const ssb = await cooler.open();
const peers = await models.meta.peers();
peers.forEach(([address]) => {
ssb.conn.disconnect(address);
});
},
connStop: async () => {
const ssb = await cooler.open();
@ -315,6 +330,65 @@ module.exports = ({ cooler, isPublic }) => {
await models.meta.connStop();
await models.meta.connStart();
},
sync: async () => {
const ssb = await cooler.open();
await ssb.conn.start();
let syncDelay = 5000; //ms
const delay = (ms) => {
return new Promise((resolve) => setTimeout(resolve, ms));
};
// Let's wait a second for things to be ready
await delay(1000);
const originalProgress = await ssb.progress();
const originalTarget = originalProgress.indexes.target;
const waitForPeers = async () => {
const peers = await models.meta.connectedPeers();
if (peers && peers.length) {
debug("Connected to: ", peers);
} else {
await delay(500);
await waitForPeers();
}
};
debug("Waiting for peers to connect...");
await waitForPeers();
const getProgress = async (originalTarget, lastCheckTarget) => {
await delay(1000);
const progress = await ssb.progress();
const currentTarget = progress.indexes.target;
if (!lastCheckTarget) {
debug("Started with %s bytes", originalTarget);
await delay(syncDelay);
await getProgress(originalTarget, currentTarget);
} else {
debug(
"Last check was %s bytes and now we have %s bytes",
lastCheckTarget,
currentTarget
);
if (currentTarget > lastCheckTarget) {
await delay(syncDelay);
await getProgress(originalTarget, currentTarget);
}
if (currentTarget === lastCheckTarget) {
debug("Downloaded %s bytes", currentTarget - originalTarget);
}
}
};
await getProgress(originalTarget, false);
// conn.stop stops the scheduler but can leave connecting peers in limbo
await models.meta.disconnect();
await models.meta.connStop();
},
acceptInvite: async (invite) => {
const ssb = await cooler.open();
return await ssb.invite.accept(invite);

View File

@ -149,6 +149,7 @@ const i18n = {
startNetworking: "Start networking",
stopNetworking: "Stop networking",
restartNetworking: "Restart networking",
sync: "Connect and Sync",
indexes: "Indexes",
invites: "Invites",
invitesDescription:

View File

@ -769,10 +769,16 @@ exports.settingsView = ({ status, peers, theme, themeNames, version }) => {
button({ type: "submit" }, i18n.stopNetworking)
);
const syncButton = form(
{ action: "/settings/conn/sync", method: "post" },
button({ type: "submit" }, i18n.sync)
);
const connButtons = div({ class: "form-button-group" }, [
startButton,
restartButton,
stopButton,
syncButton,
]);
const peerList = (peers || [])