|
| 1 | +// Copyright 2026 Alibaba Group Holding Ltd. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +// Package sbuf provides bounded, per-stream FIFO buffers for SSE (or similar) events keyed by eid, |
| 16 | +// used to serve disconnect resume (catch-up by event id). |
| 17 | +// It is storage-only: callers assign eids and decide when to delete a stream. |
| 18 | +package sbuf |
| 19 | + |
| 20 | +import ( |
| 21 | + "sync" |
| 22 | +) |
| 23 | + |
| 24 | +// Store holds bounded event rings keyed by caller-defined stream IDs (e.g. command execution id). |
| 25 | +type Store struct { |
| 26 | + cfg Config |
| 27 | + mu sync.Mutex |
| 28 | + streams map[string]*streamBuf |
| 29 | +} |
| 30 | + |
| 31 | +type streamBuf struct { |
| 32 | + mu sync.Mutex |
| 33 | + lastEid int64 |
| 34 | + ring *ring |
| 35 | + maxBytes int64 |
| 36 | +} |
| 37 | + |
| 38 | +// NewStore creates an empty store. cfg is copied after normalization. |
| 39 | +func NewStore(cfg Config) *Store { |
| 40 | + cfg = cfg.normalized() |
| 41 | + return &Store{ |
| 42 | + cfg: cfg, |
| 43 | + streams: make(map[string]*streamBuf), |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +// Append adds one event to the stream's ring. Payload is copied. |
| 48 | +// With StrictMonotonic, returns ErrOutOfOrder if eid <= previous eid for this stream. |
| 49 | +func (s *Store) Append(streamID string, eid int64, payload []byte) error { |
| 50 | + if streamID == "" { |
| 51 | + return ErrEmptyStreamID |
| 52 | + } |
| 53 | + sb := s.getOrCreate(streamID) |
| 54 | + sb.mu.Lock() |
| 55 | + defer sb.mu.Unlock() |
| 56 | + |
| 57 | + if s.cfg.StrictMonotonic { |
| 58 | + if eid <= sb.lastEid { |
| 59 | + return ErrOutOfOrder |
| 60 | + } |
| 61 | + } |
| 62 | + sb.lastEid = eid |
| 63 | + sb.ring.push(eid, payload, sb.maxBytes) |
| 64 | + return nil |
| 65 | +} |
| 66 | + |
| 67 | +func (s *Store) getOrCreate(streamID string) *streamBuf { |
| 68 | + s.mu.Lock() |
| 69 | + defer s.mu.Unlock() |
| 70 | + if sb, ok := s.streams[streamID]; ok { |
| 71 | + return sb |
| 72 | + } |
| 73 | + sb := &streamBuf{ |
| 74 | + ring: newRing(s.cfg.MaxEvents), |
| 75 | + maxBytes: s.cfg.MaxBytes, |
| 76 | + } |
| 77 | + s.streams[streamID] = sb |
| 78 | + return sb |
| 79 | +} |
| 80 | + |
| 81 | +// EventsAfter returns a snapshot of events with EID > afterEid in order. |
| 82 | +// If the stream does not exist, ok is false and events is nil. |
| 83 | +func (s *Store) EventsAfter(streamID string, afterEid int64) (events []Event, ok bool) { |
| 84 | + s.mu.Lock() |
| 85 | + sb, found := s.streams[streamID] |
| 86 | + s.mu.Unlock() |
| 87 | + if !found { |
| 88 | + return nil, false |
| 89 | + } |
| 90 | + sb.mu.Lock() |
| 91 | + defer sb.mu.Unlock() |
| 92 | + return sb.ring.snapshotAfter(afterEid), true |
| 93 | +} |
| 94 | + |
| 95 | +// Delete removes a stream buffer. No-op if missing. |
| 96 | +func (s *Store) Delete(streamID string) { |
| 97 | + s.mu.Lock() |
| 98 | + defer s.mu.Unlock() |
| 99 | + delete(s.streams, streamID) |
| 100 | +} |
| 101 | + |
| 102 | +// Has reports whether a stream currently exists. |
| 103 | +func (s *Store) Has(streamID string) bool { |
| 104 | + s.mu.Lock() |
| 105 | + defer s.mu.Unlock() |
| 106 | + _, ok := s.streams[streamID] |
| 107 | + return ok |
| 108 | +} |
0 commit comments