forked from gocolly/colly
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Description: Added random delay config to limitRule that allow user to specify additional parameter to randomized the delay on each request. Added an example for random delay Testing Methodogy: Used the rate_limit app in the _example dir and verify that adding RandomDelay will cause each request to have different delay time.
- Loading branch information
Showing
2 changed files
with
45 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/gocolly/colly" | ||
"github.com/gocolly/colly/debug" | ||
) | ||
|
||
func main() { | ||
url := "https://httpbin.org/delay/2" | ||
|
||
// Instantiate default collector | ||
c := colly.NewCollector() | ||
|
||
// Attach a debugger to the collector | ||
c.SetDebugger(&debug.LogDebugger{}) | ||
|
||
// Limit the number of threads started by colly to two | ||
// when visiting links which domains' matches "*httpbin.*" glob | ||
c.Limit(&colly.LimitRule{ | ||
DomainGlob: "*httpbin.*", | ||
Parallelism: 2, | ||
RandomDelay: 5 * time.Second, | ||
}) | ||
|
||
// Start scraping in four threads on https://httpbin.org/delay/2 | ||
for i := 0; i < 4; i++ { | ||
go c.Visit(fmt.Sprintf("%s?n=%d", url, i)) | ||
} | ||
// Start scraping on https://httpbin.org/delay/2 | ||
c.Visit(url) | ||
// Wait until threads are finished | ||
c.Wait() | ||
} |
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