forked from evergreen-ci/gimlet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcounter.go
44 lines (37 loc) · 787 Bytes
/
counter.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
package gimlet
import (
"context"
"net/http"
)
var jobIDSource <-chan int
func init() {
jobIDSource = func() <-chan int {
out := make(chan int, 50)
go func() {
var jobID int
for {
jobID++
out <- jobID
}
}()
return out
}()
}
// getNumber is a source of safe monotonically increasing integers
// for use in request ids.
func getNumber() int {
return <-jobIDSource
}
func setRequestID(r *http.Request, id int) *http.Request {
return r.WithContext(context.WithValue(r.Context(), requestIDKey, id))
}
// GetRequestID returns the unique (monotonically increaseing) ID of
// the request since startup
func GetRequestID(ctx context.Context) int {
if rv := ctx.Value(requestIDKey); rv != nil {
if id, ok := rv.(int); ok {
return id
}
}
return -1
}