Filter stale batches from live terminal logs

This commit is contained in:
npc0-hue
2026-08-25 22:34:33 +08:00
parent 7ebe3bb3dd
commit 321bda3f2f
4 changed files with 67 additions and 2 deletions
+12
View File
@@ -14,6 +14,7 @@ import (
const (
logEventHeartbeatInterval = 15 * time.Second
liveLogSourceClockSkew = 90 * time.Second
managedLogSessionIDPrefix = "log-session:"
)
@@ -23,6 +24,7 @@ func (h *coreHandlers) serverLogEvents(w http.ResponseWriter, r *http.Request) {
writeMethodNotAllowed(w, http.MethodGet)
return
}
liveBoundary := time.Now().UTC().Add(-liveLogSourceClockSkew)
instance, streams, liveEligible, subscription, err := h.openLogEventSubscription(r)
if err != nil {
writeServiceError(w, err)
@@ -129,6 +131,12 @@ func (h *coreHandlers) serverLogEvents(w http.ResponseWriter, r *http.Request) {
if !active.contains(event.Stream) {
continue
}
if !sourceLogEntryIsLive(event.Entry, liveBoundary) {
if event.Entry.Seq > emittedThrough[event.Stream.ID] {
emittedThrough[event.Stream.ID] = event.Entry.Seq
}
continue
}
if !active.hasStream(event.Stream.ID) {
active.streams = append(active.streams, event.Stream)
if err := writeSSEJSON(w, "stream", "", dto.LogStreamFromDomain(event.Stream)); err != nil {
@@ -152,6 +160,10 @@ func (h *coreHandlers) serverLogEvents(w http.ResponseWriter, r *http.Request) {
}
}
func sourceLogEntryIsLive(entry domain.LogEntry, liveBoundary time.Time) bool {
return !entry.Timestamp.IsZero() && !entry.Timestamp.Before(liveBoundary)
}
type supervisedLogSession struct {
sessionID string
startedAt time.Time