devbox/internal/hub/events.go
shoemoney 03469958d3
Some checks are pending
CI / vet-build-test (push) Waiting to run
chore: prep for public release — MIT license + github.com module path 🌍⚖️
- LICENSE: MIT (enables the adoption push; public repo was all-rights-reserved).
- go.mod + all imports: git.shoemoney.ai/shoemoney/devbox -> github.com/shoemoney/devbox,
  so outsiders can 'go install github.com/shoemoney/devbox/cmd/devbox@latest'. Forgejo
  stays the self-hosted home; the registry (devbox-hub) + public dist (devbox-dist) are
  unchanged. vet + build + go test -race all green after the rename.
- README: document the go install path.

Security: full git-history scan (manual blob sweep + gitleaks, 84 commits) found
ZERO secrets — Forgejo token never committed, no keys/passwords/private keys.
2026-06-23 19:19:09 -05:00

89 lines
2.1 KiB
Go

package hub
import (
"encoding/json"
"fmt"
"net/http"
"sync"
"time"
"github.com/shoemoney/devbox/pkg/proto"
)
// broker fans hub change events out to connected SSE subscribers. A slow
// subscriber's events are dropped rather than blocking the push path — the
// device re-syncs via Head() regardless, so a missed nudge is harmless.
type broker struct {
mu sync.Mutex
subs map[chan proto.Event]string // channel -> share filter ("" = all shares)
}
func newBroker() *broker { return &broker{subs: map[chan proto.Event]string{}} }
func (b *broker) sub(share string) chan proto.Event {
ch := make(chan proto.Event, 16)
b.mu.Lock()
b.subs[ch] = share
b.mu.Unlock()
return ch
}
func (b *broker) unsub(ch chan proto.Event) {
b.mu.Lock()
if _, ok := b.subs[ch]; ok {
delete(b.subs, ch)
close(ch)
}
b.mu.Unlock()
}
// publish delivers ev to every subscriber whose filter matches. Held under the
// same lock as unsub, so a channel is never sent-to after it is closed.
func (b *broker) publish(ev proto.Event) {
b.mu.Lock()
defer b.mu.Unlock()
for ch, filter := range b.subs {
if filter != "" && filter != ev.Share {
continue
}
select {
case ch <- ev:
default: // subscriber is slow; drop (it will re-sync via Head())
}
}
}
// handleEvents streams change events to a device as Server-Sent Events.
func (s *Server) handleEvents(w http.ResponseWriter, r *http.Request, _ string) {
flusher, ok := w.(http.Flusher)
if !ok {
writeErr(w, http.StatusInternalServerError, "streaming unsupported")
return
}
ch := s.broker.sub(r.URL.Query().Get("share"))
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") // comment line keeps idle proxies from closing us
flusher.Flush()
case ev := <-ch:
b, _ := json.Marshal(ev)
fmt.Fprintf(w, "data: %s\n\n", b)
flusher.Flush()
}
}
}