SSE 调整

This commit is contained in:
2026-06-10 15:02:58 +08:00
parent 5f015a5c75
commit 41f2a84979
4 changed files with 92 additions and 9 deletions
+5 -5
View File
@@ -66,14 +66,14 @@ type subscription struct {
st *streamer
}
// Next blocks until at least one event with ID >= lastEventID is available,
// Next blocks until at least one event with ID >= nextEventID is available,
// the stream closes, or ctx is cancelled.
// It returns all events from lastEventID to the current tail.
func (sub *subscription) Next(ctx context.Context, lastEventID int64) ([]SSEEvent, bool, error) {
// It returns all events from nextEventID to the current tail.
func (sub *subscription) Next(ctx context.Context, nextEventID int64) ([]SSEEvent, bool, error) {
sub.st.mu.Lock()
defer sub.st.mu.Unlock()
for int64(len(sub.st.events)) <= lastEventID && !sub.st.closed {
for int64(len(sub.st.events)) <= nextEventID && !sub.st.closed {
done := make(chan struct{})
go func() {
select {
@@ -88,7 +88,7 @@ func (sub *subscription) Next(ctx context.Context, lastEventID int64) ([]SSEEven
return nil, false, ctx.Err()
}
}
out := append([]SSEEvent(nil), sub.st.events[lastEventID:]...)
out := append([]SSEEvent(nil), sub.st.events[nextEventID:]...)
return out, sub.st.closed, nil
}