Skip to content

Commit 9d6298f

Browse files
committed
Let agents discover newly joined hubs
1 parent fe7569d commit 9d6298f

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed

agent/db.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,50 @@ function setHubs(zone, hubs) {
116116
)
117117
}
118118

119+
function getHub(id) {
120+
return (
121+
db.sql('SELECT zone, info FROM hubs WHERE id = ?')
122+
.bind(1, id)
123+
.exec()
124+
.map(r => {
125+
try {
126+
var hub = JSON.parse(r.info)
127+
} catch {
128+
var hub = {}
129+
}
130+
hub.zone = r.zone
131+
return hub
132+
})[0]
133+
)
134+
}
135+
136+
function setHub(id, hub) {
137+
var old = getHub(id)
138+
if (old) {
139+
var zone = hub.zone || old.zone
140+
var info = {
141+
ports: hub.ports || old.ports,
142+
version: hub.version || old.version,
143+
}
144+
db.sql('UPDATE hubs SET zone = ?, info = ? WHERE id = ?')
145+
.bind(1, zone)
146+
.bind(2, JSON.stringify(info))
147+
.bind(3, id)
148+
.exec()
149+
} else {
150+
var zone = hub.zone
151+
var info = {
152+
ports: hub.ports,
153+
version: hub.version,
154+
}
155+
db.sql('INSERT INTO hubs(id, zone, info) VALUES(?, ?, ?)')
156+
.bind(1, id)
157+
.bind(2, zone)
158+
.bind(3, JSON.stringify(info))
159+
.exec()
160+
}
161+
}
162+
119163
function recordToMesh(rec) {
120164
return {
121165
name: rec.name,
@@ -325,6 +369,8 @@ export default {
325369
setZones,
326370
allHubs,
327371
setHubs,
372+
getHub,
373+
setHub,
328374
allMeshes,
329375
getMesh,
330376
setMesh,

agent/mesh.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,21 @@ export default function (rootDir, listen, config, onConfigUpdate) {
682682
},
683683
},
684684

685+
'/api/hubs/{id}': {
686+
'POST': function ({ id }, req) {
687+
var info = JSON.decode(req.body)
688+
var hub = {
689+
zone: info.zone,
690+
ports: info.ports,
691+
version: info.version,
692+
}
693+
hubCache.set(id, hub)
694+
db.setHub(id, hub)
695+
logInfo(`Discovered new hub ${id} in zone '${hub.zone}' with ports: ${info.ports.join(', ')}`)
696+
return response(201)
697+
},
698+
},
699+
685700
'/api/file-data/{hash}': {
686701
'GET': function ({ hash }) {
687702
var data = fs.raw(hash)

hub/main.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,7 @@ var $hub = null
330330
var $hubSelected = null
331331
var $sendEOS = null
332332
var $sessionID
333+
var $broadcastID
333334
var $pingID
334335
var $pingTime
335336

@@ -405,6 +406,7 @@ function start(listen, bootstrap) {
405406
file: (path, info, ep) => updateFileInfo(path, info, ep, true),
406407
acl: (username, path, access, since) => updateACL(username, path, access, since),
407408
eviction: (username, time) => updateEviction(username, time),
409+
hub: (id, info) => broadcastHub(id, info),
408410
},
409411
log,
410412
})
@@ -1013,6 +1015,25 @@ var muxToAgent = pipeline($=>$
10131015
)
10141016
)
10151017

1018+
var broadcastToAgents = pipeline($=>$
1019+
.onStart(msg => msg)
1020+
.forkJoin(() => Object.keys(sessions)).to($=>$
1021+
.onStart(id => { $broadcastID = id })
1022+
.forkJoin(() => {
1023+
var hubs = []
1024+
sessions[$broadcastID].forEach(h => hubs.push(h))
1025+
return hubs
1026+
}).to($=>$
1027+
.onStart(hub => { $hubSelected = hub })
1028+
.pipe(muxToAgent)
1029+
.replaceData()
1030+
.replaceMessage(new StreamEnd)
1031+
)
1032+
.replaceMessage(new StreamEnd)
1033+
)
1034+
.replaceMessage(new StreamEnd)
1035+
)
1036+
10161037
var connectEndpoint = pipeline($=>$
10171038
.acceptHTTPTunnel(
10181039
function (req) {
@@ -1433,6 +1454,19 @@ function updateEviction(username, time, expiration) {
14331454
return true
14341455
}
14351456

1457+
function broadcastHub(id, info) {
1458+
broadcastToAgents.spawn(
1459+
new Message({
1460+
method: 'POST',
1461+
path: `/api/hubs/${id}`,
1462+
}, JSON.encode({
1463+
zone: info.zone,
1464+
ports: info.ports,
1465+
version: info.version,
1466+
}))
1467+
)
1468+
}
1469+
14361470
function dumpACL() {
14371471
var all = {}
14381472
Object.entries(acl).forEach(

0 commit comments

Comments
 (0)