ssb-conn
Advanced tools
Comparing version 0.11.3 to 0.12.0
@@ -39,2 +39,5 @@ "use strict"; | ||
} | ||
function notRoom(peer) { | ||
return peer[1].type !== 'room'; | ||
} | ||
function take(n) { | ||
@@ -248,2 +251,3 @@ return (arr) => arr.slice(0, Math.max(n, 0)); | ||
updateHubNow() { | ||
const conn = this.ssb.conn; | ||
if (this.conf('seed', true)) { | ||
@@ -257,3 +261,3 @@ this.updateTheseConnections(p => p[1].source === 'seed', { | ||
} | ||
if (this.ssb.conn.query().peersInConnection().length === 0) { | ||
if (conn.query().peersInConnection().length === 0) { | ||
this.updateTheseConnections(() => true, { | ||
@@ -267,3 +271,3 @@ quota: 1, | ||
this.updateTheseConnections(p => p[1].type === 'room', { | ||
quota: 10, | ||
quota: 5, | ||
backoffStep: 5e3, | ||
@@ -273,3 +277,3 @@ backoffMax: 5 * minute, | ||
}); | ||
this.updateTheseConnections(hasPinged, { | ||
this.updateTheseConnections(p => notRoom(p) && hasPinged(p), { | ||
quota: 2, | ||
@@ -280,3 +284,3 @@ backoffStep: 10e3, | ||
}); | ||
this.updateTheseConnections(hasNoAttempts, { | ||
this.updateTheseConnections(p => notRoom(p) && hasNoAttempts(p), { | ||
quota: 2, | ||
@@ -287,3 +291,3 @@ backoffStep: 30e3, | ||
}); | ||
this.updateTheseConnections(hasOnlyFailedAttempts, { | ||
this.updateTheseConnections(p => notRoom(p) && hasOnlyFailedAttempts(p), { | ||
quota: 3, | ||
@@ -294,3 +298,3 @@ backoffStep: 1 * minute, | ||
}); | ||
this.updateTheseConnections(isLegacy, { | ||
this.updateTheseConnections(p => notRoom(p) && isLegacy(p), { | ||
quota: 1, | ||
@@ -301,20 +305,20 @@ backoffStep: 4 * minute, | ||
}); | ||
this.ssb.conn | ||
conn | ||
.query() | ||
.peersConnectable('staging') | ||
.filter(this.weFollowThem) | ||
.z(take(5)) | ||
.forEach(([addr, data]) => this.ssb.conn.connect(addr, data)); | ||
this.ssb.conn | ||
.z(take(3 - conn.query().peersInConnection().length)) | ||
.forEach(([addr, data]) => conn.connect(addr, data)); | ||
conn | ||
.query() | ||
.peersInConnection() | ||
.filter(this.weBlockThem) | ||
.forEach(([addr]) => this.ssb.conn.disconnect(addr)); | ||
this.ssb.conn | ||
.forEach(([addr]) => conn.disconnect(addr)); | ||
conn | ||
.query() | ||
.peersInConnection() | ||
.filter(p => this.ssb.conn.hub().getState(p[0]) === 'connecting') | ||
.filter(p => conn.hub().getState(p[0]) === 'connecting') | ||
.filter(p => p[1].stateChange + this.maxWaitToConnect(p) < Date.now()) | ||
.forEach(([addr]) => this.ssb.conn.disconnect(addr)); | ||
this.ssb.conn | ||
.forEach(([addr]) => conn.disconnect(addr)); | ||
conn | ||
.query() | ||
@@ -324,3 +328,3 @@ .peersConnected() | ||
.filter(p => p[1].stateChange + 0.5 * hour < Date.now()) | ||
.forEach(([addr]) => this.ssb.conn.disconnect(addr)); | ||
.forEach(([addr]) => conn.disconnect(addr)); | ||
} | ||
@@ -327,0 +331,0 @@ updateNow() { |
{ | ||
"name": "ssb-conn", | ||
"description": "SSB plugin for establishing and managing peer connections", | ||
"version": "0.11.3", | ||
"version": "0.12.0", | ||
"homepage": "https://github.com/staltz/ssb-conn", | ||
@@ -6,0 +6,0 @@ "main": "lib/index.js", |
@@ -74,3 +74,3 @@ <div style="text-align:center" align="center"> | ||
``` | ||
where: | ||
where: | ||
- `addr` is a multiserver address (a **string** that [follows some rules](https://github.com/dominictarr/multiserver-address)) | ||
@@ -114,3 +114,3 @@ - `data` is an **object** with additional information about the peer (fields marked 🔷 are important and often used, fields marked 🔹 come from CONN, fields marked 🔸 are ad-hoc and added by various other modules, and fields suffixed with `?` are not always present): | ||
🔸 `onlineCount?: number`: (only if `type = 'room'`) the number of room endpoints currently connected to this room | ||
## Gossip compatibility | ||
@@ -180,15 +180,22 @@ | ||
- With (10sec) exponential backoff, try to connect to peers that we have connected successfully before | ||
- With (30sec) exponential backoff, try to connect to peers that we have never with connected before | ||
- With (1min) exponential backoff, try to connect to peers that have we always failed to connect with | ||
- With (4min) exponential backoff, try to connect to peers that seem to run a legacy version of the gossip plugin | ||
- With (5sec) exponential backoff, try to connect to at most 5 room servers | ||
- With (10sec) exponential backoff, try to connect to at most 2 non-room peers that we have connected successfully before | ||
- With (30sec) exponential backoff, try to connect to at most 2 non-room peers that we have never with connected before | ||
- With (1min) exponential backoff, try to connect to at most 3 non-room peers that have we always failed to connect with | ||
- With (4min) exponential backoff, try to connect to at most 1 non-room peer that seem to run a legacy version of the gossip plugin | ||
In none of the cases above shall we connect to a peer that we block. | ||
In none of the cases above shall we connect to a peer that we block. In addition to the above, the following actions happen automatically every (approximately) 1 second: | ||
- Automatically connect to (five) staged peers we follow | ||
- Disconnect from connected peers that have just been blocked | ||
- Connect to (at most 3) staged peers we follow | ||
- Disconnect from connected peers that have just been blocked by us | ||
- Disconnect from peers that have been connected with us for more than 30min | ||
- Disconnect from peers that have been pending in "connecting" status for too long | ||
- "Too long" means 30sec for LAN peers | ||
- "Too long" means 1min for Bluetooth peers | ||
- "Too long" means 5min for DHT invite peers | ||
- For other types of peers, "too long" means 10sec | ||
- Stage non-blocked peers that are in ConnDB, marked as `autoconnect=false` | ||
- Unstage peers that have just been blocked by us | ||
- Unstage LAN peers that haven't been updated in ConnStaging in 10 seconds | ||
- Unstage Bluetooth peers that haven't been updated in ConnStaging in 30 seconds | ||
- Wait some 10 seconds before disconnecting a "connecting" peer | ||
- Disconnect peer that have been online with us for more than 1 hour | ||
@@ -195,0 +202,0 @@ **Other events:** |
@@ -45,2 +45,6 @@ import ConnQuery = require('ssb-conn-query'); | ||
function notRoom(peer: Peer): boolean { | ||
return peer[1].type !== 'room'; | ||
} | ||
function take(n: number) { | ||
@@ -264,2 +268,4 @@ return <T>(arr: Array<T>) => arr.slice(0, Math.max(n, 0)); | ||
private updateHubNow() { | ||
const conn = this.ssb.conn; | ||
if (this.conf('seed', true)) { | ||
@@ -275,3 +281,3 @@ this.updateTheseConnections(p => p[1].source === 'seed', { | ||
// If there are no peers, then try *any* connection ASAP | ||
if (this.ssb.conn.query().peersInConnection().length === 0) { | ||
if (conn.query().peersInConnection().length === 0) { | ||
this.updateTheseConnections(() => true, { | ||
@@ -285,5 +291,5 @@ quota: 1, | ||
// Connect to rooms, up to 10 of them, prioritized over pubs | ||
// Connect to rooms, up to 5 of them | ||
this.updateTheseConnections(p => p[1].type === 'room', { | ||
quota: 10, | ||
quota: 5, | ||
backoffStep: 5e3, | ||
@@ -294,3 +300,3 @@ backoffMax: 5 * minute, | ||
this.updateTheseConnections(hasPinged, { | ||
this.updateTheseConnections(p => notRoom(p) && hasPinged(p), { | ||
quota: 2, | ||
@@ -302,3 +308,3 @@ backoffStep: 10e3, | ||
this.updateTheseConnections(hasNoAttempts, { | ||
this.updateTheseConnections(p => notRoom(p) && hasNoAttempts(p), { | ||
quota: 2, | ||
@@ -310,3 +316,3 @@ backoffStep: 30e3, | ||
this.updateTheseConnections(hasOnlyFailedAttempts, { | ||
this.updateTheseConnections(p => notRoom(p) && hasOnlyFailedAttempts(p), { | ||
quota: 3, | ||
@@ -318,3 +324,3 @@ backoffStep: 1 * minute, | ||
this.updateTheseConnections(isLegacy, { | ||
this.updateTheseConnections(p => notRoom(p) && isLegacy(p), { | ||
quota: 1, | ||
@@ -326,27 +332,27 @@ backoffStep: 4 * minute, | ||
// Automatically connect to (five) staged peers we follow | ||
this.ssb.conn | ||
// Automatically connect to some (up to 3) staged peers we follow | ||
conn | ||
.query() | ||
.peersConnectable('staging') | ||
.filter(this.weFollowThem) | ||
.z(take(5)) | ||
.forEach(([addr, data]) => this.ssb.conn.connect(addr, data)); | ||
.z(take(3 - conn.query().peersInConnection().length)) | ||
.forEach(([addr, data]) => conn.connect(addr, data)); | ||
// Purge connected peers that are now blocked | ||
this.ssb.conn | ||
conn | ||
.query() | ||
.peersInConnection() | ||
.filter(this.weBlockThem) | ||
.forEach(([addr]) => this.ssb.conn.disconnect(addr)); | ||
.forEach(([addr]) => conn.disconnect(addr)); | ||
// Purge some ongoing frustrating connection attempts | ||
this.ssb.conn | ||
conn | ||
.query() | ||
.peersInConnection() | ||
.filter(p => this.ssb.conn.hub().getState(p[0]) === 'connecting') | ||
.filter(p => conn.hub().getState(p[0]) === 'connecting') | ||
.filter(p => p[1].stateChange! + this.maxWaitToConnect(p) < Date.now()) | ||
.forEach(([addr]) => this.ssb.conn.disconnect(addr)); | ||
.forEach(([addr]) => conn.disconnect(addr)); | ||
// Purge an internet connection after it has been up for half an hour | ||
this.ssb.conn | ||
conn | ||
.query() | ||
@@ -356,3 +362,3 @@ .peersConnected() | ||
.filter(p => p[1].stateChange! + 0.5 * hour < Date.now()) | ||
.forEach(([addr]) => this.ssb.conn.disconnect(addr)); | ||
.forEach(([addr]) => conn.disconnect(addr)); | ||
} | ||
@@ -359,0 +365,0 @@ |
137458
2452
290