Files: f8a3919796796fb604a4d98decee02f962ee7cee / lib / persistent-gossip / schedule.js
5533 bytesRaw
1 | var ip = require('ip') |
2 | var onWakeup = require('on-wakeup') |
3 | var onNetwork = require('on-change-network') |
4 | var hasNetwork = require('has-network') |
5 | |
6 | var pull = require('pull-stream') |
7 | |
8 | function not (fn) { |
9 | return function (e) { return !fn(e) } |
10 | } |
11 | |
12 | function and () { |
13 | var args = [].slice.call(arguments) |
14 | return function (value) { |
15 | return args.every(function (fn) { return fn.call(null, value) }) |
16 | } |
17 | } |
18 | |
19 | //min delay (delay since last disconnect of most recent peer in unconnected set) |
20 | //unconnected filter delay peer < min delay |
21 | function delay (failures, factor, max) { |
22 | return Math.min(Math.pow(2, failures)*factor, max || Infinity) |
23 | } |
24 | |
25 | function maxStateChange (M, e) { |
26 | return Math.max(M, e.stateChange || 0) |
27 | } |
28 | |
29 | function peerNext(peer, opts) { |
30 | return (peer.stateChange|0) + delay(peer.failure|0, opts.factor, opts.max) |
31 | } |
32 | |
33 | |
34 | //detect if not connected to wifi or other network |
35 | //(i.e. if there is only localhost) |
36 | |
37 | function isOffline (e) { |
38 | if(ip.isLoopback(e.host)) return false |
39 | return !hasNetwork() |
40 | } |
41 | |
42 | var isOnline = not(isOffline) |
43 | |
44 | function isLocal (e) { |
45 | // don't rely on private ip address, because |
46 | // cjdns creates fake private ip addresses. |
47 | return ip.isPrivate(e.host) && e.source === 'local' |
48 | } |
49 | |
50 | function isUnattempted (e) { |
51 | return !e.stateChange |
52 | } |
53 | |
54 | //select peers which have never been successfully connected to yet, |
55 | //but have been tried. |
56 | function isInactive (e) { |
57 | return e.stateChange && (!e.duration || e.duration.mean == 0) |
58 | } |
59 | |
60 | function isLongterm (e) { |
61 | return e.ping && e.ping.rtt && e.ping.rtt.mean > 0 |
62 | } |
63 | |
64 | //peers which we can connect to, but are not upgraded. |
65 | //select peers which we can connect to, but are not upgraded to LT. |
66 | //assume any peer is legacy, until we know otherwise... |
67 | function isLegacy (peer) { |
68 | return peer.duration && peer.duration.mean > 0 && !exports.isLongterm(peer) |
69 | } |
70 | |
71 | function isConnect (e) { |
72 | return 'connected' === e.state || 'connecting' === e.state |
73 | } |
74 | |
75 | //sort oldest to newest then take first n |
76 | function earliest(peers, n) { |
77 | return peers.sort(function (a, b) { |
78 | return a.stateChange - b.stateChange |
79 | }).slice(0, Math.max(n, 0)) |
80 | } |
81 | |
82 | function select(peers, ts, filter, opts) { |
83 | if(opts.disable) return [] |
84 | //opts: { quota, groupMin, min, factor, max } |
85 | var type = peers.filter(filter) |
86 | var unconnect = type.filter(not(isConnect)) |
87 | var count = Math.max(opts.quota - type.filter(isConnect).length, 0) |
88 | var min = unconnect.reduce(maxStateChange, 0) + opts.groupMin |
89 | if(ts < min) return [] |
90 | |
91 | return earliest(unconnect.filter(function (peer) { |
92 | return peerNext(peer, opts) < ts |
93 | }), count) |
94 | } |
95 | |
96 | var schedule = exports = module.exports = |
97 | function (gossip, config, server) { |
98 | // return |
99 | var min = 60e3, hour = 60*60e3 |
100 | |
101 | //trigger hard reconnect after suspend or local network changes |
102 | onWakeup(gossip.reconnect) |
103 | onNetwork(gossip.reconnect) |
104 | |
105 | function conf(name, def) { |
106 | if(!config.gossip) return def |
107 | var value = config.gossip[name] |
108 | return (value === undefined || value === '') ? def : value |
109 | } |
110 | |
111 | function connect (peers, ts, name, filter, opts) { |
112 | var connected = peers.filter(isConnect).filter(filter) |
113 | |
114 | //disconnect if over quota |
115 | if(connected.length > opts.quota) { |
116 | return earliest(connected, connected.length - opts.quota) |
117 | .forEach(function (peer) { |
118 | gossip.disconnect(peer) |
119 | }) |
120 | } |
121 | |
122 | //will return [] if the quota is full |
123 | var selected = select(peers, ts, and(filter, isOnline), opts) |
124 | selected |
125 | .forEach(function (peer) { |
126 | gossip.connect(peer) |
127 | }) |
128 | } |
129 | |
130 | |
131 | var connecting = false |
132 | function connections () { |
133 | if(connecting) return |
134 | connecting = true |
135 | setTimeout(function () { |
136 | connecting = false |
137 | var ts = Date.now() |
138 | var peers = gossip.peers() |
139 | |
140 | var connected = peers.filter(and(isConnect, not(isLocal))).length |
141 | |
142 | connect(peers, ts, 'longterm', exports.isLongterm, { |
143 | quota: 3, factor: 10e3, max: 10*min, groupMin: 5e3, |
144 | disable: !conf('global', true) |
145 | }) |
146 | |
147 | connect(peers, ts, 'local', exports.isLocal, { |
148 | quota: 3, factor: 2e3, max: 10*min, groupMin: 1e3, |
149 | disable: !conf('local', true) |
150 | }) |
151 | |
152 | if(connected < 3) |
153 | connect(peers, ts, 'attempt', exports.isUnattempted, { |
154 | min: 0, quota: 1, factor: 0, max: 0, groupMin: 0, |
155 | disable: !conf('global', true) |
156 | }) |
157 | |
158 | //quota, groupMin, min, factor, max |
159 | connect(peers, ts, 'retry', exports.isInactive, { |
160 | min: 0, |
161 | quota: 3, factor: 5*60e3, max: 3*60*60e3, groupMin: 5*50e3 |
162 | }) |
163 | |
164 | var longterm = peers.filter(isConnect).filter(exports.isLongterm).length |
165 | |
166 | connect(peers, ts, 'legacy', exports.isLegacy, { |
167 | quota: 3 - longterm, |
168 | factor: 5*min, max: 3*hour, groupMin: 5*min, |
169 | disable: !conf('global', true) |
170 | }) |
171 | |
172 | peers.filter(isConnect).forEach(function (e) { |
173 | var permanent = exports.isLongterm(e) || exports.isLocal(e) |
174 | if((!permanent || e.state === 'connecting') && e.stateChange + 10e3 < ts) { |
175 | gossip.disconnect(e) |
176 | } |
177 | }) |
178 | |
179 | }, 100*Math.random()) |
180 | |
181 | } |
182 | |
183 | pull( |
184 | gossip.changes(), |
185 | pull.drain(function (ev) { |
186 | if(ev.type == 'disconnect') |
187 | connections() |
188 | }) |
189 | ) |
190 | |
191 | var int = setInterval(connections, 2e3) |
192 | if(int.unref) int.unref() |
193 | |
194 | connections() |
195 | |
196 | } |
197 | |
198 | exports.isUnattempted = isUnattempted |
199 | exports.isInactive = isInactive |
200 | exports.isLongterm = isLongterm |
201 | exports.isLegacy = isLegacy |
202 | exports.isLocal = isLocal |
203 | exports.isConnectedOrConnecting = isConnect |
204 | exports.select = select |
205 |
Built with git-ssb-web