This repository has been archived by the owner on Oct 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
/
store.go
134 lines (103 loc) · 2.53 KB
/
store.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package incus
import (
"sync"
"github.com/spf13/viper"
)
type Storage struct {
memory *MemoryStore
redis *RedisStore
StorageType string
userMu sync.RWMutex
pageMu sync.RWMutex
}
func NewStore(stats RuntimeStats) *Storage {
storeType := "memory"
var redisStore *RedisStore
if viper.GetBool("redis_enabled") {
redisHost := viper.GetString("redis_port_6379_tcp_addr")
redisPort := viper.GetInt("redis_port_6379_tcp_port")
connPoolSize := viper.GetInt("redis_connection_pool_size")
numConsumers := viper.GetInt("redis_activity_consumers")
redisStore = newRedisStore(redisHost, redisPort, numConsumers, connPoolSize, stats)
storeType = "redis"
}
var Store = Storage{
&MemoryStore{make(map[string]map[string]*Socket), make(map[string]map[string]*Socket), 0},
redisStore,
storeType,
sync.RWMutex{},
sync.RWMutex{},
}
return &Store
}
func (this *Storage) Save(sock *Socket) error {
this.userMu.Lock()
this.memory.Save(sock)
this.userMu.Unlock()
if this.StorageType == "redis" {
if err := this.redis.Save(sock); err != nil {
return err
}
}
return nil
}
func (this *Storage) Remove(sock *Socket) error {
this.userMu.Lock()
this.memory.Remove(sock)
this.userMu.Unlock()
if this.StorageType == "redis" {
if err := this.redis.Remove(sock); err != nil {
return err
}
}
return nil
}
func (this *Storage) Client(UID string) (map[string]*Socket, error) {
defer this.userMu.RUnlock()
this.userMu.RLock()
return this.memory.Client(UID)
}
func (this *Storage) Clients() map[string]map[string]*Socket {
defer this.userMu.RUnlock()
this.userMu.RLock()
return this.memory.Clients()
}
func (this *Storage) ClientList() ([]string, error) {
if this.StorageType == "redis" {
return this.redis.Clients()
}
return nil, nil
}
func (this *Storage) Count() (int64, error) {
if this.StorageType == "redis" {
return this.redis.Count()
}
return this.memory.Count()
}
func (this *Storage) SetPage(sock *Socket) error {
this.pageMu.Lock()
this.memory.SetPage(sock)
this.pageMu.Unlock()
if this.StorageType == "redis" {
if err := this.redis.SetPage(sock); err != nil {
return err
}
}
return nil
}
func (this *Storage) UnsetPage(sock *Socket) error {
this.pageMu.Lock()
this.memory.UnsetPage(sock)
this.pageMu.Unlock()
if this.StorageType == "redis" {
if err := this.redis.UnsetPage(sock); err != nil {
return err
}
}
return nil
}
func (this *Storage) getPage(page string) map[string]*Socket {
defer this.pageMu.RUnlock()
this.pageMu.RLock()
return this.memory.getPage(page)
}