-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcustom.go
79 lines (68 loc) · 1.88 KB
/
custom.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// +build ignore
package main
import (
"context"
"fmt"
"net/http"
"os"
"os/signal"
"syscall"
poller "github.com/SunSince90/website-poller"
)
// This is a simple example that defines some custom polling options.
func main() {
id := "poll-user"
offsetRange := 5
page := &poller.Page{
ID: &id,
URL: "https://api.github.com/users/sunsince90",
Headers: map[string][]string{
"Accept": {"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"},
"Accept-Encoding": {"gzip, deflate, br"},
"Accept-Language": {"en-US,it-IT;q=0.8,it;q=0.5,en;q=0.3"},
"Cache-Control": {"no-cache"},
},
UserAgentOptions: &poller.UserAgentOptions{
UserAgents: []string{
"USER-AGENT-1",
"USER-AGENT-2",
"USER-AGENT-3",
// ...
},
RandomUA: false, // rotate them at each request
},
PollOptions: &poller.PollOptions{
Frequency: 20, // poll every 20 seconds
RandomFrequency: true, // mimick user behavior: don't make requests at a fixed time
OffsetRange: &offsetRange, // offsetRange is 5, so the range of requests is [15 - 25] seconds
},
}
p, err := poller.New(page)
if err != nil {
fmt.Println("error occurred:", err)
os.Exit(1)
}
p.SetHandlerFunc(handleResponse)
ctx, canc := context.WithCancel(context.Background())
exitChan := make(chan struct{})
go func() {
p.Start(ctx, true)
close(exitChan)
}()
// -- Graceful shutdown
signalChan := make(chan os.Signal, 1)
signal.Notify(
signalChan,
syscall.SIGHUP, // kill -SIGHUP XXXX
syscall.SIGINT, // kill -SIGINT XXXX or Ctrl+c
syscall.SIGQUIT, // kill -SIGQUIT XXXX
)
<-signalChan
fmt.Println("exit requested")
canc()
<-exitChan // Wait for the poller goroutine to return
fmt.Println("goodbye!")
}
func handleResponse(id string, resp *http.Response, err error) {
fmt.Println("request with id", id, "returned status", resp.Status)
}