diff --git a/js.go b/js.go index 5f8dfe3ee..9fec788a7 100644 --- a/js.go +++ b/js.go @@ -2899,10 +2899,11 @@ func (sub *Subscription) Fetch(batch int, opts ...PullOpt) ([]*Msg, error) { } // Make our request expiration a bit shorter than the current timeout. - expires := ttl - if ttl >= 20*time.Millisecond { - expires = ttl - 10*time.Millisecond + expiresDiff := time.Duration(float64(ttl) * 0.1) + if expiresDiff > 5*time.Second { + expiresDiff = 5 * time.Second } + expires := ttl - expiresDiff nr.Batch = batch - len(msgs) nr.Expires = expires @@ -3166,10 +3167,11 @@ func (sub *Subscription) FetchBatch(batch int, opts ...PullOpt) (MessageBatch, e ttl = time.Until(deadline) // Make our request expiration a bit shorter than the current timeout. - expires := ttl - if ttl >= 20*time.Millisecond { - expires = ttl - 10*time.Millisecond + expiresDiff := time.Duration(float64(ttl) * 0.1) + if expiresDiff > 5*time.Second { + expiresDiff = 5 * time.Second } + expires := ttl - expiresDiff requestBatch := batch - len(result.msgs) req := nextRequest{ diff --git a/test/js_test.go b/test/js_test.go index 0363706c0..751e7c192 100644 --- a/test/js_test.go +++ b/test/js_test.go @@ -1357,7 +1357,7 @@ func TestPullSubscribeFetchBatchWithHeartbeat(t *testing.T) { if msgs.Error() != nil { t.Fatalf("Unexpected error: %s", msgs.Error()) } - if elapsed < 290*time.Millisecond { + if elapsed < 250*time.Millisecond { t.Fatalf("Expected timeout after 300ms; got: %v", elapsed) }