-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
123 lines (103 loc) · 3.06 KB
/
main.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Package main implements the tool.
package main
import (
"bytes"
"context"
"flag"
"fmt"
"io"
"log"
"net/http"
"sync"
"time"
"github.com/udhos/groupcache_oauth2/clientcredentials"
)
type application struct {
tokenURL string
clientID string
clientSecret string
scope string
targetURL string
targetMethod string
targetBody string
count int
softExpireSeconds int
interval time.Duration
concurrent bool
debug bool
}
func main() {
app := application{}
flag.StringVar(&app.tokenURL, "tokenURL", "http://localhost:8080/oauth/token", "token URL")
flag.StringVar(&app.clientID, "clientID", "admin", "client ID")
flag.StringVar(&app.clientSecret, "clientSecret", "admin", "client secret")
flag.StringVar(&app.scope, "scope", "", "space-delimited list of scopes")
flag.StringVar(&app.targetURL, "targetURL", "https://httpbin.org/get", "target URL")
flag.StringVar(&app.targetMethod, "targetMethod", "GET", "target method")
flag.StringVar(&app.targetBody, "targetBody", "targetBody", "target body")
flag.IntVar(&app.count, "count", 2, "how many requests to send")
flag.IntVar(&app.softExpireSeconds, "softExpireSeconds", 10, "token soft expire in seconds")
flag.DurationVar(&app.interval, "interval", 2*time.Second, "interval between sends")
flag.BoolVar(&app.concurrent, "concurrent", false, "concurrent requests")
flag.BoolVar(&app.debug, "debug", false, "enable debug logging")
flag.Parse()
groupcacheWorkspace := startGroupcache()
options := clientcredentials.Options{
TokenURL: app.tokenURL,
ClientID: app.clientID,
ClientSecret: app.clientSecret,
Scope: app.scope,
HTTPClient: http.DefaultClient,
SoftExpireInSeconds: app.softExpireSeconds,
Debug: app.debug,
GroupcacheWorkspace: groupcacheWorkspace,
}
client := clientcredentials.New(options)
metrics(client)
if app.concurrent {
//
// concurrent requests
//
var wg sync.WaitGroup
for i := 1; i <= app.count; i++ {
j := i
wg.Add(1)
go func() {
send(&app, client, j)
wg.Done()
}()
}
wg.Wait()
return
}
//
// non-concurrent requests
//
for i := 1; i <= app.count; i++ {
send(&app, client, i)
}
}
func send(app *application, client *clientcredentials.Client, i int) {
label := fmt.Sprintf("request %d/%d", i, app.count)
req, errReq := http.NewRequestWithContext(context.TODO(), app.targetMethod,
app.targetURL, bytes.NewBufferString(app.targetBody))
if errReq != nil {
log.Fatalf("%s: request: %v", label, errReq)
}
resp, errDo := client.Do(req)
if errDo != nil {
log.Fatalf("%s: do: %v", label, errDo)
}
defer resp.Body.Close()
log.Printf("%s: status: %d", label, resp.StatusCode)
body, errBody := io.ReadAll(resp.Body)
if errBody != nil {
log.Fatalf("%s: body: %v", label, errBody)
}
log.Printf("%s: body:", label)
fmt.Println(string(body))
if i < app.count && app.interval != 0 {
log.Printf("%s: sleeping for interval=%v", label, app.interval)
time.Sleep(app.interval)
}
}