-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstate.go
58 lines (50 loc) · 963 Bytes
/
state.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
package cmlclient
import (
"fmt"
"sync"
)
type clientState uint32
func (cs clientState) String() string {
switch cs {
case stateInitial:
return "INITIAL"
case stateCheckVersion:
return "CHECKVERSION"
case stateAuthRequired:
return "AUTHREQUIRED"
case stateAuthenticating:
return "AUTHENTICATING"
case stateAuthenticated:
return "AUTHENTICATED"
default:
panic(fmt.Sprintf("unknown state %d", cs))
}
}
const (
stateInitial clientState = iota
stateCheckVersion
stateAuthRequired
stateAuthenticating
stateAuthenticated
)
type apiClientState struct {
state clientState
mu *sync.RWMutex
}
func newState() *apiClientState {
var mu sync.RWMutex
return &apiClientState{
mu: &mu,
state: stateInitial,
}
}
func (acs *apiClientState) set(state clientState) {
acs.mu.Lock()
defer acs.mu.Unlock()
acs.state = state
}
func (acs *apiClientState) get() clientState {
acs.mu.RLock()
defer acs.mu.RUnlock()
return acs.state
}