Skip to content

Commit 34470fa

Browse files
authored
Merge pull request #237 from devfeel/develop
Version 1.7.14 fixed can not set redis maxIdle & maxActive when use redis session, fix for issue #236
2 parents 6cd7fd2 + 49e19ff commit 34470fa

File tree

8 files changed

+67
-24
lines changed

8 files changed

+67
-24
lines changed

cache/redis/cache_redis.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ func NewRedisCache(serverURL string) *RedisCache {
2424

2525
// Exists check item exist in redis cache.
2626
func (ca *RedisCache) Exists(key string) (bool, error) {
27-
redisClient := redisutil.GetRedisClient(ca.serverURL)
27+
redisClient := redisutil.GetDefaultRedisClient(ca.serverURL)
2828
exists, err := redisClient.Exists(key)
2929
return exists, err
3030
}
3131

3232
// Incr increase int64 counter in redis cache.
3333
func (ca *RedisCache) Incr(key string) (int64, error) {
34-
redisClient := redisutil.GetRedisClient(ca.serverURL)
34+
redisClient := redisutil.GetDefaultRedisClient(ca.serverURL)
3535
val, err := redisClient.INCR(key)
3636
if err != nil {
3737
return 0, err
@@ -41,7 +41,7 @@ func (ca *RedisCache) Incr(key string) (int64, error) {
4141

4242
// Decr decrease counter in redis cache.
4343
func (ca *RedisCache) Decr(key string) (int64, error) {
44-
redisClient := redisutil.GetRedisClient(ca.serverURL)
44+
redisClient := redisutil.GetDefaultRedisClient(ca.serverURL)
4545
val, err := redisClient.DECR(key)
4646
if err != nil {
4747
return 0, err
@@ -52,15 +52,15 @@ func (ca *RedisCache) Decr(key string) (int64, error) {
5252
// Get cache from redis cache.
5353
// if non-existed or expired, return nil.
5454
func (ca *RedisCache) Get(key string) (interface{}, error) {
55-
redisClient := redisutil.GetRedisClient(ca.serverURL)
55+
redisClient := redisutil.GetDefaultRedisClient(ca.serverURL)
5656
reply, err := redisClient.GetObj(key)
5757
return reply, err
5858
}
5959

6060
// returns value string format by given key
6161
// if non-existed or expired, return "".
6262
func (ca *RedisCache) GetString(key string) (string, error) {
63-
redisClient := redisutil.GetRedisClient(ca.serverURL)
63+
redisClient := redisutil.GetDefaultRedisClient(ca.serverURL)
6464
reply, err := redisClient.Get(key)
6565
return reply, err
6666
}
@@ -100,7 +100,7 @@ func (ca *RedisCache) GetInt64(key string) (int64, error) {
100100
// Set cache to redis.
101101
// ttl is second, if ttl is 0, it will be forever.
102102
func (ca *RedisCache) Set(key string, value interface{}, ttl int64) error {
103-
redisClient := redisutil.GetRedisClient(ca.serverURL)
103+
redisClient := redisutil.GetDefaultRedisClient(ca.serverURL)
104104
var err error
105105
if ttl <= 0 {
106106
_, err = redisClient.Set(key, value)
@@ -113,15 +113,15 @@ func (ca *RedisCache) Set(key string, value interface{}, ttl int64) error {
113113
// Delete item in redis cacha.
114114
// if not exists, we think it's success
115115
func (ca *RedisCache) Delete(key string) error {
116-
redisClient := redisutil.GetRedisClient(ca.serverURL)
116+
redisClient := redisutil.GetDefaultRedisClient(ca.serverURL)
117117
_, err := redisClient.Del(key)
118118
return err
119119
}
120120

121121
// ClearAll will delete all item in redis cache.
122122
// never error
123123
func (ca *RedisCache) ClearAll() error {
124-
redisClient := redisutil.GetRedisClient(ca.serverURL)
124+
redisClient := redisutil.GetDefaultRedisClient(ca.serverURL)
125125
redisClient.FlushDB()
126126
return nil
127127
}

config/configs.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ type (
6969
ServerIP string `xml:"serverip,attr"` // remote session server url
7070
BackupServerUrl string `xml:"backupserverurl,attr"` // backup remote session server url
7171
StoreKeyPre string `xml:"storekeypre,attr"` // remote session StoreKeyPre
72+
MaxIdle int `xml:"maxidle,attr"` // remote session MaxIdle
73+
MaxActive int `xml:"maxactive,attr"` // remote session MaxActive
7274
}
7375

7476
// RouterNode dotweb app's router config

dotweb.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -670,7 +670,7 @@ func (app *DotWeb) printDotLogo() {
670670
app.Logger().Print(` / / / / / __ \ / __/| | /| / / / _ \ / __ \`, LogTarget_HttpServer)
671671
app.Logger().Print(` / /_/ / / /_/ // /_ | |/ |/ / / __/ / /_/ /`, LogTarget_HttpServer)
672672
app.Logger().Print(`/_____/ \____/ \__/ |__/|__/ \___/ /_.___/`, LogTarget_HttpServer)
673-
app.Logger().Print(` Happy 6.1`, LogTarget_HttpServer)
673+
app.Logger().Print(` Version 1.7.14`, LogTarget_HttpServer)
674674
}
675675

676676
// Close immediately stops the server.

framework/redis/redisutil.go

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ var (
2121
)
2222

2323
const (
24-
defaultTimeout = 60 * 10 // defaults to 10 minutes
24+
defaultTimeout = 60 * 10 // defaults to 10 minutes
25+
defaultMaxIdle = 10
26+
defaultMaxActive = 50
2527
)
2628

2729
func init() {
@@ -31,27 +33,39 @@ func init() {
3133

3234
// returns new connection pool
3335
// redisURL: connection string, like "redis:// :[email protected]:6379/0"
34-
func newPool(redisURL string) *redis.Pool {
36+
func newPool(redisURL string, maxIdle, maxActive int) *redis.Pool {
3537

3638
return &redis.Pool{
37-
MaxIdle: 5,
38-
MaxActive: 20, // max number of connections
39+
MaxIdle: maxIdle,
40+
MaxActive: maxActive, // max number of connections
3941
Dial: func() (redis.Conn, error) {
4042
c, err := redis.DialURL(redisURL)
4143
return c, err
4244
},
4345
}
4446
}
4547

46-
// GetRedisClient returns the RedisClient of specified address
47-
func GetRedisClient(address string) *RedisClient {
48+
// GetDefaultRedisClient returns the RedisClient of specified address
49+
// use default maxIdle & maxActive
50+
func GetDefaultRedisClient(address string) *RedisClient {
51+
return GetRedisClient(address, defaultMaxIdle, defaultMaxActive)
52+
}
53+
54+
// GetRedisClient returns the RedisClient of specified address & maxIdle & maxActive
55+
func GetRedisClient(address string, maxIdle, maxActive int) *RedisClient {
56+
if maxIdle <= 0 {
57+
maxIdle = defaultMaxIdle
58+
}
59+
if maxActive <= 0 {
60+
maxActive = defaultMaxActive
61+
}
4862
var redis *RedisClient
4963
var mok bool
5064
mapMutex.RLock()
5165
redis, mok = redisMap[address]
5266
mapMutex.RUnlock()
5367
if !mok {
54-
redis = &RedisClient{Address: address, pool: newPool(address)}
68+
redis = &RedisClient{Address: address, pool: newPool(address, maxIdle, maxActive)}
5569
mapMutex.Lock()
5670
redisMap[address] = redis
5771
mapMutex.Unlock()

server.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,8 @@ func (server *HttpServer) SetSessionConfig(storeConfig *session.StoreConfig) {
218218
server.SessionConfig().BackupServerUrl = storeConfig.BackupServerUrl
219219
server.SessionConfig().StoreKeyPre = storeConfig.StoreKeyPre
220220
server.SessionConfig().CookieName = storeConfig.CookieName
221+
server.SessionConfig().MaxIdle = storeConfig.MaxIdle
222+
server.SessionConfig().MaxActive = storeConfig.MaxActive
221223
server.DotApp.Logger().Debug("DotWeb:HttpServer SetSessionConfig ["+jsonutil.GetJsonString(storeConfig)+"]", LogTarget_HttpServer)
222224
}
223225

@@ -229,6 +231,8 @@ func (server *HttpServer) InitSessionManager() {
229231
storeConfig.ServerIP = server.SessionConfig().ServerIP
230232
storeConfig.BackupServerUrl = server.SessionConfig().BackupServerUrl
231233
storeConfig.StoreKeyPre = server.SessionConfig().StoreKeyPre
234+
storeConfig.MaxIdle = server.SessionConfig().MaxIdle
235+
storeConfig.MaxActive = server.SessionConfig().MaxActive
232236
storeConfig.CookieName = server.SessionConfig().CookieName
233237

234238
if server.sessionManager == nil {

session/session.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ type (
4040
ServerIP string // if use redis, connection string, like "redis://:[email protected]:6379/0"
4141
BackupServerUrl string // if use redis, if ServerIP is down, use this server, like "redis://:[email protected]:6379/0"
4242
StoreKeyPre string // if use redis, set custom redis key-pre; default is dotweb:session:
43+
MaxIdle int // if use redis, set MaxIdle; default is 10
44+
MaxActive int // if use redis, set MaxActive; default is 50
4345
}
4446

4547
SessionManager struct {
@@ -71,27 +73,29 @@ func GetSessionStore(config *StoreConfig) SessionStore {
7173

7274
// NewDefaultRuntimeConfig create new store with default config and use runtime store
7375
func NewDefaultRuntimeConfig() *StoreConfig {
74-
return NewStoreConfig(SessionMode_Runtime, DefaultSessionMaxLifeTime, "", "")
76+
return NewStoreConfig(SessionMode_Runtime, DefaultSessionMaxLifeTime, "", "", 0, 0)
7577
}
7678

7779
// NewDefaultRedisConfig create new store with default config and use redis store
7880
func NewDefaultRedisConfig(serverIp string) *StoreConfig {
79-
return NewStoreConfig(SessionMode_Redis, DefaultSessionMaxLifeTime, serverIp, "")
81+
return NewRedisConfig(serverIp, DefaultSessionMaxLifeTime, "", 0, 0)
8082
}
8183

8284
// NewRedisConfig create new store with config and use redis store
8385
// must set serverIp and storeKeyPre
84-
func NewRedisConfig(serverIp string, storeKeyPre string) *StoreConfig {
85-
return NewStoreConfig(SessionMode_Redis, DefaultSessionMaxLifeTime, serverIp, storeKeyPre)
86+
func NewRedisConfig(serverIp string, maxlifetime int64, storeKeyPre string, maxIdle int, maxActive int) *StoreConfig {
87+
return NewStoreConfig(SessionMode_Redis, maxlifetime, serverIp, storeKeyPre, maxIdle, maxActive)
8688
}
8789

8890
// NewStoreConfig create new store config
89-
func NewStoreConfig(storeName string, maxlifetime int64, serverIp string, storeKeyPre string) *StoreConfig {
91+
func NewStoreConfig(storeName string, maxlifetime int64, serverIp string, storeKeyPre string, maxIdle int, maxActive int) *StoreConfig {
9092
return &StoreConfig{
9193
StoreName: storeName,
9294
Maxlifetime: maxlifetime,
9395
ServerIP: serverIp,
9496
StoreKeyPre: storeKeyPre,
97+
MaxIdle: maxIdle,
98+
MaxActive: maxActive,
9599
}
96100
}
97101

session/store_redis.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ type RedisStore struct {
2323
serverIp string // connection string, like "redis://:[email protected]:6379/0"
2424
backupServerUrl string // backup connection string, like "redis://:[email protected]:6379/0"
2525
storeKeyPre string // set custom redis key-pre; default is dotweb:session:
26+
maxIdle int // set MaxIdle; default is 10
27+
maxActive int // set MaxActive; default is 20
2628
}
2729

2830
// create new redis store
@@ -32,6 +34,8 @@ func NewRedisStore(config *StoreConfig) (*RedisStore, error) {
3234
serverIp: config.ServerIP,
3335
backupServerUrl: config.BackupServerUrl,
3436
maxlifetime: config.Maxlifetime,
37+
maxIdle: config.MaxIdle,
38+
maxActive: config.MaxActive,
3539
}
3640
store.hystrix = hystrix.NewHystrix(store.checkRedisAlive, nil)
3741
store.hystrix.SetMaxFailedNumber(HystrixErrorCount)
@@ -134,7 +138,7 @@ func (store *RedisStore) SessionUpdate(state *SessionState) error {
134138

135139
// SessionRemove delete session state in store
136140
func (store *RedisStore) SessionRemove(sessionId string) error {
137-
redisClient := redisutil.GetRedisClient(store.serverIp)
141+
redisClient := redisutil.GetRedisClient(store.serverIp, store.maxIdle, store.maxActive)
138142
key := store.getRedisKey(sessionId)
139143
_, err := redisClient.Del(key)
140144
if store.checkConnErrorAndNeedRetry(err) {
@@ -166,11 +170,11 @@ func (store *RedisStore) getRedisClient() *redisutil.RedisClient {
166170
}
167171

168172
func (store *RedisStore) getDefaultRedis() *redisutil.RedisClient {
169-
return redisutil.GetRedisClient(store.serverIp)
173+
return redisutil.GetRedisClient(store.serverIp, store.maxIdle, store.maxActive)
170174
}
171175

172176
func (store *RedisStore) getBackupRedis() *redisutil.RedisClient {
173-
return redisutil.GetRedisClient(store.backupServerUrl)
177+
return redisutil.GetRedisClient(store.backupServerUrl, store.maxIdle, store.maxActive)
174178
}
175179

176180
// checkConnErrorAndNeedRetry check err is Conn error and is need to retry

version.MD

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
## dotweb版本记录:
22

3+
#### Version 1.7.14
4+
* fix: fixed can not set redis maxIdle & maxActive when use redis session, fix for issue #236
5+
* refactor: add StoreConfig.MaxIdle & StoreConfig.MaxActive set redis maxIdle & maxActive
6+
* refactor: add redisutil.GetDefaultRedisClient to returns the RedisClient of specified address
7+
* refactor: update redisutil.GetRedisClient returns the RedisClient of specified address & maxIdle & maxActive
8+
* opt: set defaultMaxIdle=10, defaultMaxActive=50 when use default redis config
9+
* How to set redis maxIdle & maxActive when use redis session:
10+
~~~ go
11+
sessionConf := session.NewDefaultRedisConfig("redis://xx.xx.xx.xx:6379/0")
12+
sessionConf.BackupServerUrl = "redis://xx.xx.xx.xx:6379/0"
13+
sessionConf.CookieName = "dotweb-example.SessionID"
14+
sessionConf.MaxIdle = 20
15+
sessionConf.MaxActive = 100
16+
~~~
17+
* 2020-12-19 21:00 at ShangHai
318

419
#### Version 1.7.13
520
* fix: fixed can not get correct Path which in Post requests

0 commit comments

Comments
 (0)