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)
|
||||
|
||||
Reference in New Issue
Block a user