🍱 update dependencies redisclient to reduce memory consumption (#105)

* 🍱 update dependencies to avoid memory leak

* 🍱 update dep
This commit is contained in:
maxlerebourg
2023-06-20 09:22:51 +02:00
committed by GitHub
parent 0c2668d578
commit 93340ffa55
4 changed files with 16 additions and 19 deletions
+12 -15
View File
@@ -18,6 +18,7 @@ const (
RedisMiss = "redis:miss"
RedisTimeout = "redis:timeout"
RedisNoAuth = "redis:noauth"
RedisIssue = "redis:issue?"
)
// A redisCmd is used to communicate with redis at low level using commands.
@@ -70,12 +71,11 @@ func (sr *SimpleRedis) waitRedis(reader *textproto.Reader, channel chan redisCmd
}
}
func (sr *SimpleRedis) askRedis(cmd redisCmd, channel chan redisCmd) {
func (sr *SimpleRedis) askRedis(cmd redisCmd, channel chan redisCmd) redisCmd {
dialer := net.Dialer{Timeout: 2 * time.Second}
conn, err := dialer.Dial("tcp", sr.host)
if err != nil {
channel <- redisCmd{Error: fmt.Errorf(RedisUnreachable)}
return
return redisCmd{Error: fmt.Errorf(RedisUnreachable)}
}
defer func() {
if err := conn.Close(); err != nil {
@@ -111,24 +111,22 @@ func (sr *SimpleRedis) askRedis(cmd redisCmd, channel chan redisCmd) {
for {
select {
case <-time.After(time.Second * 1):
channel <- redisCmd{Error: fmt.Errorf(RedisTimeout)}
return
return redisCmd{Error: fmt.Errorf(RedisTimeout)}
default:
read, _ := reader.ReadLineBytes()
str := string(read)
if strings.Contains(str, "-NOAUTH") {
channel <- redisCmd{Error: fmt.Errorf(RedisNoAuth)}
return
return redisCmd{Error: fmt.Errorf(RedisNoAuth)}
} else if str != "$1" {
channel <- redisCmd{Error: fmt.Errorf(RedisMiss)}
return
return redisCmd{Error: fmt.Errorf(RedisMiss)}
}
read, _ = reader.ReadLineBytes()
channel <- redisCmd{Data: read}
return
return redisCmd{Data: read}
}
}
}
return redisCmd{Error: fmt.Errorf(RedisIssue)}
}
// Init sets the redisHost used to connect to redis.
@@ -145,8 +143,7 @@ func (sr *SimpleRedis) Get(name string) ([]byte, error) {
Name: name,
}
channel := make(chan redisCmd)
go sr.askRedis(cmd, channel)
resp := <-channel
resp := sr.askRedis(cmd, channel)
if resp.Error != nil {
return nil, resp.Error
}
@@ -161,7 +158,7 @@ func (sr *SimpleRedis) Set(name string, data []byte, duration int64) error {
Data: data,
Duration: duration,
}
go sr.askRedis(cmd, nil)
sr.askRedis(cmd, nil)
return nil
}
@@ -171,6 +168,6 @@ func (sr *SimpleRedis) Del(name string) error {
Command: "DEL",
Name: name,
}
go sr.askRedis(cmd, nil)
sr.askRedis(cmd, nil)
return nil
}
+1 -1
View File
@@ -1,6 +1,6 @@
# github.com/leprosus/golang-ttl-map v1.1.7
## explicit; go 1.15
github.com/leprosus/golang-ttl-map
# github.com/maxlerebourg/simpleredis v1.0.7
# github.com/maxlerebourg/simpleredis v1.0.9
## explicit; go 1.19
github.com/maxlerebourg/simpleredis