fix(go): protect durable spool replay during shutdown
This commit is contained in:
@@ -37,6 +37,8 @@ type durableRecordFile struct {
|
|||||||
record durableRecord
|
record durableRecord
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const durableReplayOperationTimeout = 30 * time.Second
|
||||||
|
|
||||||
func NewDurableSink(delegate Sink, cfg DurableConfig) *DurableSink {
|
func NewDurableSink(delegate Sink, cfg DurableConfig) *DurableSink {
|
||||||
if delegate == nil {
|
if delegate == nil {
|
||||||
panic("durable delegate sink must not be 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():
|
case <-ctx.Done():
|
||||||
return
|
return
|
||||||
case <-ticker.C:
|
case <-ticker.C:
|
||||||
if err := s.ReplayOnce(ctx); err != nil && onError != nil {
|
if err := s.replayOnceFromLoop(ctx); err != nil && onError != nil {
|
||||||
onError(err)
|
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 {
|
func (s *DurableSink) Close() error {
|
||||||
return s.delegate.Close()
|
return s.delegate.Close()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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 {
|
func durableTestEnvelope() envelope.FrameEnvelope {
|
||||||
return envelope.FrameEnvelope{
|
return envelope.FrameEnvelope{
|
||||||
Protocol: envelope.ProtocolJT808,
|
Protocol: envelope.ProtocolJT808,
|
||||||
@@ -187,3 +210,26 @@ func (s *scriptedSink) PublishUnified(context.Context, envelope.FrameEnvelope) e
|
|||||||
func (s *scriptedSink) Close() error {
|
func (s *scriptedSink) Close() error {
|
||||||
return nil
|
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
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user