-
Notifications
You must be signed in to change notification settings - Fork 3
/
request.go
96 lines (77 loc) · 1.94 KB
/
request.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
package carrot
import (
"encoding/json"
"fmt"
"time"
)
const (
MetricCount = 12
RequestCreation = iota // 1
MiddlewareInput // 2
MiddlewareOutputToDispatcher // 3
DispatchLookupStart // 4
DispatchLookupEnd // 5
DispatchRequestStart // 6
DispatchRequestEnd // 7
MethodReflectionStart // 8
MethodReflectionEnd // 9
ControllerMethodStart // 10
ControllerMethodEnd // 11
ResponderInvocation
ResponderElapsed
)
// request structs
type Request struct {
SessionToken SessionToken
endpoint string
Params map[string]interface{}
Offset *offset
data []byte
metrics []time.Time
err error
}
func NewRequest(session *Session, message []byte) *Request { //returns error,
m := make([]time.Time, MetricCount)
req := Request{
SessionToken: session.Token,
metrics: m,
data: message,
}
var md messageData
err := json.Unmarshal(message, &md)
if err == nil {
err = validSession(session.Token, SessionToken(md.SessionToken))
}
req.err = err
req.endpoint = md.Endpoint
req.Params = md.Payload.Params
req.Offset = md.Payload.Offset
req.AddMetric(RequestCreation)
return &req
}
// Add the time that a request is created to the request metric tracker
func (r *Request) AddMetric(stage int) {
r.metrics[stage] = time.Now()
}
func (r *Request) End() {
//logBenchmarks(r.metrics)
}
func logBenchmarks(metrics []time.Time) {
var prev time.Time
for i, metric := range metrics {
if i != 0 {
if i == 1 {
fmt.Printf("%v => %v\n", i, time.Now().Sub(metric))
} else {
fmt.Printf("%v => %v\n", i, metric.Sub(prev))
}
}
prev = metric
}
}
func validSession(serverToken SessionToken, clientToken SessionToken) error {
if serverToken != clientToken {
return fmt.Errorf("client-server token mismatch")
}
return nil
}