Skip to content

Commit

Permalink
Implement control over requests per second
Browse files Browse the repository at this point in the history
  • Loading branch information
STOYAN KIROV authored and STOYAN KIROV committed May 17, 2020
1 parent 11688f7 commit fe398ab
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,6 @@ import (
"time"
)

// Wayback CDX Server rate limits requests
const MAX_REQUESTS_COUNT = 28

// Pause the requests every 4 seconds
const PAUSE_INTERVAL = 4

type HistoryItem struct {
Timestamp string
Digest string
Expand All @@ -33,6 +27,7 @@ func main() {
filter := flag.String("filter", "", "Filter your search, using the wayback cdx filters (find more here: https://github.com/internetarchive/wayback/tree/master/wayback-cdx-server#filtering)")
collapse := flag.String("collapse", "", "A form of filtering, with which you can collaps adjasent fields(find more here: https://github.com/internetarchive/wayback/tree/master/wayback-cdx-server#collapsing)")

requestsPerSecond := flag.Int("req-per-sec", 0, "Requests per second. 0 means no one request at a time")
estimateTime := flag.Bool("time", false, "Show how much time it would take to make all requests for the current query")
printUrls := flag.Bool("print-urls", false, "Print to stdout only a list of historic URLs, which you can request later")
unique := flag.Bool("unique", false, "Print to stdout only unique reponses")
Expand Down Expand Up @@ -69,13 +64,14 @@ func main() {
}

historyItems := getHistoryItems(requestUrl)

if *estimateTime {
requestsCount := len(historyItems)
duration, err := time.ParseDuration(fmt.Sprintf("%vs", requestsCount/(MAX_REQUESTS_COUNT/PAUSE_INTERVAL)))
duration, err := time.ParseDuration(fmt.Sprintf("%vs", requestsCount/(*requestsPerSecond)))
if err != nil {
log.Fatalf("error parsing duration: %v", err)
}
fmt.Printf("All %v requests will take %v", requestsCount, duration)
fmt.Printf("All %v requests will be made in %v", requestsCount, duration)
}

var allHistoryUrls []string
Expand All @@ -89,13 +85,17 @@ func main() {
continue
}

wg.Add(1)
if i%MAX_REQUESTS_COUNT == 0 {
time.Sleep(PAUSE_INTERVAL * time.Second)
if i%*requestsPerSecond == 0 {
time.Sleep(1 * time.Second)
}

wg.Add(1)
go func() {
historicResponses = append(historicResponses, get(historyUrl))
response := get(historyUrl)
if !*printUrls && !*unique && *output == "" {
fmt.Println(string(response))
}
historicResponses = append(historicResponses, response)
wg.Done()
}()
}
Expand Down

0 comments on commit fe398ab

Please sign in to comment.