2026-10-13 22:19:20 -07:00
|
|
|
package server
|
|
|
|
|
|
|
|
|
|
import (
|
2026-10-17 15:32:35 -07:00
|
|
|
"sort"
|
2026-10-13 22:19:20 -07:00
|
|
|
"sync"
|
2026-10-17 23:24:26 -07:00
|
|
|
"time"
|
2026-10-13 22:19:20 -07:00
|
|
|
|
|
|
|
|
"g.fc.run/misc/byre/internal/api"
|
|
|
|
|
)
|
|
|
|
|
|
2026-10-17 23:24:26 -07:00
|
|
|
type clientKey struct{ login, host, commit string }
|
|
|
|
|
|
2026-10-13 22:19:20 -07:00
|
|
|
type hub struct {
|
2026-10-17 23:24:26 -07:00
|
|
|
mu sync.Mutex
|
|
|
|
|
subs map[chan api.Event]api.ConnectedClient
|
|
|
|
|
lastAction map[clientKey]time.Time
|
|
|
|
|
lastNotified map[clientKey]time.Time
|
2026-10-13 22:19:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func newHub() *hub {
|
2026-10-17 23:24:26 -07:00
|
|
|
return &hub{subs: map[chan api.Event]api.ConnectedClient{}, lastAction: map[clientKey]time.Time{}, lastNotified: map[clientKey]time.Time{}}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (h *hub) acted(login, host, commit string) bool {
|
|
|
|
|
h.mu.Lock()
|
|
|
|
|
defer h.mu.Unlock()
|
|
|
|
|
k := clientKey{login, host, commit}
|
|
|
|
|
now := time.Now()
|
|
|
|
|
h.lastAction[k] = now
|
|
|
|
|
prev, ok := h.lastNotified[k]
|
|
|
|
|
if ok && now.Sub(prev) < time.Second {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
h.lastNotified[k] = now
|
|
|
|
|
return true
|
2026-10-13 22:19:20 -07:00
|
|
|
}
|
|
|
|
|
|
2026-10-17 15:32:35 -07:00
|
|
|
func (h *hub) subscribe(client api.ConnectedClient) chan api.Event {
|
2026-10-13 22:19:20 -07:00
|
|
|
h.mu.Lock()
|
|
|
|
|
defer h.mu.Unlock()
|
|
|
|
|
ch := make(chan api.Event, 256)
|
2026-10-17 15:32:35 -07:00
|
|
|
h.subs[ch] = client
|
2026-10-13 22:19:20 -07:00
|
|
|
return ch
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (h *hub) unsubscribe(ch chan api.Event) {
|
|
|
|
|
h.mu.Lock()
|
|
|
|
|
defer h.mu.Unlock()
|
2026-10-17 15:32:35 -07:00
|
|
|
_, ok := h.subs[ch]
|
|
|
|
|
if !ok {
|
2026-10-13 22:19:20 -07:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
delete(h.subs, ch)
|
|
|
|
|
close(ch)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (h *hub) publish(ev api.Event) {
|
|
|
|
|
h.mu.Lock()
|
|
|
|
|
defer h.mu.Unlock()
|
|
|
|
|
for ch := range h.subs {
|
|
|
|
|
select {
|
|
|
|
|
case ch <- ev:
|
|
|
|
|
default:
|
|
|
|
|
delete(h.subs, ch)
|
|
|
|
|
close(ch)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (h *hub) count() int {
|
|
|
|
|
h.mu.Lock()
|
|
|
|
|
defer h.mu.Unlock()
|
|
|
|
|
return len(h.subs)
|
|
|
|
|
}
|
2026-10-17 15:32:35 -07:00
|
|
|
|
|
|
|
|
func (h *hub) clients() []api.ConnectedClient {
|
|
|
|
|
h.mu.Lock()
|
|
|
|
|
defer h.mu.Unlock()
|
2026-10-17 23:24:26 -07:00
|
|
|
oldest := map[clientKey]api.ConnectedClient{}
|
2026-10-17 15:32:35 -07:00
|
|
|
for _, c := range h.subs {
|
2026-10-17 23:24:26 -07:00
|
|
|
k := clientKey{c.Login, c.Host, c.Commit}
|
2026-10-17 15:32:35 -07:00
|
|
|
prev, ok := oldest[k]
|
|
|
|
|
if ok && !c.ConnectedAt.Before(prev.ConnectedAt) {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
oldest[k] = c
|
|
|
|
|
}
|
|
|
|
|
out := []api.ConnectedClient{}
|
2026-10-17 23:24:26 -07:00
|
|
|
for k, c := range oldest {
|
|
|
|
|
at, ok := h.lastAction[k]
|
|
|
|
|
if ok {
|
|
|
|
|
c.LastActionAt = &at
|
|
|
|
|
}
|
2026-10-17 15:32:35 -07:00
|
|
|
out = append(out, c)
|
|
|
|
|
}
|
|
|
|
|
sort.Slice(out, func(i, j int) bool { return out[i].ConnectedAt.Before(out[j].ConnectedAt) })
|
|
|
|
|
return out
|
|
|
|
|
}
|