Prioritize fresh process log sessions

This commit is contained in:
npc0-hue
2026-08-31 05:11:54 +08:00
parent 90de21ad9f
commit d5f4d34e07
3 changed files with 83 additions and 4 deletions
+35 -3
View File
@@ -10,6 +10,7 @@ import (
"sort"
"strings"
"sync"
"time"
"browser.local/run/protocol"
)
@@ -44,8 +45,9 @@ type durableLogBatch struct {
}
type pendingLogSegment struct {
path string
batch durableLogBatch
path string
modTime time.Time
batch durableLogBatch
}
func NewLogSpool(dir string) (LogSpool, error) {
@@ -317,11 +319,41 @@ func (spool LogSpool) pendingSegmentsLocked() ([]pendingLogSegment, error) {
}
return nil, err
}
segments = append(segments, pendingLogSegment{path: path, batch: batch})
var modTime time.Time
if info, statErr := os.Stat(path); statErr == nil {
modTime = info.ModTime()
}
segments = append(segments, pendingLogSegment{path: path, modTime: modTime, batch: batch})
}
sort.SliceStable(segments, func(i, j int) bool { return pendingLogSegmentBefore(segments[i], segments[j]) })
return segments, nil
}
func pendingLogSegmentBefore(left pendingLogSegment, right pendingLogSegment) bool {
leftBatch := left.batch.LogBatchIngestRequest
rightBatch := right.batch.LogBatchIngestRequest
if leftBatch.LogStreamID == rightBatch.LogStreamID {
if leftBatch.FirstSeq != rightBatch.FirstSeq {
return leftBatch.FirstSeq < rightBatch.FirstSeq
}
return leftBatch.LastSeq < rightBatch.LastSeq
}
leftTime := pendingLogSegmentPriorityTime(left)
rightTime := pendingLogSegmentPriorityTime(right)
if !leftTime.Equal(rightTime) {
return leftTime.After(rightTime)
}
return left.path < right.path
}
func pendingLogSegmentPriorityTime(segment pendingLogSegment) time.Time {
batch := segment.batch.LogBatchIngestRequest
if batch.Source == "process" && strings.TrimSpace(batch.LogSessionID) != "" && !batch.SessionStartedAt.IsZero() {
return batch.SessionStartedAt
}
return segment.modTime
}
func (spool LogSpool) Ack(response protocol.LogBatchIngestResponse) error {
if response.AcceptedFrom == 0 || response.AcceptedTo < response.AcceptedFrom {
return nil