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