Skip to content

Commit 0ecb5f4

Browse files
committed
fix redis incr ttl
1 parent eb9e3b8 commit 0ecb5f4

2 files changed

Lines changed: 41 additions & 20 deletions

File tree

connect/transport_rate_limit.go

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"fmt"
77
"time"
88

9+
"github.com/redis/go-redis/v9"
10+
911
"github.com/urnetwork/glog"
1012

1113
"github.com/urnetwork/server"
@@ -111,25 +113,26 @@ func (self *ConnectionRateLimit) Connect() (err error, disconnect func()) {
111113
var burstCount int64
112114
var totalCount int64
113115
server.Redis(self.ctx, func(r server.RedisClient) {
114-
burstCount, err = r.Incr(self.ctx, burstKey).Result()
116+
var burstCmd *redis.IntCmd
117+
var totalCmd *redis.IntCmd
118+
r.TxPipelined(self.ctx, func(pipe redis.Pipeliner) error {
119+
burstCmd = pipe.Incr(self.ctx, burstKey)
120+
pipe.Expire(self.ctx, burstKey, self.settings.BurstDuration)
121+
122+
totalCmd = pipe.Incr(self.ctx, totalKey)
123+
pipe.Expire(self.ctx, totalKey, self.settings.TotalExpiration)
124+
125+
return nil
126+
})
127+
burstCount, err = burstCmd.Result()
115128
if err != nil {
116129
return
117130
}
118-
if burstCount == 1 {
119-
// FIXME put this in an atomic lua script
120-
// for now it doesnt matter if a few keys linger
121-
r.Expire(self.ctx, burstKey, self.settings.BurstDuration).Err()
122-
}
123-
totalCount, err = r.Incr(self.ctx, totalKey).Result()
131+
totalCount, err = totalCmd.Result()
124132
if err != nil {
125133
return
126134
}
127135
totalIncremented = true
128-
if totalCount == 1 {
129-
// FIXME put this in an atomic lua script
130-
// for now it doesnt matter if a few keys linger
131-
r.Expire(self.ctx, totalKey, self.settings.TotalExpiration).Err()
132-
}
133136
})
134137
if err != nil {
135138
return

model/network_client_model.go

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import (
1919
// "github.com/twmb/murmur3"
2020
"golang.org/x/exp/maps"
2121

22+
"github.com/redis/go-redis/v9"
23+
2224
"github.com/urnetwork/glog"
2325

2426
"github.com/urnetwork/server"
@@ -1321,33 +1323,49 @@ func ClientError(ctx context.Context, networkId server.Id, clientId server.Id, c
13211323

13221324
server.Redis(ctx, func(r server.RedisClient) {
13231325

1324-
networkCount, err := r.Incr(ctx, networkKey).Result()
1326+
var networkCountCmd *redis.IntCmd
1327+
var clientCountCmd *redis.IntCmd
1328+
var networkErrorMessageCountCmd *redis.IntCmd
1329+
var clientErrorMessageCountCmd *redis.IntCmd
1330+
r.TxPipelined(ctx, func(pipe redis.Pipeliner) error {
1331+
networkCountCmd = pipe.Incr(ctx, networkKey)
1332+
pipe.Expire(ctx, networkKey, ttl)
1333+
1334+
clientCountCmd = pipe.Incr(ctx, clientKey)
1335+
pipe.Expire(ctx, clientKey, ttl)
1336+
1337+
networkErrorMessageCountCmd = pipe.Incr(ctx, networkErrorMessageKey)
1338+
pipe.Expire(ctx, networkErrorMessageKey, ttl)
1339+
1340+
clientErrorMessageCountCmd = pipe.Incr(ctx, clientErrorMessageKey)
1341+
pipe.Expire(ctx, clientErrorMessageKey, ttl)
1342+
1343+
return nil
1344+
})
1345+
1346+
networkCount, err := networkCountCmd.Result()
13251347
if err == nil {
1326-
r.Expire(ctx, networkKey, ttl)
13271348
if networkCount%warnThreshold == 0 {
13281349
glog.V(1).Infof("[ncm][%s]network has a significant amount of connection errors (%d)\n", networkId, networkCount)
13291350
}
13301351
}
13311352

1332-
clientCount, err := r.Incr(ctx, clientKey).Result()
1353+
clientCount, err := clientCountCmd.Result()
13331354
if err == nil {
1334-
r.Expire(ctx, clientKey, ttl)
13351355
if clientCount%warnThreshold == 0 {
13361356
glog.V(1).Infof("[ncm][%s]client has a significant amount of connection errors (%d)\n", clientId, clientCount)
13371357
}
13381358
}
13391359

1340-
networkErrorMessageCount, err := r.Incr(ctx, networkErrorMessageKey).Result()
1360+
networkErrorMessageCount, err := networkErrorMessageCountCmd.Result()
13411361
if err == nil {
1342-
r.Expire(ctx, networkErrorMessageKey, ttl)
13431362
if networkErrorMessageCount%warnThreshold == 0 {
13441363
glog.V(1).Infof("[ncm][%s]network has a significant count of connection error message (%d): %s\n", networkId, networkErrorMessageCount, errorMessage)
13451364
}
13461365
}
13471366

1348-
clientErrorMessageCount, err := r.Incr(ctx, clientErrorMessageKey).Result()
1367+
clientErrorMessageCount, err := clientErrorMessageCountCmd.Result()
13491368
if err == nil {
1350-
r.Expire(ctx, clientErrorMessageKey, ttl)
13511369
if clientErrorMessageCount%warnThreshold == 0 {
13521370
glog.V(1).Infof("[ncm][%s]client has a significant count of connection error message (%d): %s\n", clientId, clientErrorMessageCount, errorMessage)
13531371
}

0 commit comments

Comments
 (0)