Let the owner change regions by place name, without a restart
The p2p node can now add and drop regions while running (SetRegions): topics, validators, subscriptions and per-region peer discovery start and stop with them, and a newly followed region catches up by sync. PUT /api/v1/regions (owner token) applies a change and the node saves it in regions.json, which wins over --region from then on. The owner's browser gets a "Regions" page: search a place by name (GeoNames cities, offline, CC BY 4.0), follow its cell plus any cell within about 40 km. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AeFqV96dVeBX94inzcgZKw
This commit is contained in:
+31
-15
@@ -41,15 +41,28 @@ func (n *Node) startDiscovery() error {
|
||||
}
|
||||
n.dht = d
|
||||
rd := drouting.NewRoutingDiscovery(d)
|
||||
for _, r := range n.cfg.Regions {
|
||||
ns := regionNamespace + r
|
||||
n.wg.Add(2)
|
||||
go n.advertise(rd, ns)
|
||||
go n.findPeers(rd, r, ns)
|
||||
n.mu.Lock()
|
||||
n.rd = rd
|
||||
runs := make(map[string]*regionRun, len(n.regions))
|
||||
for r, run := range n.regions {
|
||||
runs[r] = run
|
||||
}
|
||||
n.mu.Unlock()
|
||||
for r, run := range runs {
|
||||
n.discoverRegion(run.ctx, rd, r)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// discoverRegion advertises this node under the region and looks for others,
|
||||
// until ctx ends (the region is dropped or the node stops).
|
||||
func (n *Node) discoverRegion(ctx context.Context, rd *drouting.RoutingDiscovery, region string) {
|
||||
ns := regionNamespace + region
|
||||
n.wg.Add(2)
|
||||
go n.advertise(ctx, rd, ns)
|
||||
go n.findPeers(ctx, rd, region, ns)
|
||||
}
|
||||
|
||||
func (n *Node) discoverInterval() time.Duration {
|
||||
if n.cfg.DiscoverInterval > 0 {
|
||||
return n.cfg.DiscoverInterval
|
||||
@@ -58,11 +71,14 @@ func (n *Node) discoverInterval() time.Duration {
|
||||
}
|
||||
|
||||
// sleep waits for d or until the node stops; false means it stopped.
|
||||
func (n *Node) sleep(d time.Duration) bool {
|
||||
func (n *Node) sleep(d time.Duration) bool { return sleepCtx(n.ctx, d) }
|
||||
|
||||
// sleepCtx waits for d or until ctx ends; false means it ended.
|
||||
func sleepCtx(ctx context.Context, d time.Duration) bool {
|
||||
t := time.NewTimer(d)
|
||||
defer t.Stop()
|
||||
select {
|
||||
case <-n.ctx.Done():
|
||||
case <-ctx.Done():
|
||||
return false
|
||||
case <-t.C:
|
||||
return true
|
||||
@@ -71,16 +87,16 @@ func (n *Node) sleep(d time.Duration) bool {
|
||||
|
||||
// advertise keeps this node findable under the region's name. Until the
|
||||
// routing table has a peer in it there is nowhere to publish, so it retries.
|
||||
func (n *Node) advertise(rd *drouting.RoutingDiscovery, ns string) {
|
||||
func (n *Node) advertise(rctx context.Context, rd *drouting.RoutingDiscovery, ns string) {
|
||||
defer n.wg.Done()
|
||||
back := n.backoff()
|
||||
for {
|
||||
wait := back.next()
|
||||
ctx, cancel := context.WithTimeout(n.ctx, 30*time.Second)
|
||||
ctx, cancel := context.WithTimeout(rctx, 30*time.Second)
|
||||
ttl, err := rd.Advertise(ctx, ns)
|
||||
cancel()
|
||||
switch {
|
||||
case n.ctx.Err() != nil:
|
||||
case rctx.Err() != nil:
|
||||
return
|
||||
case err != nil:
|
||||
n.log.Debug("advertise", "ns", ns, "err", err) // retried soon, then less often
|
||||
@@ -89,7 +105,7 @@ func (n *Node) advertise(rd *drouting.RoutingDiscovery, ns string) {
|
||||
wait = ttl * 3 / 4 // refresh before the record lapses
|
||||
back.reset()
|
||||
}
|
||||
if !n.sleep(wait) {
|
||||
if !sleepCtx(rctx, wait) {
|
||||
return
|
||||
}
|
||||
}
|
||||
@@ -120,22 +136,22 @@ func (b *backoff) bump() {
|
||||
}
|
||||
|
||||
// findPeers connects to other nodes of the region that the DHT knows.
|
||||
func (n *Node) findPeers(rd *drouting.RoutingDiscovery, region, ns string) {
|
||||
func (n *Node) findPeers(rctx context.Context, rd *drouting.RoutingDiscovery, region, ns string) {
|
||||
defer n.wg.Done()
|
||||
back := n.backoff()
|
||||
for {
|
||||
if !n.sleep(back.next()) {
|
||||
if !sleepCtx(rctx, back.next()) {
|
||||
return
|
||||
}
|
||||
back.bump()
|
||||
if len(n.RegionPeers(region)) >= enoughRegionPeers {
|
||||
continue
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(n.ctx, 30*time.Second)
|
||||
ctx, cancel := context.WithTimeout(rctx, 30*time.Second)
|
||||
found, err := rd.FindPeers(ctx, ns, discovery.Limit(findLimit))
|
||||
if err != nil {
|
||||
cancel()
|
||||
if n.ctx.Err() == nil {
|
||||
if rctx.Err() == nil {
|
||||
n.log.Debug("find peers", "ns", ns, "err", err)
|
||||
}
|
||||
continue
|
||||
|
||||
Reference in New Issue
Block a user