git ssb

4+

Dominic / scuttlebot



Commit aed774c44c87b37e617bc05b87adddd2467ba2c5

working on persisting gossip peers

Dominic Tarr committed on 10/9/2016, 1:35:49 AM
Parent: da63dafc1984303dc09aa3dffc765f2ec017d359

Files changed

plugins/gossip/index.jschanged
plugins/gossip/schedule.jschanged
plugins/gossip/index.jsView
@@ -7,12 +7,15 @@
77 var apidoc = require('../../lib/apidocs').gossip
88 var u = require('../../lib/util')
99 var ref = require('ssb-ref')
1010 var ping = require('pull-ping')
11-var Stats = require('statistics')
11 +var stats = require('statistics')
1212 var isArray = Array.isArray
1313 var Schedule = require('./schedule')
1414 var Init = require('./init')
15 +var AtomicFile = require('atomic-file')
16 +var path = require('path')
17 +var deepEqual = require('deep-equal')
1518
1619 function isFunction (f) {
1720 return 'function' === typeof f
1821 }
@@ -44,8 +47,10 @@
4447 var notify = Notify()
4548 var conf = config.gossip || {}
4649 var home = ref.parseAddress(server.getAddress())
4750
51 + var stateFile = AtomicFile(path.join(config.path, 'gossip.json'))
52 +
4853 //Known Peers
4954 var peers = []
5055
5156 function getPeer(id) {
@@ -90,9 +95,9 @@
9095 p.failure = (p.failure || 0) + 1
9196 p.stateChange = Date.now()
9297 notify({ type: 'connect-failure', peer: p })
9398 server.emit('log:info', ['SBOT', p.host+':'+p.port+p.key, 'connection failed', err.message || err])
94- p.duration.value(0)
99 + p.duration = stats(p.duration, 0)
95100 return (cb && cb(err))
96101 }
97102 else {
98103 p.state = 'connected'
@@ -133,9 +138,9 @@
133138 if(!f) {
134139 // new peer
135140 addr.source = source
136141 addr.announcers = 1
137- addr.duration = Stats()
142 + addr.duration = null
138143 peers.push(addr)
139144 notify({ type: 'discover', peer: addr, source: source || 'manual' })
140145 return addr
141146 }
@@ -199,18 +204,38 @@
199204 //or how many failures there have been.
200205 var since = peer.stateChange
201206 peer.stateChange = Date.now()
202207 if(peer.state === 'connected') //may be "disconnecting"
203- peer.duration.value(peer.stateChange - since)
208 + peer.duration = stats(peer.duration, peer.stateChange - since)
204209 peer.state = undefined
205210 notify({ type: 'disconnect', peer: peer })
206211 server.emit('log:info', ['SBOT', rpc.id, 'disconnect'])
207212 })
208213
209214 notify({ type: 'connect', peer: peer })
210215 })
211216
217 + var last
218 + stateFile.get(function (err, ary) {
219 + last = ary || []
220 + if(Array.isArray(ary))
221 + ary.forEach(function (v) {
222 + delete v.state
223 + var p = gossip.add(v, 'stored')
224 + })
225 + })
226 +
227 + setInterval(function () {
228 + if(deepEqual(peers, last)) return
229 + var copy = JSON.parse(JSON.stringify(peers))
230 + copy.forEach(function (e) {
231 + delete e.state
232 + })
233 + stateFile.set(copy, console.log.bind(console))
234 + }, 10*1000)
235 +
212236 return gossip
213237 }
214238 }
215239
216240
241 +
plugins/gossip/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('../../lib/util')
109
1110 function rand(array) {
@@ -42,10 +41,9 @@
4241 //(i.e. if there is only localhost)
4342
4443 function isOffline (e) {
4544 if(ip.isLoopback(e.host)) return false
46- var lo = Object.keys(os.networkInterfaces())
47- return lo.length === 1 && lo[0] === 'lo'
45 + return !hasNetwork()
4846 }
4947
5048 var isOnline = not(isOffline)
5149
@@ -61,20 +59,20 @@
6159
6260 //select peers which have never been successfully connected to yet,
6361 //but have been tried.
6462 function isInactive (e) {
65- return e.stateChange && e.duration.mean == 0
63 + return e.stateChange && (!e.duration || e.duration.mean == 0)
6664 }
6765
6866 function isLongterm (e) {
69- return e.ping && e.ping.rtt.mean > 0
67 + return e.ping && e.ping.rtt && e.ping.rtt.mean > 0
7068 }
7169
7270 //peers which we can connect to, but are not upgraded.
7371 //select peers which we can connect to, but are not upgraded to LT.
7472 //assume any peer is legacy, until we know otherwise...
7573 function isLegacy (peer) {
76- return peer.duration.mean > 0 && !exports.isLongterm(peer)
74 + return peer.duration && peer.duration.mean > 0 && !exports.isLongterm(peer)
7775 }
7876
7977 function isConnect (e) {
8078 return 'connected' === e.state || 'connecting' === e.state
@@ -102,9 +100,9 @@
102100 }
103101
104102 var schedule = exports = module.exports =
105103 function (gossip, config, server) {
106-
104 +// return
107105 var min = 60e3, hour = 60*60e3
108106
109107 //trigger hard reconnect after suspend or local network changes
110108 onWakeup(gossip.reconnect)
@@ -128,9 +126,10 @@
128126 gossip.disconnect(peer)
129127 })
130128 }
131129
132- select(peers, ts, and(filter, isOnline), opts)
130 + var selected = select(peers, ts, and(filter, isOnline), opts)
131 + selected
133132 .forEach(function (peer) {
134133 gossip.connect(peer)
135134 })
136135 }
@@ -140,40 +139,38 @@
140139 function connections () {
141140 if(connecting) return
142141 connecting = true
143142 setTimeout(function () {
144- connecting = false
145- var ts = Date.now()
146- var peers = gossip.peers()
143 + connecting = false
144 + var ts = Date.now()
145 + var peers = gossip.peers()
147146
148-// if(Math.random() > 0.1) return
147 + connect(peers, ts, 'attempt', exports.isUnattempted, {
148 + min: 0, quota: 10, factor: 0, max: 0, groupMin: 0,
149 + disable: !conf('global', true)
150 + })
149151
150- connect(peers, ts, 'attempt', exports.isUnattempted, {
151- min: 0, quota: 10, factor: 0, max: 0, groupMin: 0,
152- disable: !conf('global', true)
153- })
152 + //quota, groupMin, min, factor, max
153 + connect(peers, ts, 'retry', exports.isInactive, {
154 + min: 0,
155 + quota: 3, factor: 5*60e3, max: 3*60*60e3, groupMin: 5*50e3
156 + })
154157
155- //quota, groupMin, min, factor, max
156- connect(peers, ts, 'retry', exports.isInactive, {
157- min: 0,
158- quota: 3, factor: 5*60e3, max: 3*60*60e3, groupMin: 5*50e3
159- })
158 + connect(peers, ts, 'legacy', exports.isLegacy, {
159 + quota: 3, factor: 5*min, max: 3*hour, groupMin: 5*min,
160 + disable: !conf('global', true)
161 + })
160162
161- connect(peers, ts, 'legacy', exports.isLegacy, {
162- quota: 3, factor: 5*min, max: 3*hour, groupMin: 5*min,
163 + connect(peers, ts, 'longterm', exports.isLongterm, {
164 + quota: 3, factor: 10e3, max: 10*min, groupMin: 5e3,
163165 disable: !conf('global', true)
164166 })
165167
166- connect(peers, ts, 'longterm', exports.isLongterm, {
167- quota: 3, factor: 10e3, max: 10*min, groupMin: 5e3,
168- disable: !conf('global', true)
169- })
168 + connect(peers, ts, 'local', exports.isLocal, {
169 + quota: 3, factor: 2e3, max: 10*min, groupMin: 1e3,
170 + disable: !conf('local', true)
171 + })
170172
171- connect(peers, ts, 'local', exports.isLocal, {
172- quota: 3, factor: 2e3, max: 10*min, groupMin: 1e3,
173- disable: !conf('local', true)
174- })
175-
176173 }, 100*Math.random())
177174
178175 }
179176
@@ -201,4 +198,9 @@
201198 exports.select = select
202199
203200
204201
202 +
203 +
204 +
205 +
206 +

Built with git-ssb-web