feat: remove peers from the CLI and the gossip API (#10)
Peers were only ever merged, so a retired node stayed in the table forever and
failed on every sweep — the laptop node retried a dead LAN peer every minute for
weeks, fixable only by editing SQLite.
Adds DeletePeer (by peer_id or exact address), DELETE /v1/peers/{id} and
DELETE /v1/peers?addr= behind the existing token middleware, and
`knox gossip peer list|rm`. The CLI warns when the removed address is still in
KNOX_PEERS, since a static seed is re-learned on the next sweep.
This commit is contained in:
@@ -54,6 +54,54 @@ func NewGossipCmd(kdb *db.KnoxDB) *cobra.Command {
|
||||
return gossipDiff(kdb, args[0])
|
||||
},
|
||||
},
|
||||
&cobra.Command{
|
||||
Use: "peer <list|rm>",
|
||||
Short: "List or forget known peers",
|
||||
Args: cobra.RangeArgs(0, 2),
|
||||
RunE: func(c *cobra.Command, args []string) error {
|
||||
action := "list"
|
||||
if len(args) > 0 {
|
||||
action = args[0]
|
||||
}
|
||||
switch action {
|
||||
case "list":
|
||||
peers, err := kdb.ListPeers()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(peers) == 0 {
|
||||
fmt.Println("no peers")
|
||||
return nil
|
||||
}
|
||||
for _, p := range peers {
|
||||
fmt.Printf("%s %-30s last=%s cursor=%d\n", p.PeerID, p.Addr, p.LastHandshake, p.Cursor)
|
||||
}
|
||||
return nil
|
||||
case "rm":
|
||||
if len(args) != 2 {
|
||||
return fmt.Errorf("usage: knox gossip peer rm <peer-id|address>")
|
||||
}
|
||||
deleted, err := kdb.DeletePeer(args[1])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if deleted == 0 {
|
||||
return fmt.Errorf("no peer matching %q", args[1])
|
||||
}
|
||||
fmt.Printf("forgot %s\n", args[1])
|
||||
// A static seed would simply be re-learned on the next
|
||||
// sweep, so say so rather than let the operator wonder.
|
||||
for _, seeded := range watch.PeerAddrs() {
|
||||
if seeded == args[1] {
|
||||
fmt.Printf("warning: %s is still in KNOX_PEERS and will be re-learned\n", args[1])
|
||||
}
|
||||
}
|
||||
return nil
|
||||
default:
|
||||
return fmt.Errorf("unknown peer action %q (use list or rm)", action)
|
||||
}
|
||||
},
|
||||
},
|
||||
&cobra.Command{
|
||||
Use: "sync",
|
||||
Short: "Run one anti-entropy sweep against configured peers",
|
||||
|
||||
Reference in New Issue
Block a user