🍱 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
+1 -1
View File
@@ -4,5 +4,5 @@ go 1.19
require ( require (
github.com/leprosus/golang-ttl-map v1.1.7 github.com/leprosus/golang-ttl-map v1.1.7
github.com/maxlerebourg/simpleredis v1.0.7 github.com/maxlerebourg/simpleredis v1.0.9
) )
+2 -2
View File
@@ -1,4 +1,4 @@
github.com/leprosus/golang-ttl-map v1.1.7 h1:cF4AAFDDnJTFSV+/42sKLhmMluvLdRlCGS2UaifH6UM= github.com/leprosus/golang-ttl-map v1.1.7 h1:cF4AAFDDnJTFSV+/42sKLhmMluvLdRlCGS2UaifH6UM=
github.com/leprosus/golang-ttl-map v1.1.7/go.mod h1:4QWHJPeVBbrkhOhXdhCv9IEiyj/YzkO04/iexy4vSe0= github.com/leprosus/golang-ttl-map v1.1.7/go.mod h1:4QWHJPeVBbrkhOhXdhCv9IEiyj/YzkO04/iexy4vSe0=
github.com/maxlerebourg/simpleredis v1.0.7 h1:d53p3GOIgQtxxWuqsWMGTJ0dZjP8UhiDX8L1q2QZ/vY= github.com/maxlerebourg/simpleredis v1.0.9 h1:aj1hKaYPeOVE4Ksu3TV/zsreUDDWOpKXBAvoFysiqII=
github.com/maxlerebourg/simpleredis v1.0.7/go.mod h1:/DH8zOK6kDskSqoX/m5CJJdNGfkIQZd/ERBJgytDDSk= github.com/maxlerebourg/simpleredis v1.0.9/go.mod h1:/DH8zOK6kDskSqoX/m5CJJdNGfkIQZd/ERBJgytDDSk=
+12 -15
View File
@@ -18,6 +18,7 @@ const (
RedisMiss = "redis:miss" RedisMiss = "redis:miss"
RedisTimeout = "redis:timeout" RedisTimeout = "redis:timeout"
RedisNoAuth = "redis:noauth" RedisNoAuth = "redis:noauth"
RedisIssue = "redis:issue?"
) )
// A redisCmd is used to communicate with redis at low level using commands. // 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} dialer := net.Dialer{Timeout: 2 * time.Second}
conn, err := dialer.Dial("tcp", sr.host) conn, err := dialer.Dial("tcp", sr.host)
if err != nil { if err != nil {
channel <- redisCmd{Error: fmt.Errorf(RedisUnreachable)} return redisCmd{Error: fmt.Errorf(RedisUnreachable)}
return
} }
defer func() { defer func() {
if err := conn.Close(); err != nil { if err := conn.Close(); err != nil {
@@ -111,24 +111,22 @@ func (sr *SimpleRedis) askRedis(cmd redisCmd, channel chan redisCmd) {
for { for {
select { select {
case <-time.After(time.Second * 1): case <-time.After(time.Second * 1):
channel <- redisCmd{Error: fmt.Errorf(RedisTimeout)} return redisCmd{Error: fmt.Errorf(RedisTimeout)}
return
default: default:
read, _ := reader.ReadLineBytes() read, _ := reader.ReadLineBytes()
str := string(read) str := string(read)
if strings.Contains(str, "-NOAUTH") { if strings.Contains(str, "-NOAUTH") {
channel <- redisCmd{Error: fmt.Errorf(RedisNoAuth)} return redisCmd{Error: fmt.Errorf(RedisNoAuth)}
return
} else if str != "$1" { } else if str != "$1" {
channel <- redisCmd{Error: fmt.Errorf(RedisMiss)} return redisCmd{Error: fmt.Errorf(RedisMiss)}
return
} }
read, _ = reader.ReadLineBytes() read, _ = reader.ReadLineBytes()
channel <- redisCmd{Data: read} return redisCmd{Data: read}
return
} }
} }
} }
return redisCmd{Error: fmt.Errorf(RedisIssue)}
} }
// Init sets the redisHost used to connect to redis. // Init sets the redisHost used to connect to redis.
@@ -145,8 +143,7 @@ func (sr *SimpleRedis) Get(name string) ([]byte, error) {
Name: name, Name: name,
} }
channel := make(chan redisCmd) channel := make(chan redisCmd)
go sr.askRedis(cmd, channel) resp := sr.askRedis(cmd, channel)
resp := <-channel
if resp.Error != nil { if resp.Error != nil {
return nil, resp.Error return nil, resp.Error
} }
@@ -161,7 +158,7 @@ func (sr *SimpleRedis) Set(name string, data []byte, duration int64) error {
Data: data, Data: data,
Duration: duration, Duration: duration,
} }
go sr.askRedis(cmd, nil) sr.askRedis(cmd, nil)
return nil return nil
} }
@@ -171,6 +168,6 @@ func (sr *SimpleRedis) Del(name string) error {
Command: "DEL", Command: "DEL",
Name: name, Name: name,
} }
go sr.askRedis(cmd, nil) sr.askRedis(cmd, nil)
return nil return nil
} }
+1 -1
View File
@@ -1,6 +1,6 @@
# github.com/leprosus/golang-ttl-map v1.1.7 # github.com/leprosus/golang-ttl-map v1.1.7
## explicit; go 1.15 ## explicit; go 1.15
github.com/leprosus/golang-ttl-map github.com/leprosus/golang-ttl-map
# github.com/maxlerebourg/simpleredis v1.0.7 # github.com/maxlerebourg/simpleredis v1.0.9
## explicit; go 1.19 ## explicit; go 1.19
github.com/maxlerebourg/simpleredis github.com/maxlerebourg/simpleredis