-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bypass request delay when request is cancelled #619
Open
waltton
wants to merge
7
commits into
gocolly:master
Choose a base branch
from
waltton:master
base: master
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7ed6592
Fix golint issue
waltton cd5d8db
Bypass request delay when request is cancelled
waltton 842b7e7
Remove NewRequestWithContext from test for 1.12 compability
waltton 191b0b7
Prevent panic in during when error was expected but it's nil
waltton 6c602af
Replace time based test with test based on new return error
waltton e0c3c8b
Remove trace and show magic numbers in the output
waltton a1e4bc8
Hardcode seed to make test more reliable
waltton File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package colly | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"math/rand" | ||
"net/http" | ||
"net/http/cookiejar" | ||
"net/http/httptest" | ||
"strconv" | ||
"strings" | ||
"sync" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestHTTPBackendDoCancelation(t *testing.T) { | ||
// hardcoded fixed to ensure that p, n, c are the same making the test more reliable | ||
rand.Seed(2927496558871806) | ||
|
||
// rand up to 10 to not extend the test duration too much | ||
p := 1 + rand.Intn(5) // p: parallel requests | ||
n := p + p*rand.Intn(10) // n: after n, cancel will be called; ensure 1 calls per worker + rand | ||
c := n + p*2 + rand.Intn(10) // c: total number of calls; ensure 2 calls per worker after cancel is called + rand | ||
|
||
ts := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { | ||
fmt.Fprint(rw, "OK") | ||
})) | ||
defer ts.Close() | ||
|
||
checkHeadersFunc := func(req *http.Request, statusCode int, header http.Header) bool { return true } | ||
|
||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
|
||
backend := &httpBackend{} | ||
jar, _ := cookiejar.New(nil) | ||
backend.Init(jar) | ||
limit := &LimitRule{ | ||
DomainRegexp: ".*", | ||
Parallelism: p, | ||
Delay: 5 * time.Millisecond, | ||
} | ||
backend.Limit(limit) | ||
|
||
var wg sync.WaitGroup | ||
wg.Add(c) | ||
|
||
out := make(chan error) | ||
|
||
for i := 0; i < c; i++ { | ||
go func(i int) { | ||
defer wg.Done() | ||
req, _ := http.NewRequest("GET", ts.URL+"/"+strconv.Itoa(i), nil) | ||
req = req.WithContext(ctx) | ||
|
||
_, err := backend.Do(req, 0, checkHeadersFunc) | ||
out <- err | ||
}(i) | ||
} | ||
|
||
go func() { | ||
wg.Wait() | ||
close(out) | ||
}() | ||
|
||
i := 0 | ||
nonEarlyCount := 0 | ||
for err := range out { | ||
i++ | ||
if i == n { | ||
cancel() | ||
} | ||
|
||
if i <= n { | ||
if err != nil { | ||
t.Errorf("no error was expected for the first %d responses; error: %q", n, err) | ||
} | ||
} else { | ||
errStr := "" | ||
if err != nil { | ||
errStr = err.Error() | ||
} | ||
|
||
// non early returns are allowed up to the number of maximum allowed concurrent requests; | ||
// bacause those requests could be already running when cancel was called | ||
if !strings.Contains(errStr, "early return") { | ||
if nonEarlyCount > p { | ||
t.Errorf("count of non early return is above the number of maximum allowed concurrent requests; p: %d; n: %d; c: %d", p, n, c) | ||
} | ||
nonEarlyCount++ | ||
} | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe earlier version of this PR returned
ctx.Err()
instead. Why did you decide to return a new error instead?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that was the case. Usually i would use wrap from
github.com/pkg/errors
and just add to the error but the library is not in use here.My first try at writing the code I was asserting against the time that it took to run all the calls and check if they return asap after the cancel; worked fine locally but fail on CI and yea, check like that is really unreliable.
So my second attempt its based on the error message; to check if the error was the normal error from the request or if it was the early return; I had to make the early return message different from
ctx.Err()
cause that is what we get from the http client that tries to run with a cancelled request.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for the late response. I don't think tests are good enough reason to return different errors (depending on where it happened) when context is cancelled. Are you aware of any other library that does not return
ctx.Err()
unchanged?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No worries, any suggestions on how to get the test done?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suppose you could make a test like this:
What do you think?