-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
89 lines (70 loc) · 1.72 KB
/
main.go
File metadata and controls
89 lines (70 loc) · 1.72 KB
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
80
81
82
83
84
85
86
87
88
89
package main
import (
"flag"
"fmt"
"os"
"os/signal"
"strings"
"syscall"
"time"
"github.com/logrusorgru/aurora"
"github.com/spiceai/sping/pkg/ping"
"github.com/valyala/fasthttp"
)
var (
interval time.Duration
timeout time.Duration
method string
showContent bool
)
func main() {
flag.Parse()
method = strings.ToUpper(method)
if flag.NArg() < 1 {
fmt.Println("Usage: aping [-interval <duration>] [-timeout <duration>] [-show-content] <url>")
fmt.Println()
fmt.Println("Example: aping data.spiceai.io/health -interval 5s -timeout 5s")
return
}
uri := fasthttp.AcquireURI()
defer fasthttp.ReleaseURI(uri)
if err := uri.Parse(nil, []byte(flag.Arg(0))); err != nil {
fmt.Println(err)
return
}
if len(uri.Scheme()) == 0 {
uri.SetScheme("https")
}
req := fasthttp.AcquireRequest()
defer fasthttp.ReleaseRequest(req)
req.SetURI(uri)
req.Header.SetMethod(method)
req.Header.Set("Accept-Encoding", "gzip")
pingClient := ping.NewPingClient(req, timeout, showContent)
fmt.Printf("SPING %s %s\n", method, aurora.BrightCyan(uri.String()))
timer := time.NewTimer(0)
signalChannel := make(chan os.Signal, 1)
signal.Notify(signalChannel,
syscall.SIGINT)
go func() {
for {
select {
case <-signalChannel:
timer.Stop()
return
case <-timer.C:
pingClient.Ping()
timer.Reset(interval)
}
}
}()
<-signalChannel
pingClient.PrintStats()
fmt.Println()
}
func init() {
flag.DurationVar(&interval, "interval", time.Second, "interval between pings")
flag.DurationVar(&timeout, "timeout", time.Second*5, "timeout for each ping")
flag.BoolVar(&showContent, "show-content", false, "show response content")
flag.StringVar(&method, "method", "GET", "method to use for each ping")
}