| 1 | package chat |
| 2 | |
| 3 | import ( |
| 4 | "sync" |
| 5 | |
| 6 | "amadan.net/oficina/equipo/internal/wire" |
| 7 | ) |
| 8 | |
| 9 | // Hub wakes streams in this process after a commit. Other processes (an |
| 10 | // overlapping deploy) are covered by each stream's one-second poll. |
| 11 | type Hub struct { |
| 12 | mu sync.Mutex |
| 13 | waiters map[string]chan struct{} |
| 14 | runners map[string]registration |
| 15 | next int64 |
| 16 | closing chan struct{} |
| 17 | closed bool |
| 18 | // live are the streams open in this process, for events that are |
| 19 | // never stored (a glance): sent to whoever is following now, and |
| 20 | // missed by anyone who is not. |
| 21 | live map[*liveSub]bool |
| 22 | // streams counts each person's open /events streams in this process |
| 23 | // (Streaming): a reply pushes only to someone not following. |
| 24 | streams map[string]int |
| 25 | } |
| 26 | |
| 27 | type liveSub struct { |
| 28 | person, lane string |
| 29 | ch chan wire.Event |
| 30 | } |
| 31 | |
| 32 | func NewHub() *Hub { |
| 33 | return &Hub{waiters: map[string]chan struct{}{}, runners: map[string]registration{}, closing: make(chan struct{}), live: map[*liveSub]bool{}, streams: map[string]int{}} |
| 34 | } |
| 35 | |
| 36 | // Live follows the person's live-only events in one lane, until stop. |
| 37 | // A stream that falls behind loses them; they are never replayed. |
| 38 | func (h *Hub) Live(person, lane string) (events <-chan wire.Event, stop func()) { |
| 39 | sub := &liveSub{person: person, lane: lane, ch: make(chan wire.Event, 16)} |
| 40 | h.mu.Lock() |
| 41 | h.live[sub] = true |
| 42 | h.mu.Unlock() |
| 43 | return sub.ch, func() { |
| 44 | h.mu.Lock() |
| 45 | delete(h.live, sub) |
| 46 | h.mu.Unlock() |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | // Follow counts an open /events stream of the person's until done. |
| 51 | func (h *Hub) Follow(person string) (done func()) { |
| 52 | h.mu.Lock() |
| 53 | h.streams[person]++ |
| 54 | h.mu.Unlock() |
| 55 | return func() { |
| 56 | h.mu.Lock() |
| 57 | defer h.mu.Unlock() |
| 58 | if h.streams[person]--; h.streams[person] <= 0 { |
| 59 | delete(h.streams, person) |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | // Streaming reports whether the person has an /events stream open in this |
| 65 | // process. Another process's (a deploy overlap) is not seen: a reply then |
| 66 | // may push while they watch, never the other way round. |
| 67 | func (h *Hub) Streaming(person string) bool { |
| 68 | h.mu.Lock() |
| 69 | defer h.mu.Unlock() |
| 70 | return h.streams[person] > 0 |
| 71 | } |
| 72 | |
| 73 | // publish hands a live-only event to the person's streams in its lane |
| 74 | // in this process, without waiting for any. |
| 75 | func (h *Hub) publish(person, lane string, e wire.Event) { |
| 76 | h.mu.Lock() |
| 77 | defer h.mu.Unlock() |
| 78 | for sub := range h.live { |
| 79 | if sub.person == person && sub.lane == lane { |
| 80 | select { |
| 81 | case sub.ch <- e: |
| 82 | default: |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | // Wait returns a channel closed at the person's next wake. |
| 89 | func (h *Hub) Wait(person string) <-chan struct{} { |
| 90 | h.mu.Lock() |
| 91 | defer h.mu.Unlock() |
| 92 | c := h.waiters[person] |
| 93 | if c == nil { |
| 94 | c = make(chan struct{}) |
| 95 | h.waiters[person] = c |
| 96 | } |
| 97 | return c |
| 98 | } |
| 99 | |
| 100 | // Wake releases everyone waiting on person. |
| 101 | func (h *Hub) Wake(person string) { |
| 102 | h.mu.Lock() |
| 103 | defer h.mu.Unlock() |
| 104 | if c := h.waiters[person]; c != nil { |
| 105 | close(c) |
| 106 | delete(h.waiters, person) |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | // Closing is closed when the process starts draining. |
| 111 | func (h *Hub) Closing() <-chan struct{} { return h.closing } |
| 112 | |
| 113 | // Drain closes every stream and tells runners to reconnect elsewhere. |
| 114 | func (h *Hub) Drain() { |
| 115 | h.mu.Lock() |
| 116 | defer h.mu.Unlock() |
| 117 | if !h.closed { |
| 118 | h.closed = true |
| 119 | close(h.closing) |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | // claim registers this process's connection for a runner and closes the |
| 124 | // previous one, so one process holds at most one socket per runner. |
| 125 | func (h *Hub) claim(runner string, stop func()) (release func()) { |
| 126 | h.mu.Lock() |
| 127 | h.next++ |
| 128 | id := h.next |
| 129 | previous := h.runners[runner] |
| 130 | h.runners[runner] = registration{id, stop} |
| 131 | h.mu.Unlock() |
| 132 | if previous.stop != nil { |
| 133 | previous.stop() |
| 134 | } |
| 135 | return func() { |
| 136 | h.mu.Lock() |
| 137 | defer h.mu.Unlock() |
| 138 | if h.runners[runner].id == id { |
| 139 | delete(h.runners, runner) |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | type registration struct { |
| 145 | id int64 |
| 146 | stop func() |
| 147 | } |
| 148 | |