Repository navigation
Conversation
| // Timeout setup | ||
| if config.InactivityTimeout > 0 { | ||
| timeoutCtx, cancel := context.WithTimeout(ctx, config.InactivityTimeout) | ||
| defer cancel() | ||
| ctx = timeoutCtx | ||
| } |
There was a problem hiding this comment.
This doesn't seem an Inactivity timeout but rather a global timeout, is this a leftover?
There was a problem hiding this comment.
This is the "connection" phase, during which we exchange basic headers.
In this phase, I opted for a global timeout, which is much simpler to implement (I'm not sure an inactivity timeout is even possible here).
There was a problem hiding this comment.
I see, maybe we could update the comment
// setup a wall clock timeout during the connection phase.
| if err != nil { | ||
| d.err = err | ||
| d.lock.Lock() | ||
| if d.err == nil { |
There was a problem hiding this comment.
I am not sure that this code is actually working; the whole system is based on the assumption that a download that doesn't progress will continuously return 0, nil in the Read operation. AFAIK, this isn't always the case, and if the Read is blocked for the whole timeout, we have no way to exit with the timeout error. I think that we probably need to do the read loop in a go routine and use a select to do early return in case the read doesn't advance
There was a problem hiding this comment.
the whole system is based on the assumption that a download that doesn't progress will continuously return 0, nil in the Read operation
No, the assumption is that closing an io.ReadCloser will unblock all pending Reads.
That's what the timer function does after a t.Timeout period, unless some bytes are read and the timer is Resetted.
At least this is the theory I didn't test this properly (yet).
There was a problem hiding this comment.
Ah, ok, I missed the Close call in my review. Anyway, I am not sure we should rely on this. Does the Close operation require stopping all running Read operations? For instance, the user could wrap the reader in a NopCloser, which completely ignores the close operation. In the specific case of HTTP body reading, we may be safe, but we should double-check.
| d.lock.Lock() | ||
| if d.err == nil { | ||
| d.err = fmt.Errorf("download timeout after %s of inactivity", d.timeout) | ||
| in.Close() |
There was a problem hiding this comment.
in this case I would add a comment here
| in.Close() | |
| in.Close() // close the reader for stopping the download |
| var timeoutTimer *time.Timer | ||
| if d.timeout > 0 { | ||
| timeoutTimer = time.AfterFunc(d.timeout, func() { | ||
| d.lock.Lock() | ||
| if d.err == nil { | ||
| d.err = fmt.Errorf("download timeout after %s of inactivity", d.timeout) | ||
| in.Close() | ||
| } | ||
| d.lock.Unlock() | ||
| }) | ||
| defer timeoutTimer.Stop() | ||
| } |
There was a problem hiding this comment.
I think you can create an already triggered timer to avoid the nil check below
| var timeoutTimer *time.Timer | |
| if d.timeout > 0 { | |
| timeoutTimer = time.AfterFunc(d.timeout, func() { | |
| d.lock.Lock() | |
| if d.err == nil { | |
| d.err = fmt.Errorf("download timeout after %s of inactivity", d.timeout) | |
| in.Close() | |
| } | |
| d.lock.Unlock() | |
| }) | |
| defer timeoutTimer.Stop() | |
| } | |
| var timeoutTimer *time.Timer | |
| if d.timeout > 0 { | |
| timeoutTimer = time.AfterFunc(d.timeout, func() { | |
| d.lock.Lock() | |
| if d.err == nil { | |
| d.err = fmt.Errorf("download timeout after %s of inactivity", d.timeout) | |
| in.Close() | |
| } | |
| d.lock.Unlock() | |
| }) | |
| defer timeoutTimer.Stop() | |
| }else{ | |
| timeoutTimer = time.NewTimer(0) | |
| } |
|
Ok, I'm closing this one and making a new proposal. |
This PR implements an "inactivity" timeout that triggers if no network activity lasts for a certain period of time.