refactor(M8): drop the control /events SSE stream — no consumer yet (ponytail) ✂️
ponytail-review caught it: publishActivity -> ctrl.Publish -> broker -> SSE was a producer with nothing on the other end. GET /events only feeds the M11 TUI, which is demand-driven and not built. /state + /pause + /resume cover every real caller (devbox status/pause/resume). Cut the broker, handleEvents, Publish, publishActivity, and the ctrl handle — re-add the (small, known) fan-out when a TUI actually consumes it. Net -100 lines.
This commit is contained in:
parent
4734d674b0
commit
9f90f2c147
2 changed files with 11 additions and 113 deletions
|
|
@ -5,11 +5,9 @@ package control
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"sync"
|
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -26,7 +24,6 @@ type Daemon interface {
|
||||||
// build one with Serve.
|
// build one with Serve.
|
||||||
type Server struct {
|
type Server struct {
|
||||||
d Daemon
|
d Daemon
|
||||||
broker *broker
|
|
||||||
ln net.Listener
|
ln net.Listener
|
||||||
srv *http.Server
|
srv *http.Server
|
||||||
sock string
|
sock string
|
||||||
|
|
@ -37,6 +34,11 @@ type Server struct {
|
||||||
// returns once the listener is bound; the serving runs in the background. A bind
|
// returns once the listener is bound; the serving runs in the background. A bind
|
||||||
// failure is returned so the caller can warn-and-continue — sync must not depend
|
// failure is returned so the caller can warn-and-continue — sync must not depend
|
||||||
// on the control plane.
|
// on the control plane.
|
||||||
|
//
|
||||||
|
// ponytail: GET /events (an SSE activity stream) is intentionally NOT served — it
|
||||||
|
// only feeds the M11 TUI, which doesn't exist yet. Add the route + a fan-out
|
||||||
|
// broker when a real consumer lands; today /state + /pause + /resume cover every
|
||||||
|
// caller (devbox status/pause/resume).
|
||||||
func Serve(ctx context.Context, dir string, d Daemon, logf func(string, ...any)) (*Server, error) {
|
func Serve(ctx context.Context, dir string, d Daemon, logf func(string, ...any)) (*Server, error) {
|
||||||
sock := SockPath(dir)
|
sock := SockPath(dir)
|
||||||
if err := os.MkdirAll(dir, 0o700); err != nil {
|
if err := os.MkdirAll(dir, 0o700); err != nil {
|
||||||
|
|
@ -58,10 +60,9 @@ func Serve(ctx context.Context, dir string, d Daemon, logf func(string, ...any))
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
s := &Server{d: d, broker: newBroker(), ln: ln, sock: sock}
|
s := &Server{d: d, ln: ln, sock: sock}
|
||||||
mux := http.NewServeMux()
|
mux := http.NewServeMux()
|
||||||
mux.HandleFunc("GET /state", s.handleState)
|
mux.HandleFunc("GET /state", s.handleState)
|
||||||
mux.HandleFunc("GET /events", s.handleEvents)
|
|
||||||
mux.HandleFunc("POST /pause", s.handlePause)
|
mux.HandleFunc("POST /pause", s.handlePause)
|
||||||
mux.HandleFunc("POST /resume", s.handleResume)
|
mux.HandleFunc("POST /resume", s.handleResume)
|
||||||
s.srv = &http.Server{Handler: mux}
|
s.srv = &http.Server{Handler: mux}
|
||||||
|
|
@ -78,15 +79,6 @@ func Serve(ctx context.Context, dir string, d Daemon, logf func(string, ...any))
|
||||||
return s, nil
|
return s, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Publish reports that a mount finished a sync; the server fans it out to any
|
|
||||||
// GET /events subscribers. Safe to call from the sync goroutines.
|
|
||||||
func (s *Server) Publish(share, detail string) {
|
|
||||||
if s == nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
s.broker.publish(activity{Share: share, Detail: detail, At: time.Now().Unix()})
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Server) close() {
|
func (s *Server) close() {
|
||||||
shutCtx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
shutCtx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
@ -108,84 +100,3 @@ func (s *Server) handleResume(w http.ResponseWriter, _ *http.Request) {
|
||||||
s.d.Resume()
|
s.d.Resume()
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
}
|
}
|
||||||
|
|
||||||
// handleEvents streams sync activity as Server-Sent Events, mirroring the hub's
|
|
||||||
// SSE wire style (`data: <json>\n\n`, periodic `: ping` comments to keep idle
|
|
||||||
// proxies open). ponytail: copied, not imported — internal/hub is off-limits.
|
|
||||||
func (s *Server) handleEvents(w http.ResponseWriter, r *http.Request) {
|
|
||||||
flusher, ok := w.(http.Flusher)
|
|
||||||
if !ok {
|
|
||||||
http.Error(w, "streaming unsupported", http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
ch := s.broker.sub()
|
|
||||||
defer s.broker.unsub(ch)
|
|
||||||
|
|
||||||
h := w.Header()
|
|
||||||
h.Set("Content-Type", "text/event-stream")
|
|
||||||
h.Set("Cache-Control", "no-cache")
|
|
||||||
h.Set("Connection", "keep-alive")
|
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
flusher.Flush()
|
|
||||||
|
|
||||||
ping := time.NewTicker(25 * time.Second)
|
|
||||||
defer ping.Stop()
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case <-r.Context().Done():
|
|
||||||
return
|
|
||||||
case <-ping.C:
|
|
||||||
fmt.Fprint(w, ": ping\n\n")
|
|
||||||
flusher.Flush()
|
|
||||||
case ev := <-ch:
|
|
||||||
b, _ := json.Marshal(ev)
|
|
||||||
fmt.Fprintf(w, "data: %s\n\n", b)
|
|
||||||
flusher.Flush()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// activity is one line of sync activity emitted on GET /events.
|
|
||||||
type activity struct {
|
|
||||||
Share string `json:"share"`
|
|
||||||
Detail string `json:"detail"`
|
|
||||||
At int64 `json:"at"` // unix seconds
|
|
||||||
}
|
|
||||||
|
|
||||||
// broker fans sync activity out to connected SSE subscribers. A slow subscriber
|
|
||||||
// is dropped rather than blocking the sync path — same trade-off as the hub's
|
|
||||||
// broker. ponytail: pattern copied from internal/hub/events.go.
|
|
||||||
type broker struct {
|
|
||||||
mu sync.Mutex
|
|
||||||
subs map[chan activity]struct{}
|
|
||||||
}
|
|
||||||
|
|
||||||
func newBroker() *broker { return &broker{subs: map[chan activity]struct{}{}} }
|
|
||||||
|
|
||||||
func (b *broker) sub() chan activity {
|
|
||||||
ch := make(chan activity, 16)
|
|
||||||
b.mu.Lock()
|
|
||||||
b.subs[ch] = struct{}{}
|
|
||||||
b.mu.Unlock()
|
|
||||||
return ch
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *broker) unsub(ch chan activity) {
|
|
||||||
b.mu.Lock()
|
|
||||||
if _, ok := b.subs[ch]; ok {
|
|
||||||
delete(b.subs, ch)
|
|
||||||
close(ch)
|
|
||||||
}
|
|
||||||
b.mu.Unlock()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *broker) publish(ev activity) {
|
|
||||||
b.mu.Lock()
|
|
||||||
defer b.mu.Unlock()
|
|
||||||
for ch := range b.subs {
|
|
||||||
select {
|
|
||||||
case ch <- ev:
|
|
||||||
default: // slow subscriber; drop
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,6 @@ package daemon
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
|
||||||
"log"
|
"log"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
@ -46,7 +45,6 @@ type Daemon struct {
|
||||||
state map[string]string // mountKey -> last-applied snapshot
|
state map[string]string // mountKey -> last-applied snapshot
|
||||||
nudges map[string]func() // mountKey -> trigger a catch-up sync (for Resume)
|
nudges map[string]func() // mountKey -> trigger a catch-up sync (for Resume)
|
||||||
mounts map[string]config.Mount // mountKey -> mount config (for StateSnapshot)
|
mounts map[string]config.Mount // mountKey -> mount config (for StateSnapshot)
|
||||||
ctrl *control.Server // nil until Run starts it (and on Windows)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// New builds a daemon from a loaded config. logf may be nil (defaults to log.Printf).
|
// New builds a daemon from a loaded config. logf may be nil (defaults to log.Printf).
|
||||||
|
|
@ -80,10 +78,8 @@ func (d *Daemon) Run(ctx context.Context) error {
|
||||||
// Start the control plane before mounts so `devbox status`/`pause` can reach
|
// Start the control plane before mounts so `devbox status`/`pause` can reach
|
||||||
// the daemon immediately. A bind failure is non-fatal: sync must not depend on
|
// the daemon immediately. A bind failure is non-fatal: sync must not depend on
|
||||||
// the control socket. Serve stops itself on ctx cancel.
|
// the control socket. Serve stops itself on ctx cancel.
|
||||||
if srv, err := control.Serve(ctx, d.dir, d, d.logf); err != nil {
|
if _, err := control.Serve(ctx, d.dir, d, d.logf); err != nil {
|
||||||
d.logf("control socket unavailable (introspection/pause disabled): %v", err)
|
d.logf("control socket unavailable (introspection/pause disabled): %v", err)
|
||||||
} else {
|
|
||||||
d.ctrl = srv
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
|
|
@ -303,7 +299,6 @@ func (d *Daemon) syncMount(c *transport.Client, m config.Mount) {
|
||||||
}
|
}
|
||||||
d.setBase(m, pr.Base)
|
d.setBase(m, pr.Base)
|
||||||
d.report(m, pr)
|
d.report(m, pr)
|
||||||
d.publishActivity(m, pr)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -314,14 +309,6 @@ func (d *Daemon) syncMount(c *transport.Client, m config.Mount) {
|
||||||
}
|
}
|
||||||
d.setBase(m, newBase)
|
d.setBase(m, newBase)
|
||||||
d.report(m, pr)
|
d.report(m, pr)
|
||||||
d.publishActivity(m, pr)
|
|
||||||
}
|
|
||||||
|
|
||||||
// publishActivity feeds a one-line sync summary to the control plane's
|
|
||||||
// GET /events stream (no-op when the control server didn't start).
|
|
||||||
func (d *Daemon) publishActivity(m config.Mount, pr syncer.PullResult) {
|
|
||||||
d.ctrl.Publish(m.Share, fmt.Sprintf("synced: %d written, %d deleted, %d conflicts, %d skipped",
|
|
||||||
len(pr.Written), len(pr.Deleted), len(pr.Conflicts), len(pr.Skipped)))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Daemon) report(m config.Mount, pr syncer.PullResult) {
|
func (d *Daemon) report(m config.Mount, pr syncer.PullResult) {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue