git ssb

0+

alanz / patchwork



forked from Matt McKegg / patchwork

Commit 650fccfe928ae39c6023e3e2af686943028bb9af

use scuttlebot persistent-gossip

Matt McKegg committed on 11/2/2016, 12:18:35 AM
Parent: 61a89f2614a9e41e661b0fca3162cd220b9c1b8e

Files changed

lib/gossip-with-slow-rollout/schedule.jschanged
package.jsonchanged
lib/gossip-with-slow-rollout/schedule.jsView
@@ -1,11 +1,10 @@
11 var nonPrivate = require('non-private-ip')
22 var ip = require('ip')
33 var onWakeup = require('on-wakeup')
44 var onNetwork = require('on-change-network')
5+var hasNetwork = require('has-network')
56
6-var Stats = require('statistics')
7-var os = require('os')
87 var pull = require('pull-stream')
98 var u = require('scuttlebot/lib/util')
109
1110 function rand(array) {
@@ -36,16 +35,18 @@
3635 function peerNext(peer, opts) {
3736 return (peer.stateChange|0) + delay(peer.failure|0, opts.factor, opts.max)
3837 }
3938
39+function isFollowing (e) {
40+ return e.source === 'self'
41+}
4042
4143 //detect if not connected to wifi or other network
4244 //(i.e. if there is only localhost)
4345
4446 function isOffline (e) {
4547 if(ip.isLoopback(e.host)) return false
46- var lo = Object.keys(os.networkInterfaces())
47- return lo.length === 1 && lo[0] === 'lo'
48+ return !hasNetwork()
4849 }
4950
5051 var isOnline = not(isOffline)
5152
@@ -54,31 +55,27 @@
5455 // cjdns creates fake private ip addresses.
5556 return ip.isPrivate(e.host) && e.type === 'local'
5657 }
5758
58-function isFollowing (e) {
59- return e.source === 'self'
60-}
61-
6259 function isUnattempted (e) {
6360 return !e.stateChange
6461 }
6562
6663 //select peers which have never been successfully connected to yet,
6764 //but have been tried.
6865 function isInactive (e) {
69- return e.stateChange && e.duration.mean == 0
66+ return e.stateChange && (!e.duration || e.duration.mean == 0)
7067 }
7168
7269 function isLongterm (e) {
73- return e.ping && e.ping.rtt.mean > 0
70+ return e.ping && e.ping.rtt && e.ping.rtt.mean > 0
7471 }
7572
7673 //peers which we can connect to, but are not upgraded.
7774 //select peers which we can connect to, but are not upgraded to LT.
7875 //assume any peer is legacy, until we know otherwise...
7976 function isLegacy (peer) {
80- return peer.duration.mean > 0 && !exports.isLongterm(peer)
77+ return peer.duration && peer.duration.mean > 0 && !exports.isLongterm(peer)
8178 }
8279
8380 function isConnect (e) {
8481 return 'connected' === e.state || 'connecting' === e.state
@@ -106,9 +103,9 @@
106103 }
107104
108105 var schedule = exports = module.exports =
109106 function (gossip, config, server) {
110-
107+// return
111108 var min = 60e3, hour = 60*60e3
112109
113110 //trigger hard reconnect after suspend or local network changes
114111 onWakeup(gossip.reconnect)
@@ -121,21 +118,20 @@
121118 }
122119
123120 function connect (peers, ts, name, filter, opts) {
124121 var connected = peers.filter(isConnect).filter(filter)
125- .filter(function (peer) {
126- // don't disconnect from followed pubs
127- return !isFollowing(peer) && peer.stateChange + 10e3 < ts
128- })
129122
123+ //disconnect if over quota
130124 if(connected.length > opts.quota) {
131125 return earliest(connected, connected.length - opts.quota)
132126 .forEach(function (peer) {
133127 gossip.disconnect(peer)
134128 })
135129 }
136130
137- select(peers, ts, and(filter, isOnline), opts)
131+ //will return [] if the quota is full
132+ var selected = select(peers, ts, and(filter, isOnline), opts)
133+ selected
138134 .forEach(function (peer) {
139135 gossip.connect(peer)
140136 })
141137 }
@@ -145,44 +141,56 @@
145141 function connections () {
146142 if(connecting) return
147143 connecting = true
148144 setTimeout(function () {
149- connecting = false
150- var ts = Date.now()
151- var peers = gossip.peers()
145+ connecting = false
146+ var ts = Date.now()
147+ var peers = gossip.peers()
152148
153-// if(Math.random() > 0.1) return
149+ var connected = peers.filter(isConnect).length
154150
155- connect(peers, ts, 'following', exports.isFollowing, {
151+ connect(peers, ts, 'following', exports.isFollowing, {
156152 min: 0, quota: 5, factor: 0, max: 0, groupMin: 0,
157153 disable: !conf('global', true)
158- })
154+ })
159155
160- connect(peers, ts, 'attempt', exports.isUnattempted, {
161- min: 0, quota: 1, factor: 2e3, max: 0, groupMin: 0,
156+ connect(peers, ts, 'longterm', exports.isLongterm, {
157+ quota: 3, factor: 10e3, max: 10*min, groupMin: 5e3,
162158 disable: !conf('global', true)
163- })
159+ })
164160
165- //quota, groupMin, min, factor, max
166- connect(peers, ts, 'retry', exports.isInactive, {
161+ connect(peers, ts, 'local', exports.isLocal, {
162+ quota: 3, factor: 2e3, max: 10*min, groupMin: 1e3,
163+ disable: !conf('local', true)
164+ })
165+
166+ if(connected === 0)
167+ connect(peers, ts, 'attempt', exports.isUnattempted, {
168+ min: 0, quota: 1, factor: 0, max: 0, groupMin: 0,
169+ disable: !conf('global', true)
170+ })
171+
172+ //quota, groupMin, min, factor, max
173+ connect(peers, ts, 'retry', exports.isInactive, {
167174 min: 0,
168175 quota: 3, factor: 5*60e3, max: 3*60*60e3, groupMin: 5*50e3
169176 })
170177
171- connect(peers, ts, 'legacy', exports.isLegacy, {
172- quota: 3, factor: 5*min, max: 3*hour, groupMin: 5*min,
178+ var longterm = peers.filter(isConnect).filter(exports.isLongterm).length
179+
180+ connect(peers, ts, 'legacy', exports.isLegacy, {
181+ quota: 3 - longterm,
182+ factor: 5*min, max: 3*hour, groupMin: 5*min,
173183 disable: !conf('global', true)
174184 })
175185
176- connect(peers, ts, 'longterm', exports.isLongterm, {
177- quota: 3, factor: 10e3, max: 10*min, groupMin: 5e3,
178- disable: !conf('global', true)
179- })
186+ peers.filter(isConnect).forEach(function (e) {
187+ if((!exports.isLongterm(e) || e.state === 'connecting') && e.stateChange + 10e3 < ts) {
188+ console.log('disconnect', exports.isLongterm(e), e.state, e.stateChange - ts)
189+ gossip.disconnect(e)
190+ }
180191
181- connect(peers, ts, 'local', exports.isLocal, {
182- quota: 3, factor: 2e3, max: 10*min, groupMin: 1e3,
183- disable: !conf('local', true)
184- })
192+ })
185193
186194 }, 100*Math.random())
187195
188196 }
@@ -203,10 +211,10 @@
203211 }
204212
205213 exports.isUnattempted = isUnattempted
206214 exports.isInactive = isInactive
215+exports.isFollowing = isFollowing
207216 exports.isLongterm = isLongterm
208-exports.isFollowing = isFollowing
209217 exports.isLegacy = isLegacy
210218 exports.isLocal = isLocal
211219 exports.isConnectedOrConnecting = isConnect
212220 exports.select = select
package.jsonView
@@ -15,11 +15,15 @@
1515 "@mmckegg/mutant": "~3.7.1",
1616 "data-uri-to-buffer": "0.0.4",
1717 "electron": "~1.4.4",
1818 "electron-default-menu": "~1.0.0",
19+ "has-network": "0.0.0",
1920 "insert-css": "~1.0.0",
2021 "is-visible": "^2.1.1",
2122 "micro-css": "~0.6.2",
23+ "non-private-ip": "^1.4.1",
24+ "on-change-network": "0.0.2",
25+ "on-wakeup": "^1.0.1",
2226 "patchbay": "~3.6.1",
2327 "pull-abortable": "^4.1.0",
2428 "pull-file": "~1.0.0",
2529 "pull-identify-filetype": "^1.1.0",

Built with git-ssb-web