-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathglobals.go
56 lines (41 loc) · 1.25 KB
/
globals.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
package cslb
/*
globals and the manipulation of the current cslb predominantly exist for tests. If we didn't have
tests, globals would solely consist of an init() function something like:
func init() {
currentCSLB = newCslb()
Enable(http.DefaultTransport.(*http.Transport))
}
because in a normal application only one cslb is created and it lives for the life of the program.
*/
import (
"net/http"
"sync"
)
var (
cslbMu sync.RWMutex // For go test -race as tests creates new cslbs so they can
currentCSLB *cslb // be sure they are working with a known initial state.
)
// init enables the http DefaultTransport for CSLB processing. Perhaps we should have an env
// variable to control this or possibly not even enable it by default?
func init() {
realInit().start()
}
// realInit is separated out from init() so tests can call it multiple times without knowing the
// innards of what is needed to reset the globals to their initial state.
func realInit() *cslb {
cslb := setCSLB(newCslb())
Enable(http.DefaultTransport.(*http.Transport))
return cslb
}
func setCSLB(c *cslb) *cslb {
cslbMu.Lock()
defer cslbMu.Unlock()
currentCSLB = c
return c
}
func getCSLB() *cslb {
cslbMu.RLock()
defer cslbMu.RUnlock()
return currentCSLB
}