-
Notifications
You must be signed in to change notification settings - Fork 181
/
local_state_lock.go
101 lines (81 loc) · 2.51 KB
/
local_state_lock.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package distrlock
import (
"fmt"
"io/ioutil"
"os"
"sync"
"github.com/okex/exchain/libs/tendermint/libs/log"
)
// LocalStateService is designed to save stream state info into a local file.
// It's not supported to satisfy HA requirement.
// It mainly works when paired with LocalWebSocketEngine.
type LocalStateService struct {
logger log.Logger
lockerID string // unique identifier of locker
lockFileDir string
mutex *sync.Mutex
}
func NewLocalStateService(logger log.Logger, lockerID string, lockFileDir string) (s *LocalStateService, err error) {
_, err = os.Stat(lockFileDir)
if err != nil {
err = os.MkdirAll(lockFileDir, os.ModePerm)
}
if err == nil {
s = &LocalStateService{
logger: logger,
lockerID: lockerID,
lockFileDir: lockFileDir,
mutex: &sync.Mutex{},
}
}
logger.Debug(fmt.Sprintf("NewLocalStateService lockerId: %s lockFileDir: %s", lockerID, lockFileDir))
return s, err
}
func (s *LocalStateService) RemoveStateFile(stateKey string) error {
path := s.getFullPath(stateKey)
return os.Remove(path)
}
func (s *LocalStateService) getFullPath(stateName string) string {
return s.lockFileDir + string(os.PathSeparator) + s.lockerID + "." + stateName
}
func (s *LocalStateService) GetLockerID() string {
return s.lockerID
}
func (s *LocalStateService) GetDistState(stateKey string) (state string, err error) {
s.mutex.Lock()
defer s.mutex.Unlock()
stateFilePath := s.getFullPath(stateKey)
_, err = os.Stat(stateFilePath)
if os.IsNotExist(err) {
return "", nil
}
bytes, err := ioutil.ReadFile(stateFilePath)
if err == nil {
state = string(bytes)
}
return state, err
}
func (s *LocalStateService) SetDistState(stateKey string, stateValue string) error {
s.mutex.Lock()
defer s.mutex.Unlock()
stateFilePath := s.getFullPath(stateKey)
err := ioutil.WriteFile(stateFilePath, []byte(stateValue), 0600)
return err
}
// DiskLock is not supported in LocalStateService, in the other word,
// FetchDistLock and ReleaseDistLock will always be success.
func (s *LocalStateService) FetchDistLock(lockKey string, locker string, expiredInMS int) (bool, error) {
s.mutex.Lock()
defer s.mutex.Unlock()
return true, nil
}
func (s *LocalStateService) ReleaseDistLock(lockKey string, locker string) (bool, error) {
s.mutex.Lock()
defer s.mutex.Unlock()
return true, nil
}
func (s *LocalStateService) UnlockDistLockWithState(
lockKey string, locker string, stateKey string, stateValue string) (bool, error) {
err := s.SetDistState(stateKey, stateValue)
return true, err
}