From 8453b9e11dad5bc5acebd06b29ab42a0081e31d0 Mon Sep 17 00:00:00 2001 From: Randy <857271370@qq.com> Date: Sun, 20 Sep 2026 04:33:14 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix:=20let=20callers=20choose=20?= =?UTF-8?q?Scan=20deadlines?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- client.go | 7 ++---- native.go | 7 ++++-- scan_deadline_test.go | 52 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 60 insertions(+), 8 deletions(-) create mode 100644 scan_deadline_test.go diff --git a/README.md b/README.md index b167bb9..f39110c 100644 --- a/README.md +++ b/README.md @@ -633,7 +633,7 @@ attempts, backoff and 20% jitter. It retries only `ResourceExhausted` statuses carrying `google.rpc.ErrorInfo` with `domain="sink"` and `reason="SCAN_ADMISSION_REJECTED"`, which guarantee rejection before backend execution. Set `ScanRetry.MaxAttempts` to one to disable retries. -`ClientOptions.ScanTimeout` defaults to 30 seconds and bounds the whole page, +`ClientOptions.ScanTimeout` defaults to zero and adds no deadline. A positive value explicitly bounds the whole page, including all attempts and backoff. A shorter caller deadline wins; cancellation interrupts backoff. Save the next cursor only after successfully processing the returned page; automatic admission retries do not replay successful pages. diff --git a/client.go b/client.go index 9c0ebad..d5d9317 100644 --- a/client.go +++ b/client.go @@ -29,7 +29,6 @@ const ( defaultRetryJitter = 0.2 defaultMaxMessageBytes = 64 << 20 defaultDNSRefreshInterval = 30 * time.Second - defaultScanTimeout = 30 * time.Second ) // RetryPolicy controls backoff and attempt limits. Read retries Unavailable and @@ -50,7 +49,8 @@ type ClientOptions struct { ScanRetry RetryPolicy // ScanTimeout bounds a whole page, including admission retries and backoff. - // Zero defaults to 30 seconds. A shorter context deadline wins. + // Zero adds no deadline. A positive value is an explicit caller-selected + // page limit; a shorter context deadline wins. ScanTimeout time.Duration MaxReceiveMessageBytes int MaxSendMessageBytes int @@ -167,9 +167,6 @@ func newClientConfig(opts ClientOptions) (clientConfig, error) { return config, fmt.Errorf("create Sink client: scan retry: %w", err) } scanTimeout := opts.ScanTimeout - if scanTimeout == 0 { - scanTimeout = defaultScanTimeout - } maxReceiveBytes := opts.MaxReceiveMessageBytes if maxReceiveBytes == 0 { maxReceiveBytes = defaultMaxMessageBytes diff --git a/native.go b/native.go index 4e12a69..c7f278f 100644 --- a/native.go +++ b/native.go @@ -216,8 +216,11 @@ func (c *Client) Scan(ctx context.Context, req ScanRequest) (ScanResponse, error if err != nil { return empty, err } - ctx, cancel := context.WithTimeout(ctx, c.config.scanTimeout) - defer cancel() + if c.config.scanTimeout > 0 { + var cancel context.CancelFunc + ctx, cancel = context.WithTimeout(ctx, c.config.scanTimeout) + defer cancel() + } var response *sinkv1.ScanResponse backoff := c.config.scanRetry.InitialBackoff for attempt := 1; attempt <= c.config.scanRetry.MaxAttempts; attempt++ { diff --git a/scan_deadline_test.go b/scan_deadline_test.go new file mode 100644 index 0000000..ca40e30 --- /dev/null +++ b/scan_deadline_test.go @@ -0,0 +1,52 @@ +package sink_test + +import ( + "context" + "testing" + "time" + + sink "github.com/liran/sink-go" + sinkv1 "github.com/liran/sink-go/api/sink/v1" +) + +type scanDeadlineServer struct { + sinkv1.UnimplementedSinkServer + deadlines chan time.Time +} + +func (s *scanDeadlineServer) Scan(ctx context.Context, _ *sinkv1.ScanRequest) (*sinkv1.ScanResponse, error) { + deadline, _ := ctx.Deadline() + s.deadlines <- deadline + response := &sinkv1.ScanResponse{} + return response, nil +} + +func TestScanDeadlineBelongsToCaller(t *testing.T) { + for _, mode := range []string{"none", "context", "explicit_option"} { + t.Run(mode, func(t *testing.T) { + server := &scanDeadlineServer{deadlines: make(chan time.Time, 1)} + opts := sink.ClientOptions{} + if mode == "explicit_option" { + opts.ScanTimeout = time.Hour + } + client := startTestClient(t, server, opts) + ctx := context.Background() + if mode == "context" { + var cancel context.CancelFunc + ctx, cancel = context.WithTimeout(ctx, time.Hour) + defer cancel() + } + request := sink.ScanRequest{Command: sdkNativeRequest().Command} + if _, err := client.Scan(ctx, request); err != nil { + t.Fatal(err) + } + got := <-server.deadlines + if mode == "none" && !got.IsZero() { + t.Fatalf("default Scan invented a deadline: %v", got) + } + if mode != "none" && (got.IsZero() || time.Until(got) < 59*time.Minute) { + t.Fatalf("caller hour shortened: %v", got) + } + }) + } +}