-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
55 lines (48 loc) · 959 Bytes
/
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
package main
import (
"flag"
"fmt"
"io"
"net/http"
"os"
"sync/atomic"
"time"
)
var (
addr = flag.String("addr", "localhost:8080", "http service address")
counter int64
)
func hello(rw http.ResponseWriter, req *http.Request) {
atomic.AddInt64(&counter, 1)
rw.WriteHeader(http.StatusOK)
rw.Write([]byte("hello, world"))
}
func echo(rw http.ResponseWriter, req *http.Request) {
if req.Method != "POST" {
rw.WriteHeader(http.StatusBadRequest)
return
}
atomic.AddInt64(&counter, 1)
rw.WriteHeader(http.StatusOK)
io.Copy(rw, req.Body)
}
func main() {
flag.Parse()
go func() {
ticker := time.NewTicker(time.Second)
for {
select {
case <-ticker.C:
qps := atomic.LoadInt64(&counter)
atomic.AddInt64(&counter, -qps)
fmt.Println("qps:", qps)
}
}
}()
http.HandleFunc("/hello", hello)
http.HandleFunc("/echo", echo)
err := http.ListenAndServe(*addr, nil)
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
}