server: /metrics endpoint + signaling stub for v0.3 WebRTC
- Add Prometheus counters/gauges/histograms: messages_total{source},
message_bytes_total, active_subscribers, publish_duration_seconds.
- Add /api/signal/<room> mailbox endpoint (POST adds, GET drains).
Currently scaffolding for WebRTC SDP/ICE exchange — peers do not
use it yet; client-side WebRTC negotiation is roadmap.
client: structured default label
Use "<GOOS>-sse-<role>" (e.g., windows-sse-listener) instead of the
old "linux-client" fallback. -label still overrides.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
113
server/main.go
113
server/main.go
@@ -1,5 +1,7 @@
|
||||
// tether-server: HTTP+SSE relay for phone↔client clipboard sync.
|
||||
// MVP: single broadcast bus, no auth, no rendezvous, no WebRTC yet.
|
||||
//
|
||||
// v0.1: SSE-only relay with broadcast bus.
|
||||
// v0.2 (this): /metrics endpoint + signaling stubs (mailbox for WebRTC SDP/ICE).
|
||||
package main
|
||||
|
||||
import (
|
||||
@@ -12,6 +14,10 @@ import (
|
||||
"net/http"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/prometheus/client_golang/prometheus/promauto"
|
||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||
)
|
||||
|
||||
//go:embed web/index.html
|
||||
@@ -25,19 +31,16 @@ type Message struct {
|
||||
|
||||
type bus struct {
|
||||
mu sync.Mutex
|
||||
clients map[chan Message]string // chan → label
|
||||
history []Message // last N (for new subscribers)
|
||||
clients map[chan Message]string
|
||||
history []Message
|
||||
}
|
||||
|
||||
func newBus() *bus {
|
||||
return &bus{clients: map[chan Message]string{}}
|
||||
}
|
||||
func newBus() *bus { return &bus{clients: map[chan Message]string{}} }
|
||||
|
||||
func (b *bus) subscribe(label string) chan Message {
|
||||
ch := make(chan Message, 16)
|
||||
b.mu.Lock()
|
||||
b.clients[ch] = label
|
||||
// replay last few so newcomers see something
|
||||
for _, m := range b.history {
|
||||
select {
|
||||
case ch <- m:
|
||||
@@ -45,6 +48,7 @@ func (b *bus) subscribe(label string) chan Message {
|
||||
}
|
||||
}
|
||||
b.mu.Unlock()
|
||||
subscribers.Inc()
|
||||
return ch
|
||||
}
|
||||
|
||||
@@ -53,6 +57,7 @@ func (b *bus) unsubscribe(ch chan Message) {
|
||||
delete(b.clients, ch)
|
||||
b.mu.Unlock()
|
||||
close(ch)
|
||||
subscribers.Dec()
|
||||
}
|
||||
|
||||
func (b *bus) publish(m Message) {
|
||||
@@ -65,23 +70,75 @@ func (b *bus) publish(m Message) {
|
||||
for ch := range b.clients {
|
||||
select {
|
||||
case ch <- m:
|
||||
default: // drop on slow consumer
|
||||
default:
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Prometheus metrics --------------------------------------------------------
|
||||
|
||||
var (
|
||||
messages = promauto.NewCounterVec(prometheus.CounterOpts{
|
||||
Name: "tether_messages_total",
|
||||
Help: "Total messages published to the broadcast bus, by source label.",
|
||||
}, []string{"source"})
|
||||
bytesIn = promauto.NewCounter(prometheus.CounterOpts{
|
||||
Name: "tether_message_bytes_total",
|
||||
Help: "Total bytes of message text published.",
|
||||
})
|
||||
subscribers = promauto.NewGauge(prometheus.GaugeOpts{
|
||||
Name: "tether_active_subscribers",
|
||||
Help: "Number of currently-connected SSE subscribers.",
|
||||
})
|
||||
publishLatency = promauto.NewHistogram(prometheus.HistogramOpts{
|
||||
Name: "tether_publish_duration_seconds",
|
||||
Help: "Latency of the publish() fan-out, including channel sends.",
|
||||
Buckets: prometheus.ExponentialBuckets(0.0001, 4, 8), // 0.1ms..1.6s
|
||||
})
|
||||
)
|
||||
|
||||
// Signaling mailbox (v0.3 WebRTC scaffolding) --------------------------------
|
||||
// Peers POST offers/answers/ICE candidates into a per-room mailbox, peers GET
|
||||
// to drain. Pure relay — no SDP parsing, no peer state on server.
|
||||
|
||||
type signalBox struct {
|
||||
mu sync.Mutex
|
||||
rooms map[string][]json.RawMessage
|
||||
}
|
||||
|
||||
func newSignalBox() *signalBox { return &signalBox{rooms: map[string][]json.RawMessage{}} }
|
||||
|
||||
func (s *signalBox) post(room string, msg json.RawMessage) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
s.rooms[room] = append(s.rooms[room], msg)
|
||||
if len(s.rooms[room]) > 64 {
|
||||
s.rooms[room] = s.rooms[room][len(s.rooms[room])-64:]
|
||||
}
|
||||
}
|
||||
|
||||
func (s *signalBox) drain(room string) []json.RawMessage {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
out := s.rooms[room]
|
||||
delete(s.rooms, room)
|
||||
return out
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
func main() {
|
||||
addr := flag.String("addr", ":8765", "listen address")
|
||||
flag.Parse()
|
||||
|
||||
b := newBus()
|
||||
sig := newSignalBox()
|
||||
|
||||
// Serve embedded HTML
|
||||
sub, _ := fs.Sub(webFS, "web")
|
||||
mux := http.NewServeMux()
|
||||
mux.Handle("/", http.FileServer(http.FS(sub)))
|
||||
mux.Handle("/metrics", promhttp.Handler())
|
||||
|
||||
// POST /api/send — accept a message, broadcast to subscribers
|
||||
mux.HandleFunc("/api/send", func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
http.Error(w, "POST only", http.StatusMethodNotAllowed)
|
||||
@@ -99,12 +156,16 @@ func main() {
|
||||
}
|
||||
}
|
||||
m.TS = time.Now().UnixMilli()
|
||||
|
||||
t0 := time.Now()
|
||||
b.publish(m)
|
||||
publishLatency.Observe(time.Since(t0).Seconds())
|
||||
messages.WithLabelValues(m.Source).Inc()
|
||||
bytesIn.Add(float64(len(m.Text)))
|
||||
log.Printf("publish: %s len=%d", m.Source, len(m.Text))
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
})
|
||||
|
||||
// GET /api/stream — SSE feed of all published messages
|
||||
mux.HandleFunc("/api/stream", func(w http.ResponseWriter, r *http.Request) {
|
||||
fl, ok := w.(http.Flusher)
|
||||
if !ok {
|
||||
@@ -124,7 +185,6 @@ func main() {
|
||||
defer b.unsubscribe(ch)
|
||||
log.Printf("subscribe: %s", label)
|
||||
|
||||
// keepalive
|
||||
ka := time.NewTicker(30 * time.Second)
|
||||
defer ka.Stop()
|
||||
|
||||
@@ -144,6 +204,35 @@ func main() {
|
||||
}
|
||||
})
|
||||
|
||||
// WebRTC signaling: POST to add a message, GET to drain.
|
||||
// /api/signal/<room> is a dumb relay — peers exchange SDP + ICE via this.
|
||||
mux.HandleFunc("/api/signal/", func(w http.ResponseWriter, r *http.Request) {
|
||||
room := r.URL.Path[len("/api/signal/"):]
|
||||
if room == "" {
|
||||
http.Error(w, "missing room", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
switch r.Method {
|
||||
case http.MethodPost:
|
||||
body, _ := func() (json.RawMessage, error) {
|
||||
var raw json.RawMessage
|
||||
err := json.NewDecoder(r.Body).Decode(&raw)
|
||||
return raw, err
|
||||
}()
|
||||
sig.post(room, body)
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
case http.MethodGet:
|
||||
out := sig.drain(room)
|
||||
if out == nil {
|
||||
out = []json.RawMessage{}
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(out)
|
||||
default:
|
||||
http.Error(w, "POST or GET", http.StatusMethodNotAllowed)
|
||||
}
|
||||
})
|
||||
|
||||
mux.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Write([]byte("ok"))
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user