1
0
mirror of https://github.com/astaxie/beego.git synced 2024-06-28 09:24:13 +00:00

Merge pull request #3239 from wilhelmguo/develop

add session redis IdleTimeout config
This commit is contained in:
astaxie 2018-07-20 14:44:07 +08:00 committed by GitHub
commit f15732798f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -37,6 +37,7 @@ import (
"strconv" "strconv"
"strings" "strings"
"sync" "sync"
"time"
"github.com/astaxie/beego/session" "github.com/astaxie/beego/session"
@ -118,8 +119,8 @@ type Provider struct {
} }
// SessionInit init redis session // SessionInit init redis session
// savepath like redis server addr,pool size,password,dbnum // savepath like redis server addr,pool size,password,dbnum,IdleTimeout second
// e.g. 127.0.0.1:6379,100,astaxie,0 // e.g. 127.0.0.1:6379,100,astaxie,0,30
func (rp *Provider) SessionInit(maxlifetime int64, savePath string) error { func (rp *Provider) SessionInit(maxlifetime int64, savePath string) error {
rp.maxlifetime = maxlifetime rp.maxlifetime = maxlifetime
configs := strings.Split(savePath, ",") configs := strings.Split(savePath, ",")
@ -149,6 +150,13 @@ func (rp *Provider) SessionInit(maxlifetime int64, savePath string) error {
} else { } else {
rp.dbNum = 0 rp.dbNum = 0
} }
var idleTimeout time.Duration = 0
if len(configs) > 4 {
timeout, err := strconv.Atoi(configs[4])
if err == nil && timeout > 0 {
idleTimeout = time.Duration(timeout) * time.Second
}
}
rp.poollist = &redis.Pool{ rp.poollist = &redis.Pool{
Dial: func() (redis.Conn, error) { Dial: func() (redis.Conn, error) {
c, err := redis.Dial("tcp", rp.savePath) c, err := redis.Dial("tcp", rp.savePath)
@ -171,9 +179,11 @@ func (rp *Provider) SessionInit(maxlifetime int64, savePath string) error {
} }
return c, err return c, err
}, },
MaxIdle: rp.poolsize, MaxIdle: rp.poolsize,
} }
rp.poollist.IdleTimeout = idleTimeout
return rp.poollist.Get().Err() return rp.poollist.Get().Err()
} }