-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcounter.go
More file actions
44 lines (37 loc) · 811 Bytes
/
counter.go
File metadata and controls
44 lines (37 loc) · 811 Bytes
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 aviation
import (
"context"
)
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
}
// SetRequesID attaches a request id to a context.
func SetRequestID(ctx context.Context, id int) context.Context {
return context.WithValue(ctx, 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
}