Commit ed7e4997927bef14491b40767dbd64f58f2d246e
sbot.close should kill any current connections, and should allow connections to localhost while offline
Dominic Tarr committed on 6/14/2017, 9:52:53 PMParent: a8eda5e5db9ec86d6df1064d0c0c8e1c23a5d62f
Files changed
plugins/gossip/index.js | changed |
plugins/gossip/schedule.js | changed |
plugins/gossip/index.js | ||
---|---|---|
@@ -42,8 +42,9 @@ | ||
42 | 42 | anonymous: {allow: ['ping']} |
43 | 43 | }, |
44 | 44 | init: function (server, config) { |
45 | 45 | var notify = Notify() |
46 | + var closed = false, closeScheduler | |
46 | 47 | var conf = config.gossip || {} |
47 | 48 | var home = ref.parseAddress(server.getAddress()) |
48 | 49 | |
49 | 50 | var stateFile = AtomicFile(path.join(config.path, 'gossip.json')) |
@@ -56,8 +57,18 @@ | ||
56 | 57 | return e && e.key === id |
57 | 58 | }) |
58 | 59 | } |
59 | 60 | |
61 | + server.close.hook(function (fn, args) { | |
62 | + closed = true | |
63 | + closeScheduler() | |
64 | + for(var id in server.peers) | |
65 | + server.peers[id].forEach(function (peer) { | |
66 | + peer.close(true) | |
67 | + }) | |
68 | + return fn.apply(this, args) | |
69 | + }) | |
70 | + | |
60 | 71 | var timer_ping = 5*6e4 |
61 | 72 | |
62 | 73 | var gossip = { |
63 | 74 | wakeup: 0, |
@@ -74,8 +85,9 @@ | ||
74 | 85 | ) |
75 | 86 | }) |
76 | 87 | }, |
77 | 88 | connect: valid.async(function (addr, cb) { |
89 | + console.log("CONNECT", addr) | |
78 | 90 | addr = ref.parseAddress(addr) |
79 | 91 | if (!addr || typeof addr != 'object') |
80 | 92 | return cb(new Error('first param must be an address')) |
81 | 93 | |
@@ -173,9 +185,9 @@ | ||
173 | 185 | return gossip.wakeup = Date.now() |
174 | 186 | } |
175 | 187 | } |
176 | 188 | |
177 | - Schedule (gossip, config, server) | |
189 | + closeScheduler = Schedule (gossip, config, server) | |
178 | 190 | Init (gossip, config, server) |
179 | 191 | //get current state |
180 | 192 | |
181 | 193 | server.on('rpc:connect', function (rpc, isClient) { |
@@ -264,4 +276,6 @@ | ||
264 | 276 | return gossip |
265 | 277 | } |
266 | 278 | } |
267 | 279 | |
280 | + | |
281 | + |
plugins/gossip/schedule.js | ||
---|---|---|
@@ -1,4 +1,5 @@ | ||
1 | +'use strict' | |
1 | 2 | var ip = require('ip') |
2 | 3 | var onWakeup = require('on-wakeup') |
3 | 4 | var onNetwork = require('on-change-network') |
4 | 5 | var hasNetwork = require('has-network') |
@@ -34,9 +35,9 @@ | ||
34 | 35 | //detect if not connected to wifi or other network |
35 | 36 | //(i.e. if there is only localhost) |
36 | 37 | |
37 | 38 | function isOffline (e) { |
38 | - if(ip.isLoopback(e.host)) return false | |
39 | + if(ip.isLoopback(e.host) || e.host == 'localhost') return false | |
39 | 40 | return !hasNetwork() |
40 | 41 | } |
41 | 42 | |
42 | 43 | var isOnline = not(isOffline) |
@@ -104,9 +105,9 @@ | ||
104 | 105 | |
105 | 106 | var schedule = exports = module.exports = |
106 | 107 | function (gossip, config, server) { |
107 | 108 | // return |
108 | - var min = 60e3, hour = 60*60e3 | |
109 | + var min = 60e3, hour = 60*60e3, closed = false | |
109 | 110 | |
110 | 111 | //trigger hard reconnect after suspend or local network changes |
111 | 112 | onWakeup(gossip.reconnect) |
112 | 113 | onNetwork(gossip.reconnect) |
@@ -139,11 +140,11 @@ | ||
139 | 140 | |
140 | 141 | |
141 | 142 | var connecting = false |
142 | 143 | function connections () { |
143 | - if(connecting) return | |
144 | + if(connecting || closed) return | |
144 | 145 | connecting = true |
145 | - setTimeout(function () { | |
146 | + var timer = setTimeout(function () { | |
146 | 147 | connecting = false |
147 | 148 | var ts = Date.now() |
148 | 149 | var peers = gossip.peers() |
149 | 150 | |
@@ -154,9 +155,9 @@ | ||
154 | 155 | if(conf('seed', true)) |
155 | 156 | connect(peers, ts, 'seeds', isSeed, { |
156 | 157 | quota: 3, factor: 2e3, max: 10*min, groupMin: 1e3, |
157 | 158 | }) |
158 | - | |
159 | + return //XXX | |
159 | 160 | if(conf('local', true)) |
160 | 161 | connect(peers, ts, 'local', isLocal, { |
161 | 162 | quota: 3, factor: 2e3, max: 10*min, groupMin: 1e3, |
162 | 163 | }) |
@@ -212,9 +213,9 @@ | ||
212 | 213 | } |
213 | 214 | }) |
214 | 215 | |
215 | 216 | }, 100*Math.random()) |
216 | - | |
217 | + if(timer.unref) timer.unref() | |
217 | 218 | } |
218 | 219 | |
219 | 220 | pull( |
220 | 221 | gossip.changes(), |
@@ -228,8 +229,13 @@ | ||
228 | 229 | if(int.unref) int.unref() |
229 | 230 | |
230 | 231 | connections() |
231 | 232 | |
233 | + return function onClose () { | |
234 | + closed = true | |
235 | + | |
236 | + } | |
237 | + | |
232 | 238 | } |
233 | 239 | |
234 | 240 | exports.isUnattempted = isUnattempted |
235 | 241 | exports.isInactive = isInactive |
Built with git-ssb-web