Skip to content

Commit 3cbc879

Browse files
committed
fix stream timeout
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
1 parent 05add42 commit 3cbc879

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

http.go

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,10 @@ func (h *httpClient) stream(ctx context.Context, addr string, req client.Request
181181
if len(opts.ContentType) > 0 {
182182
ct = opts.ContentType
183183
}
184-
185184
// set timeout in nanoseconds
186-
header.Set("Timeout", fmt.Sprintf("%d", opts.RequestTimeout))
185+
if opts.StreamTimeout > time.Duration(0) {
186+
header.Set("Timeout", fmt.Sprintf("%d", opts.StreamTimeout))
187+
}
187188
// set the content type for the request
188189
header.Set("Content-Type", ct)
189190

@@ -399,22 +400,22 @@ func (h *httpClient) Call(ctx context.Context, req client.Request, rsp interface
399400
func (h *httpClient) Stream(ctx context.Context, req client.Request, opts ...client.CallOption) (client.Stream, error) {
400401
// make a copy of call opts
401402
callOpts := h.opts.CallOptions
402-
for _, opt := range opts {
403-
opt(&callOpts)
403+
for _, o := range opts {
404+
o(&callOpts)
404405
}
405406

406407
// check if we already have a deadline
407408
d, ok := ctx.Deadline()
408-
if !ok {
409+
if !ok && callOpts.StreamTimeout > time.Duration(0) {
409410
var cancel context.CancelFunc
410411
// no deadline so we create a new one
411-
ctx, cancel = context.WithTimeout(ctx, callOpts.RequestTimeout)
412+
ctx, cancel = context.WithTimeout(ctx, callOpts.StreamTimeout)
412413
defer cancel()
413414
} else {
414415
// got a deadline so no need to setup context
415416
// but we need to set the timeout we pass along
416-
opt := client.WithRequestTimeout(time.Until(d))
417-
opt(&callOpts)
417+
o := client.WithStreamTimeout(time.Until(d))
418+
o(&callOpts)
418419
}
419420

420421
// should we noop right here?
@@ -426,10 +427,7 @@ func (h *httpClient) Stream(ctx context.Context, req client.Request, opts ...cli
426427

427428
/*
428429
// make copy of call method
429-
hstream, err := h.stream()
430-
if err != nil {
431-
return nil, err
432-
}
430+
hstream := h.stream
433431
// wrap the call in reverse
434432
for i := len(callOpts.CallWrappers); i > 0; i-- {
435433
hstream = callOpts.CallWrappers[i-1](hstream)

0 commit comments

Comments
 (0)