fix(go): protect durable spool replay during shutdown

This commit is contained in:
lingniu
2026-07-02 13:25:15 +08:00
parent 640f70636d
commit bc9025d566
2 changed files with 55 additions and 1 deletions

View File

@@ -37,6 +37,8 @@ type durableRecordFile struct {
record durableRecord
}
const durableReplayOperationTimeout = 30 * time.Second
func NewDurableSink(delegate Sink, cfg DurableConfig) *DurableSink {
if delegate == nil {
panic("durable delegate sink must not be nil")
@@ -130,13 +132,19 @@ func (s *DurableSink) ReplayLoop(ctx context.Context, interval time.Duration, on
case <-ctx.Done():
return
case <-ticker.C:
if err := s.ReplayOnce(ctx); err != nil && onError != nil {
if err := s.replayOnceFromLoop(ctx); err != nil && onError != nil {
onError(err)
}
}
}
}
func (s *DurableSink) replayOnceFromLoop(ctx context.Context) error {
replayCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), durableReplayOperationTimeout)
defer cancel()
return s.ReplayOnce(replayCtx)
}
func (s *DurableSink) Close() error {
return s.delegate.Close()
}

View File

@@ -95,6 +95,29 @@ func TestDurableSinkReplayPublishesRawBeforeUnifiedEvenWhenFilesAreOutOfOrder(t
}
}
func TestDurableSinkReplayLoopTickUsesUncancelledContext(t *testing.T) {
dir := t.TempDir()
env := durableTestEnvelope()
writeDurableRecord(t, filepath.Join(dir, "0001-raw.json"), durableRecord{Kind: "raw", Envelope: env})
delegate := &contextCheckingReplaySink{}
sink := NewDurableSink(delegate, DurableConfig{Directory: dir})
parent, cancel := context.WithCancel(context.Background())
cancel()
if err := sink.replayOnceFromLoop(parent); err != nil {
t.Fatalf("replayOnceFromLoop() error = %v", err)
}
if delegate.rawCtxErr != nil {
t.Fatalf("delegate saw cancelled context: %v", delegate.rawCtxErr)
}
if delegate.rawCalls != 1 {
t.Fatalf("raw calls = %d, want 1", delegate.rawCalls)
}
if files := spoolFiles(t, dir); len(files) != 0 {
t.Fatalf("spool files after replay = %#v, want none", files)
}
}
func durableTestEnvelope() envelope.FrameEnvelope {
return envelope.FrameEnvelope{
Protocol: envelope.ProtocolJT808,
@@ -187,3 +210,26 @@ func (s *scriptedSink) PublishUnified(context.Context, envelope.FrameEnvelope) e
func (s *scriptedSink) Close() error {
return nil
}
type contextCheckingReplaySink struct {
rawCtxErr error
unifiedCtxErr error
rawCalls int
unifiedCalls int
}
func (s *contextCheckingReplaySink) PublishRaw(ctx context.Context, _ envelope.FrameEnvelope) error {
s.rawCtxErr = ctx.Err()
s.rawCalls++
return s.rawCtxErr
}
func (s *contextCheckingReplaySink) PublishUnified(ctx context.Context, _ envelope.FrameEnvelope) error {
s.unifiedCtxErr = ctx.Err()
s.unifiedCalls++
return s.unifiedCtxErr
}
func (s *contextCheckingReplaySink) Close() error {
return nil
}