-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput.go
More file actions
63 lines (54 loc) · 1.54 KB
/
input.go
File metadata and controls
63 lines (54 loc) · 1.54 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
package tract
import (
"context"
)
// Input specifies a way for a Tract to get requests.
type Input interface {
// Get gets the next request. The bool return value is true if a request was gotten.
// It's false when there is no requests and never will be any more.
Get() (Request, bool)
}
var (
_ Input = InputChannel(nil)
_ Input = InputGenerator{}
_ Input = MetricsInput{}
)
// InputChannel is a channel of requests.
type InputChannel <-chan Request
// Get gets the next request from the channel.
func (c InputChannel) Get() (Request, bool) {
request, ok := <-c
return request, ok
}
// InputGenerator generates request objects.
// It is the default input of a Tract.
type InputGenerator struct{}
// Get generates the next request.
// The current time is stored in the request at this generation time.
// It can be retrieved by using GetRequestStartTime().
func (c InputGenerator) Get() (Request, bool) {
return setRequestStartTime(context.Background(), now()), true
}
// MetricsInput is a wrapper around an Input that will automatically generate input latency metrics
type MetricsInput struct {
Input
metricsHandler MetricsHandler
}
// Get gets from the inner input while gathering metrics.
func (i MetricsInput) Get() (Request, bool) {
var (
request Request
ok bool
)
if i.metricsHandler != nil && i.metricsHandler.ShouldHandle() {
before := now()
request, ok = i.Input.Get()
after := now()
i.metricsHandler.HandleMetrics(
Metric{MetricsKeyIn, after.Sub(before)},
)
} else {
request, ok = i.Input.Get()
}
return request, ok
}