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:
@@ -0,0 +1,87 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
|
||||
"git.stinnesbeck.com/nils/allmende/internal/listing"
|
||||
"git.stinnesbeck.com/nils/allmende/internal/p2p"
|
||||
)
|
||||
|
||||
// regionsFile remembers the regions an owner chose in the browser. Once it
|
||||
// exists it decides which regions the node follows; --region only seeds it.
|
||||
const regionsFile = "regions.json"
|
||||
|
||||
// loadRegions returns the regions saved in dir; ok is false if none were saved.
|
||||
func loadRegions(dir string) (regions []string, ok bool, err error) {
|
||||
data, err := os.ReadFile(filepath.Join(dir, regionsFile))
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
return nil, false, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
var f struct {
|
||||
Regions []string `json:"regions"`
|
||||
}
|
||||
if err := json.Unmarshal(data, &f); err != nil {
|
||||
return nil, false, fmt.Errorf("%s: %w", regionsFile, err)
|
||||
}
|
||||
for _, r := range f.Regions {
|
||||
if !listing.ValidRegion(r) {
|
||||
return nil, false, fmt.Errorf("%s: %q is not a region", regionsFile, r)
|
||||
}
|
||||
}
|
||||
return f.Regions, len(f.Regions) > 0, nil
|
||||
}
|
||||
|
||||
// saveRegions writes the regions so a restart keeps them. The file is replaced
|
||||
// whole, so a crash never leaves half of one.
|
||||
func saveRegions(dir string, regions []string) error {
|
||||
data, err := json.MarshalIndent(map[string][]string{"regions": regions}, "", " ")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
tmp, err := os.CreateTemp(dir, regionsFile+".*")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer os.Remove(tmp.Name())
|
||||
if _, err := tmp.Write(data); err != nil {
|
||||
tmp.Close()
|
||||
return err
|
||||
}
|
||||
if err := tmp.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
return os.Rename(tmp.Name(), filepath.Join(dir, regionsFile))
|
||||
}
|
||||
|
||||
// regionKeeper applies a change of regions to the running node and saves it.
|
||||
type regionKeeper struct {
|
||||
node *p2p.Node
|
||||
dir string
|
||||
mu sync.Mutex // one change at a time
|
||||
}
|
||||
|
||||
func (k *regionKeeper) Regions() []string { return k.node.Regions() }
|
||||
|
||||
func (k *regionKeeper) SetRegions(regions []string) error {
|
||||
k.mu.Lock()
|
||||
defer k.mu.Unlock()
|
||||
before := k.node.Regions()
|
||||
if err := k.node.SetRegions(regions); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := saveRegions(k.dir, regions); err != nil {
|
||||
// Not remembered means the change would silently vanish at the next
|
||||
// start: undo it instead.
|
||||
k.node.SetRegions(before)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestRegionsAreSavedAndLoaded(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
if _, ok, err := loadRegions(dir); ok || err != nil {
|
||||
t.Fatalf("nothing saved yet: ok=%v err=%v", ok, err)
|
||||
}
|
||||
want := []string{"u1j", "u1x"}
|
||||
if err := saveRegions(dir, want); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, ok, err := loadRegions(dir)
|
||||
if err != nil || !ok || !reflect.DeepEqual(got, want) {
|
||||
t.Fatalf("loaded %v ok=%v err=%v, want %v", got, ok, err, want)
|
||||
}
|
||||
// Saving again replaces the file whole and leaves no temporary files.
|
||||
if err := saveRegions(dir, []string{"u30"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got, _, _ := loadRegions(dir); !reflect.DeepEqual(got, []string{"u30"}) {
|
||||
t.Fatalf("after replacing: %v", got)
|
||||
}
|
||||
entries, _ := os.ReadDir(dir)
|
||||
if len(entries) != 1 {
|
||||
t.Fatalf("stray files: %v", entries)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBrokenRegionFilesAreReportedNotIgnored(t *testing.T) {
|
||||
for name, content := range map[string]string{
|
||||
"not json": "u1j",
|
||||
"not a region": `{"regions":["u1j","hello"]}`,
|
||||
"empty regions": `{"regions":[]}`,
|
||||
} {
|
||||
dir := t.TempDir()
|
||||
if err := os.WriteFile(filepath.Join(dir, regionsFile), []byte(content), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, ok, err := loadRegions(dir)
|
||||
switch name {
|
||||
case "empty regions":
|
||||
if ok || err != nil {
|
||||
t.Errorf("%s: ok=%v err=%v, want it treated as not set", name, ok, err)
|
||||
}
|
||||
default:
|
||||
if err == nil {
|
||||
t.Errorf("%s: no error, a broken file must not silently change what the node follows", name)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
"os"
|
||||
"os/signal"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
@@ -52,6 +53,19 @@ func cmdRun(args []string) error {
|
||||
"how many new conversations each of your listings can start per day")
|
||||
fs.Parse(args)
|
||||
|
||||
// Regions the owner chose in the browser win over --region, which then only
|
||||
// says where a node starts the first time.
|
||||
saved, ok, err := loadRegions(*data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if ok {
|
||||
if len(regions) > 0 && !slices.Equal(regions, saved) {
|
||||
slog.Info("using the regions chosen in the browser, not --region; delete "+regionsFile+" to use --region again",
|
||||
"regions", saved)
|
||||
}
|
||||
regions = saved
|
||||
}
|
||||
// A bootstrap node may follow no region: it then only helps others connect.
|
||||
if len(regions) == 0 && !*public {
|
||||
return errors.New("choose at least one region to replicate, e.g. --region u1x (a region is a 3-character geohash, about 150 km wide)")
|
||||
@@ -120,6 +134,7 @@ func cmdRun(args []string) error {
|
||||
Store: st, Node: node, Device: dev, Regions: regions, Token: token, Difficulty: *difficulty,
|
||||
MessageDifficulty: *msgDifficulty,
|
||||
Inbox: in,
|
||||
RegionKeeper: ®ionKeeper{node: node, dir: *data},
|
||||
})
|
||||
|
||||
ln, err := net.Listen("tcp", *httpAddr)
|
||||
|
||||
Reference in New Issue
Block a user