Skip to content
This repository was archived by the owner on Nov 8, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
7 changes: 2 additions & 5 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions native.go
Original file line number Diff line number Diff line change
Expand Up @@ -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++ {
Expand Down
52 changes: 52 additions & 0 deletions scan_deadline_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}
Loading