forked from influxdata/influxdb
-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.go
73 lines (60 loc) · 1.34 KB
/
server.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
package main
import (
"expvar"
"fmt"
"github.com/paulbellamy/ratecounter"
"io"
"io/ioutil"
"net/http"
"strings"
"sync"
"time"
)
var (
counter *ratecounter.RateCounter
hitspersecond = expvar.NewInt("hits_per_second")
mu sync.Mutex
m sync.Mutex
)
// Query handles /query endpoint
func Query(w http.ResponseWriter, req *http.Request) {
io.WriteString(w, "du")
}
// Count handles /count endpoint
func Count(w http.ResponseWriter, req *http.Request) {
io.WriteString(w, fmt.Sprintf("%v", linecount))
}
var n int
var linecount int
// Write handles /write endpoints
func Write(w http.ResponseWriter, req *http.Request) {
mu.Lock()
n++
mu.Unlock()
counter.Incr(1)
hitspersecond.Set(counter.Rate())
w.WriteHeader(http.StatusNoContent)
fmt.Printf("Reqests Per Second: %v\n", hitspersecond)
fmt.Printf("Count: %v\n", n)
content, _ := ioutil.ReadAll(req.Body)
m.Lock()
arr := strings.Split(string(content), "\n")
linecount += len(arr)
m.Unlock()
fmt.Printf("Line Count: %v\n\n", linecount)
}
func init() {
n = 0
linecount = 0
counter = ratecounter.NewRateCounter(1 * time.Second)
}
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/query", Query)
mux.HandleFunc("/write", Write)
mux.HandleFunc("/count", Count)
err := http.ListenAndServe(":1234", mux)
if err != nil {
fmt.Println("Fatal")
}
}