diff --git a/.example/other/test.go b/.example/other/test.go index 267409019..84f83a83d 100644 --- a/.example/other/test.go +++ b/.example/other/test.go @@ -1,54 +1,35 @@ package main import ( - "fmt" - "github.com/gogf/gf/container/gvar" - "github.com/gogf/gf/encoding/gparser" + "github.com/gogf/gf/frame/g" + "github.com/gogf/gf/os/glog" "github.com/gogf/gf/os/gtime" + "github.com/gogf/gf/os/gtimer" + "time" ) -type EntityTest struct { - ID int64 - DeviceID string - DeviceType uint8 - ProtoVer uint8 - DataType uint8 - RecordTime *gtime.Time - CreateTime *gtime.Time - RemoteIP string - Voltage int16 - Battery uint8 - CellularVersion uint8 - StateID uint16 - OperatorID uint16 - RegionID uint16 - BaseStationID uint32 - BaseStationSignalStrength int8 - ContainerStatus int8 - PositionX int16 - PositionY int16 - PositionZ int16 - ContainerID string - NetworkRegisterTime int16 - Temperature int8 - Humidity uint8 - SleepMode uint8 - CellularChangeStatus int8 - GPSSearchTime int16 - Latitude float64 - Longitude float64 - Altitude int16 - Speed uint8 - DateStr string - TimeStr string - FrameID uint16 +func GetList() { +START: + for { + res, err := g.Redis().DoVar("RPOP", "mill") + if err != nil { + glog.Debug("Rpop:", err) + break + } + glog.Debug(res) + if res.IsEmpty() { + glog.Debug("nil") + continue START + } + interval := 50 * time.Second + gtimer.AddOnce(interval, func() { + glog.Debug("end------:", res, gtime.Now().Format("Y-m-d H:i:s")) + }) + } } func main() { - array := []string{"DeviceID", "8134567890ABCDEF", "DeviceType", "16", "ProtoVer", "11", "DataType", "2", "RecordTime", "2020-02-27 15:23:23", "CreateTime", "2020-03-09 19:24:22", "RemoteIP", "127.0.0.1:60471", "Voltage", "3610", "Battery", "90", "CellularVersion", "4", "StateID", "401", "OperatorID", "345", "RegionID", "17716", "BaseStationID", "571479331", "BaseStationSignalStrength", "45", "ContainerStatus", "1", "PositionX", "40", "PositionY", "-25", "PositionZ", "16", "ContainerID", "aAvVDEFPQAA", "DateStr", "270220", "TimeStr", "232323", "NetworkRegisterTime", "60", "Temperature", "25", "Humidity", "70", "SleepMode", "1", "CellularChangeStatus", "15", "GPSSearchTime", "32767", "Latitude", "116.23532", "Longitude", "39.21526", "Altitude", "2800", "Speed", "66", "FrameID", "4660"} - v := gvar.New(array) - o := &EntityTest{} - v.Struct(o) - //b, _ := json.Marshal(o) - fmt.Println(gparser.MustToJsonIndentString(o)) + g.Redis().SetMaxActive(2) + //g.Redis().SetMaxIdle(100) + GetList() } diff --git a/database/gredis/gredis.go b/database/gredis/gredis.go index 6bd31378a..f6828d6d2 100644 --- a/database/gredis/gredis.go +++ b/database/gredis/gredis.go @@ -41,10 +41,10 @@ type Config struct { Port int Db int Pass string // Password for AUTH. - MaxIdle int // Maximum number of connections allowed to be idle (default is 0 means no idle connection) - MaxActive int // Maximum number of connections limit (default is 0 means no limit) - IdleTimeout time.Duration // Maximum idle time for connection (default is 60 seconds, not allowed to be set to 0) - MaxConnLifetime time.Duration // Maximum lifetime of the connection (default is 60 seconds, not allowed to be set to 0) + MaxIdle int // Maximum number of connections allowed to be idle (default is 10) + MaxActive int // Maximum number of connections limit (default is 0 means no limit). + IdleTimeout time.Duration // Maximum idle time for connection (default is 10 seconds, not allowed to be set to 0) + MaxConnLifetime time.Duration // Maximum lifetime of the connection (default is 30 seconds, not allowed to be set to 0) ConnectTimeout time.Duration // Dial connection timeout. } @@ -54,8 +54,9 @@ type PoolStats struct { } const ( - gDEFAULT_POOL_IDLE_TIMEOUT = 30 * time.Second + gDEFAULT_POOL_IDLE_TIMEOUT = 10 * time.Second gDEFAULT_POOL_CONN_TIMEOUT = 10 * time.Second + gDEFAULT_POOL_MAX_IDLE = 10 gDEFAULT_POOL_MAX_LIFE_TIME = 30 * time.Second ) @@ -67,6 +68,12 @@ var ( // New creates a redis client object with given configuration. // Redis client maintains a connection pool automatically. func New(config Config) *Redis { + // The MaxIdle is the most important attribute of the connection pool. + // Only if this attribute is set, the created connections from client + // can not exceed the limit of the server. + if config.MaxIdle == 0 { + config.MaxIdle = gDEFAULT_POOL_MAX_IDLE + } if config.IdleTimeout == 0 { config.IdleTimeout = gDEFAULT_POOL_IDLE_TIMEOUT } @@ -132,7 +139,8 @@ func NewFromStr(str string) (*Redis, error) { // It is not necessary to call Close manually. func (r *Redis) Close() error { if r.group != "" { - // If it is an instance object, it needs to remove it from the instance Map. + // If it is an instance object, + // it needs to remove it from the instance Map. instances.Remove(r.group) } pools.Remove(fmt.Sprintf("%v", r.config)) @@ -151,22 +159,31 @@ func (r *Redis) GetConn() *Conn { return r.Conn() } -// SetMaxIdle sets the MaxIdle attribute of the connection pool. +// SetMaxIdle sets the maximum number of idle connections in the pool. func (r *Redis) SetMaxIdle(value int) { r.pool.MaxIdle = value } -// SetMaxActive sets the MaxActive attribute of the connection pool. +// SetMaxActive sets the maximum number of connections allocated by the pool at a given time. +// When zero, there is no limit on the number of connections in the pool. +// +// Note that if the pool is at the MaxActive limit, then all the operations will wait for +// a connection to be returned to the pool before returning. func (r *Redis) SetMaxActive(value int) { r.pool.MaxActive = value } // SetIdleTimeout sets the IdleTimeout attribute of the connection pool. +// It closes connections after remaining idle for this duration. If the value +// is zero, then idle connections are not closed. Applications should set +// the timeout to a value less than the server's timeout. func (r *Redis) SetIdleTimeout(value time.Duration) { r.pool.IdleTimeout = value } // SetMaxConnLifetime sets the MaxConnLifetime attribute of the connection pool. +// It closes connections older than this duration. If the value is zero, then +// the pool does not close connections based on age. func (r *Redis) SetMaxConnLifetime(value time.Duration) { r.pool.MaxConnLifetime = value }