git ssb

4+

Dominic / scuttlebot



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 PM
Parent: a8eda5e5db9ec86d6df1064d0c0c8e1c23a5d62f

Files changed

plugins/gossip/index.jschanged
plugins/gossip/schedule.jschanged
plugins/gossip/index.jsView
@@ -42,8 +42,9 @@
4242 anonymous: {allow: ['ping']}
4343 },
4444 init: function (server, config) {
4545 var notify = Notify()
46+ var closed = false, closeScheduler
4647 var conf = config.gossip || {}
4748 var home = ref.parseAddress(server.getAddress())
4849
4950 var stateFile = AtomicFile(path.join(config.path, 'gossip.json'))
@@ -56,8 +57,18 @@
5657 return e && e.key === id
5758 })
5859 }
5960
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+
6071 var timer_ping = 5*6e4
6172
6273 var gossip = {
6374 wakeup: 0,
@@ -74,8 +85,9 @@
7485 )
7586 })
7687 },
7788 connect: valid.async(function (addr, cb) {
89+ console.log("CONNECT", addr)
7890 addr = ref.parseAddress(addr)
7991 if (!addr || typeof addr != 'object')
8092 return cb(new Error('first param must be an address'))
8193
@@ -173,9 +185,9 @@
173185 return gossip.wakeup = Date.now()
174186 }
175187 }
176188
177- Schedule (gossip, config, server)
189+ closeScheduler = Schedule (gossip, config, server)
178190 Init (gossip, config, server)
179191 //get current state
180192
181193 server.on('rpc:connect', function (rpc, isClient) {
@@ -264,4 +276,6 @@
264276 return gossip
265277 }
266278 }
267279
280+
281+
plugins/gossip/schedule.jsView
@@ -1,4 +1,5 @@
1+'use strict'
12 var ip = require('ip')
23 var onWakeup = require('on-wakeup')
34 var onNetwork = require('on-change-network')
45 var hasNetwork = require('has-network')
@@ -34,9 +35,9 @@
3435 //detect if not connected to wifi or other network
3536 //(i.e. if there is only localhost)
3637
3738 function isOffline (e) {
38- if(ip.isLoopback(e.host)) return false
39+ if(ip.isLoopback(e.host) || e.host == 'localhost') return false
3940 return !hasNetwork()
4041 }
4142
4243 var isOnline = not(isOffline)
@@ -104,9 +105,9 @@
104105
105106 var schedule = exports = module.exports =
106107 function (gossip, config, server) {
107108 // return
108- var min = 60e3, hour = 60*60e3
109+ var min = 60e3, hour = 60*60e3, closed = false
109110
110111 //trigger hard reconnect after suspend or local network changes
111112 onWakeup(gossip.reconnect)
112113 onNetwork(gossip.reconnect)
@@ -139,11 +140,11 @@
139140
140141
141142 var connecting = false
142143 function connections () {
143- if(connecting) return
144+ if(connecting || closed) return
144145 connecting = true
145- setTimeout(function () {
146+ var timer = setTimeout(function () {
146147 connecting = false
147148 var ts = Date.now()
148149 var peers = gossip.peers()
149150
@@ -154,9 +155,9 @@
154155 if(conf('seed', true))
155156 connect(peers, ts, 'seeds', isSeed, {
156157 quota: 3, factor: 2e3, max: 10*min, groupMin: 1e3,
157158 })
158-
159+ return //XXX
159160 if(conf('local', true))
160161 connect(peers, ts, 'local', isLocal, {
161162 quota: 3, factor: 2e3, max: 10*min, groupMin: 1e3,
162163 })
@@ -212,9 +213,9 @@
212213 }
213214 })
214215
215216 }, 100*Math.random())
216-
217+ if(timer.unref) timer.unref()
217218 }
218219
219220 pull(
220221 gossip.changes(),
@@ -228,8 +229,13 @@
228229 if(int.unref) int.unref()
229230
230231 connections()
231232
233+ return function onClose () {
234+ closed = true
235+
236+ }
237+
232238 }
233239
234240 exports.isUnattempted = isUnattempted
235241 exports.isInactive = isInactive

Built with git-ssb-web