-
Notifications
You must be signed in to change notification settings - Fork 29
/
adapter.go
197 lines (163 loc) · 4.13 KB
/
adapter.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// Copyright (C) 2020 Finogeeks Co., Ltd
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License, version 3,
// as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package adapter
import (
"math/rand"
"reflect"
"time"
)
const (
DEBUG_LEVEL_DEBUG = "debug"
DEBUG_LEVEL_PROD = "prod"
)
type KafkaCommonCfg struct {
EnableIdempotence bool
//only avoid kafka sync send has problem can force to async send
ForceAsyncSend bool
ReplicaFactor int
NumPartitions int
NumProducers int
}
type DomainCfg struct {
Domains []string
}
type LockCfg struct {
Timeout int
Wait int
Force bool
}
type DistLockCfg struct {
LockInstance LockCfg
LockRoomState LockCfg
LockRoomStateExt LockCfg
}
type DebugCfg struct {
DebugLevel string
}
type CommonCfg struct {
Kafka KafkaCommonCfg
Domain DomainCfg
DistLock DistLockCfg
Debug DebugCfg
Cache CacheCfg
}
type CacheCfg struct {
TokenExpire int64
UtlExpire int64
LatestToken int
}
var AdapterCfg CommonCfg
func SetKafkaEnableIdempotence(enableIdempotence bool) {
AdapterCfg.Kafka.EnableIdempotence = enableIdempotence
}
func GetKafkaEnableIdempotence() bool {
return AdapterCfg.Kafka.EnableIdempotence
}
func SetKafkaForceAsyncSend(forceAsyncSend bool) {
AdapterCfg.Kafka.ForceAsyncSend = forceAsyncSend
}
func GetKafkaForceAsyncSend() bool {
return AdapterCfg.Kafka.ForceAsyncSend
}
func SetKafkaReplicaFactor(replicaFactor int) {
AdapterCfg.Kafka.ReplicaFactor = replicaFactor
}
func GetKafkaReplicaFactor() int {
return AdapterCfg.Kafka.ReplicaFactor
}
func SetKafkaNumPartitions(numPartitions int) {
AdapterCfg.Kafka.NumPartitions = numPartitions
}
func GetKafkaNumPartitions() int {
return AdapterCfg.Kafka.NumPartitions
}
func SetKafkaNumProducers(numProducers int) {
AdapterCfg.Kafka.NumProducers = numProducers
}
func GetKafkaNumProducers() int {
return AdapterCfg.Kafka.NumProducers
}
func SetDomainCfg(domains []string) {
AdapterCfg.Domain.Domains = domains
}
func GetDomainCfg() []string {
return AdapterCfg.Domain.Domains
}
func Contains(obj interface{}, target interface{}) bool {
targetValue := reflect.ValueOf(target)
switch reflect.TypeOf(target).Kind() {
case reflect.Slice, reflect.Array:
for i := 0; i < targetValue.Len(); i++ {
if targetValue.Index(i).Interface() == obj {
return true
}
}
case reflect.Map:
if targetValue.MapIndex(reflect.ValueOf(obj)).IsValid() {
return true
}
}
return false
}
func CheckSameDomain(domains []string) bool {
checkDomains := GetDomainCfg()
for _, domain := range domains {
if !Contains(domain, checkDomains) {
return false
}
}
return true
}
func GetDistLockCfg() DistLockCfg {
return AdapterCfg.DistLock
}
func SetDistLockItemCfg(item string, timeout, wait int, force bool) {
lockCfg := LockCfg{
Timeout: timeout,
Wait: wait,
Force: force,
}
switch item {
case "instance":
AdapterCfg.DistLock.LockInstance = lockCfg
case "room_state":
AdapterCfg.DistLock.LockRoomState = lockCfg
case "room_state_ext":
AdapterCfg.DistLock.LockRoomStateExt = lockCfg
}
}
func SetDebugLevel(debugLevel string) {
AdapterCfg.Debug.DebugLevel = debugLevel
}
func GetDebugLevel() string {
return AdapterCfg.Debug.DebugLevel
}
func Random(min, max int) int {
rand.Seed(time.Now().UnixNano())
return rand.Intn(max-min) + min
}
func SetCacheCfg(tokenExpire, utlExpire int64, latestToken int) {
AdapterCfg.Cache.TokenExpire = tokenExpire
AdapterCfg.Cache.UtlExpire = utlExpire
AdapterCfg.Cache.LatestToken = latestToken
}
func GetTokenExpire() int64 {
return AdapterCfg.Cache.TokenExpire
}
func GetUtlExpire() int64 {
return AdapterCfg.Cache.UtlExpire
}
func GetLatestToken() int {
return AdapterCfg.Cache.LatestToken
}