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:
@@ -53,6 +53,10 @@ func (n *Node) Handler() http.Handler {
|
||||
mux.HandleFunc("GET /v1/log", n.handleLog)
|
||||
mux.HandleFunc("POST /v1/obs/batch", n.handleBatch)
|
||||
mux.HandleFunc("GET /v1/diff", n.handleDiff)
|
||||
mux.HandleFunc("DELETE /v1/peers/{id}", n.handleDeletePeer)
|
||||
// Addresses contain slashes, so they travel as ?addr= rather than a path
|
||||
// segment.
|
||||
mux.HandleFunc("DELETE /v1/peers", n.handleDeletePeer)
|
||||
return mux
|
||||
}
|
||||
|
||||
@@ -183,6 +187,31 @@ func (n *Node) handleDiff(w http.ResponseWriter, r *http.Request) {
|
||||
})
|
||||
}
|
||||
|
||||
// handleDeletePeer forgets a peer, either by id in the path or by ?addr= for
|
||||
// addresses that contain slashes. Deletion is not permanent against a static
|
||||
// seed: KNOX_PEERS is re-merged on the next sweep, so a seeded peer must also
|
||||
// be removed from the environment.
|
||||
func (n *Node) handleDeletePeer(w http.ResponseWriter, r *http.Request) {
|
||||
key := r.PathValue("id")
|
||||
if addr := r.URL.Query().Get("addr"); addr != "" {
|
||||
key = addr
|
||||
}
|
||||
if key == "" {
|
||||
http.Error(w, "peer id or address required", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
deleted, err := n.Kdb.DeletePeer(key)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if deleted == 0 {
|
||||
http.Error(w, "no such peer: "+key, http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
writeJSON(w, map[string]any{"deleted": deleted})
|
||||
}
|
||||
|
||||
func writeJSON(w http.ResponseWriter, v any) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
enc := json.NewEncoder(w)
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
@@ -363,3 +364,47 @@ func TestClientPushSurfacesHTTPError(t *testing.T) {
|
||||
t.Fatal("expected push error on HTTP 500, got nil")
|
||||
}
|
||||
}
|
||||
|
||||
// TestHandleDeletePeer: the peer list is otherwise append-only, so there must be
|
||||
// a way to forget a node that moved or was decommissioned. Both addressing
|
||||
// forms are accepted, and an unknown peer is a 404 rather than a silent success.
|
||||
func TestHandleDeletePeer(t *testing.T) {
|
||||
b := tmpKnoxDB(t)
|
||||
node := &Node{Kdb: b, Name: "A"}
|
||||
sv := httptest.NewServer(node.Handler())
|
||||
defer sv.Close()
|
||||
|
||||
if err := b.MergePeer("0123456789abcdef0123456789abcdef", "http://10.0.0.9:8931", "old"); err != nil {
|
||||
t.Fatalf("merge: %v", err)
|
||||
}
|
||||
|
||||
del := func(path string) int {
|
||||
req, _ := http.NewRequest(http.MethodDelete, sv.URL+path, nil)
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
t.Fatalf("delete %s: %v", path, err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
return resp.StatusCode
|
||||
}
|
||||
|
||||
if code := del("/v1/peers/unknown-id"); code != http.StatusNotFound {
|
||||
t.Errorf("unknown peer: got %d, want 404", code)
|
||||
}
|
||||
if code := del("/v1/peers/0123456789abcdef0123456789abcdef"); code != http.StatusOK {
|
||||
t.Errorf("by id: got %d, want 200", code)
|
||||
}
|
||||
if peers, _ := b.ListPeers(); len(peers) != 0 {
|
||||
t.Fatalf("peer not deleted: %+v", peers)
|
||||
}
|
||||
|
||||
if err := b.MergePeer("ffff", "http://10.0.0.10:8931", "old2"); err != nil {
|
||||
t.Fatalf("merge2: %v", err)
|
||||
}
|
||||
if code := del("/v1/peers?addr=" + url.QueryEscape("http://10.0.0.10:8931")); code != http.StatusOK {
|
||||
t.Errorf("by addr: got %d, want 200", code)
|
||||
}
|
||||
if peers, _ := b.ListPeers(); len(peers) != 0 {
|
||||
t.Fatalf("peer not deleted by addr: %+v", peers)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user