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