Skip to content

Commit f426952

Browse files
fscnickrueian
andauthored
feat: reduce size of redis message (#810)
* feat: reduce size of redis message Signed-off-by: fscnick <[email protected]> * chore: fix comment and remove redundant lines Signed-off-by: fscnick <[email protected]> * fix: mock package Signed-off-by: fscnick <[email protected]> * fix: rename to slicemsg and strmsg Signed-off-by: fscnick <[email protected]> * fix: rename integer to intlen and change read function return Signed-off-by: fscnick <[email protected]> * fix: compare nil instead Signed-off-by: fscnick <[email protected]> * fix: remove redundant code Signed-off-by: fscnick <[email protected]> * chore: remove redundant code Signed-off-by: Rueian <[email protected]> --------- Signed-off-by: fscnick <[email protected]> Signed-off-by: Rueian <[email protected]> Co-authored-by: Rueian <[email protected]>
1 parent b9d950c commit f426952

21 files changed

+3421
-3274
lines changed

cache.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ func (a *adapter) Delete(keys []RedisMessage) {
137137
}
138138
} else {
139139
for _, k := range keys {
140-
a.del(k.string)
140+
a.del(k.string())
141141
}
142142
}
143143
a.mu.Unlock()

cache_test.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -23,36 +23,36 @@ func test(t *testing.T, storeFn func() CacheStore) {
2323
t.Fatal("flights before Update should return empty RedisMessage and non-nil CacheEntry")
2424
}
2525

26-
store.Delete([]RedisMessage{{typ: '+', string: "key"}}) // Delete should not affect pending CacheEntry
26+
store.Delete([]RedisMessage{strmsg('+', "key")}) // Delete should not affect pending CacheEntry
2727

2828
v2, e2 := store.Flight("key", "cmd", time.Millisecond*100, now)
2929
if v2.typ != 0 || e != e2 {
3030
t.Fatal("flights before Update should return empty RedisMessage and the same CacheEntry, not be affected by Delete")
3131
}
3232

33-
v = RedisMessage{typ: '+', string: "val"}
33+
v = strmsg('+', "val")
3434
v.setExpireAt(now.Add(time.Second).UnixMilli())
3535
if pttl := store.Update("key", "cmd", v); pttl < now.Add(90*time.Millisecond).UnixMilli() || pttl > now.Add(100*time.Millisecond).UnixMilli() {
3636
t.Fatal("Update should return a desired pttl")
3737
}
3838

3939
v2, err = e.Wait(context.Background())
40-
if v2.typ != v.typ || v2.string != v.string || err != nil {
40+
if v2.typ != v.typ || v2.string() != v.string() || err != nil {
4141
t.Fatal("unexpected cache response")
4242
}
4343
if pttl := v2.CachePXAT(); pttl < now.Add(90*time.Millisecond).UnixMilli() || pttl > now.Add(100*time.Millisecond).UnixMilli() {
4444
t.Fatal("CachePXAT should return a desired pttl")
4545
}
4646

4747
v2, _ = store.Flight("key", "cmd", time.Millisecond*100, now)
48-
if v2.typ != v.typ || v2.string != v.string {
48+
if v2.typ != v.typ || v2.string() != v.string() {
4949
t.Fatal("flights after Update should return updated RedisMessage")
5050
}
5151
if pttl := v2.CachePXAT(); pttl < now.Add(90*time.Millisecond).UnixMilli() || pttl > now.Add(100*time.Millisecond).UnixMilli() {
5252
t.Fatal("CachePXAT should return a desired pttl")
5353
}
5454

55-
store.Delete([]RedisMessage{{typ: '+', string: "key"}})
55+
store.Delete([]RedisMessage{strmsg('+', "key")})
5656
v, e = store.Flight("key", "cmd", time.Millisecond*100, now)
5757
if v.typ != 0 || e != nil {
5858
t.Fatal("flights after Delete should return empty RedisMessage and nil CacheEntry")
@@ -74,7 +74,7 @@ func test(t *testing.T, storeFn func() CacheStore) {
7474
t.Fatal("flights before Update should return empty RedisMessage and non-nil CacheEntry")
7575
}
7676

77-
store.Delete([]RedisMessage{{typ: '+', string: "key"}}) // Delete should not affect pending CacheEntry
77+
store.Delete([]RedisMessage{strmsg('+', "key")}) // Delete should not affect pending CacheEntry
7878

7979
v2, e2 := store.Flight("key", "cmd", time.Millisecond*100, now)
8080
if v2.typ != 0 || e != e2 {
@@ -99,13 +99,13 @@ func test(t *testing.T, storeFn func() CacheStore) {
9999
var store = storeFn()
100100

101101
for _, deletions := range [][]RedisMessage{
102-
{{typ: '+', string: "key"}},
102+
{strmsg('+', "key")},
103103
nil,
104104
} {
105105
store.Flight("key", "cmd1", time.Millisecond*100, now)
106106
store.Flight("key", "cmd2", time.Millisecond*100, now)
107-
store.Update("key", "cmd1", RedisMessage{typ: '+', string: "val"})
108-
store.Update("key", "cmd2", RedisMessage{typ: '+', string: "val"})
107+
store.Update("key", "cmd1", strmsg('+', "val"))
108+
store.Update("key", "cmd2", strmsg('+', "val"))
109109

110110
store.Delete(deletions)
111111

@@ -128,7 +128,7 @@ func test(t *testing.T, storeFn func() CacheStore) {
128128
t.Fatal("first flight should return empty RedisMessage and nil CacheEntry")
129129
}
130130

131-
v = RedisMessage{typ: '+', string: "val"}
131+
v = strmsg('+', "val")
132132
v.setExpireAt(now.Add(time.Millisecond).UnixMilli())
133133
store.Update("key", "cmd", v)
134134

client_test.go

+34-34
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ func TestSingleClient(t *testing.T) {
280280
if !reflect.DeepEqual(cmd.Commands(), c.Commands()) {
281281
t.Fatalf("unexpected command %v", cmd)
282282
}
283-
return newResult(RedisMessage{typ: '+', string: "Do"}, nil)
283+
return newResult(strmsg('+', "Do"), nil)
284284
}
285285
if v, err := client.Do(context.Background(), c).ToString(); err != nil || v != "Do" {
286286
t.Fatalf("unexpected response %v %v", v, err)
@@ -303,7 +303,7 @@ func TestSingleClient(t *testing.T) {
303303
if !reflect.DeepEqual(cmd[0].Commands(), c.Commands()) {
304304
t.Fatalf("unexpected command %v", cmd)
305305
}
306-
return &redisresults{s: []RedisResult{newResult(RedisMessage{typ: '+', string: "Do"}, nil)}}
306+
return &redisresults{s: []RedisResult{newResult(strmsg('+', "Do"), nil)}}
307307
}
308308
if len(client.DoMulti(context.Background())) != 0 {
309309
t.Fatalf("unexpected response length")
@@ -332,7 +332,7 @@ func TestSingleClient(t *testing.T) {
332332
if !reflect.DeepEqual(cmd.Commands(), c.Commands()) || ttl != 100 {
333333
t.Fatalf("unexpected command %v, %v", cmd, ttl)
334334
}
335-
return newResult(RedisMessage{typ: '+', string: "DoCache"}, nil)
335+
return newResult(strmsg('+', "DoCache"), nil)
336336
}
337337
if v, err := client.DoCache(context.Background(), c, 100).ToString(); err != nil || v != "DoCache" {
338338
t.Fatalf("unexpected response %v %v", v, err)
@@ -345,7 +345,7 @@ func TestSingleClient(t *testing.T) {
345345
if !reflect.DeepEqual(multi[0].Cmd.Commands(), c.Commands()) || multi[0].TTL != 100 {
346346
t.Fatalf("unexpected command %v, %v", multi[0].Cmd, multi[0].TTL)
347347
}
348-
return &redisresults{s: []RedisResult{newResult(RedisMessage{typ: '+', string: "DoCache"}, nil)}}
348+
return &redisresults{s: []RedisResult{newResult(strmsg('+', "DoCache"), nil)}}
349349
}
350350
if len(client.DoMultiCache(context.Background())) != 0 {
351351
t.Fatalf("unexpected response length")
@@ -419,10 +419,10 @@ func TestSingleClient(t *testing.T) {
419419
closed := false
420420
w := &mockWire{
421421
DoFn: func(cmd Completed) RedisResult {
422-
return newResult(RedisMessage{typ: '+', string: "Delegate"}, nil)
422+
return newResult(strmsg('+', "Delegate"), nil)
423423
},
424424
DoMultiFn: func(cmd ...Completed) *redisresults {
425-
return &redisresults{s: []RedisResult{newResult(RedisMessage{typ: '+', string: "Delegate"}, nil)}}
425+
return &redisresults{s: []RedisResult{newResult(strmsg('+', "Delegate"), nil)}}
426426
},
427427
ReceiveFn: func(ctx context.Context, subscribe Completed, fn func(message PubSubMessage)) error {
428428
return ErrClosing
@@ -485,10 +485,10 @@ func TestSingleClient(t *testing.T) {
485485
closed := false
486486
w := &mockWire{
487487
DoFn: func(cmd Completed) RedisResult {
488-
return newResult(RedisMessage{typ: '+', string: "Delegate"}, nil)
488+
return newResult(strmsg('+', "Delegate"), nil)
489489
},
490490
DoMultiFn: func(cmd ...Completed) *redisresults {
491-
return &redisresults{s: []RedisResult{newResult(RedisMessage{typ: '+', string: "Delegate"}, nil)}}
491+
return &redisresults{s: []RedisResult{newResult(strmsg('+', "Delegate"), nil)}}
492492
},
493493
ReceiveFn: func(ctx context.Context, subscribe Completed, fn func(message PubSubMessage)) error {
494494
return ErrClosing
@@ -688,7 +688,7 @@ func SetupClientRetry(t *testing.T, fn func(mock *mockConn) Client) {
688688
c, m := setup()
689689
m.DoFn = makeDoFn(
690690
newErrResult(ErrClosing),
691-
newResult(RedisMessage{typ: '+', string: "Do"}, nil),
691+
newResult(strmsg('+', "Do"), nil),
692692
)
693693
if v, err := c.Do(context.Background(), c.B().Get().Key("Do").Build()).ToString(); err != nil || v != "Do" {
694694
t.Fatalf("unexpected response %v %v", v, err)
@@ -739,7 +739,7 @@ func SetupClientRetry(t *testing.T, fn func(mock *mockConn) Client) {
739739
}
740740
m.DoFn = makeDoFn(
741741
newErrResult(ErrClosing),
742-
newResult(RedisMessage{typ: '+', string: "Do"}, nil),
742+
newResult(strmsg('+', "Do"), nil),
743743
)
744744
if v, err := c.Do(context.Background(), c.B().Get().Key("Do").Build()).ToString(); !errors.Is(err, ErrClosing) {
745745
t.Fatalf("unexpected response %v %v", v, err)
@@ -758,7 +758,7 @@ func SetupClientRetry(t *testing.T, fn func(mock *mockConn) Client) {
758758
c, m := setup()
759759
m.DoMultiFn = makeDoMultiFn(
760760
[]RedisResult{newErrResult(ErrClosing)},
761-
[]RedisResult{newResult(RedisMessage{typ: '+', string: "Do"}, nil)},
761+
[]RedisResult{newResult(strmsg('+', "Do"), nil)},
762762
)
763763
if v, err := c.DoMulti(context.Background(), c.B().Get().Key("Do").Build())[0].ToString(); err != nil || v != "Do" {
764764
t.Fatalf("unexpected response %v %v", v, err)
@@ -817,7 +817,7 @@ func SetupClientRetry(t *testing.T, fn func(mock *mockConn) Client) {
817817
}
818818
m.DoMultiFn = makeDoMultiFn(
819819
[]RedisResult{newErrResult(ErrClosing)},
820-
[]RedisResult{newResult(RedisMessage{typ: '+', string: "Do"}, nil)},
820+
[]RedisResult{newResult(strmsg('+', "Do"), nil)},
821821
)
822822
if v, err := c.DoMulti(context.Background(), c.B().Get().Key("Do").Build())[0].ToString(); !errors.Is(err, ErrClosing) {
823823
t.Fatalf("unexpected response %v %v", v, err)
@@ -836,7 +836,7 @@ func SetupClientRetry(t *testing.T, fn func(mock *mockConn) Client) {
836836
c, m := setup()
837837
m.DoCacheFn = makeDoCacheFn(
838838
newErrResult(ErrClosing),
839-
newResult(RedisMessage{typ: '+', string: "Do"}, nil),
839+
newResult(strmsg('+', "Do"), nil),
840840
)
841841
if v, err := c.DoCache(context.Background(), c.B().Get().Key("Do").Cache(), 0).ToString(); err != nil || v != "Do" {
842842
t.Fatalf("unexpected response %v %v", v, err)
@@ -887,7 +887,7 @@ func SetupClientRetry(t *testing.T, fn func(mock *mockConn) Client) {
887887
}
888888
m.DoCacheFn = makeDoCacheFn(
889889
newErrResult(ErrClosing),
890-
newResult(RedisMessage{typ: '+', string: "Do"}, nil),
890+
newResult(strmsg('+', "Do"), nil),
891891
)
892892
if v, err := c.DoCache(context.Background(), c.B().Get().Key("Do").Cache(), 0).ToString(); !errors.Is(err, ErrClosing) {
893893
t.Fatalf("unexpected response %v %v", v, err)
@@ -898,7 +898,7 @@ func SetupClientRetry(t *testing.T, fn func(mock *mockConn) Client) {
898898
c, m := setup()
899899
m.DoMultiCacheFn = makeDoMultiCacheFn(
900900
[]RedisResult{newErrResult(ErrClosing)},
901-
[]RedisResult{newResult(RedisMessage{typ: '+', string: "Do"}, nil)},
901+
[]RedisResult{newResult(strmsg('+', "Do"), nil)},
902902
)
903903
if v, err := c.DoMultiCache(context.Background(), CT(c.B().Get().Key("Do").Cache(), 0))[0].ToString(); err != nil || v != "Do" {
904904
t.Fatalf("unexpected response %v %v", v, err)
@@ -957,7 +957,7 @@ func SetupClientRetry(t *testing.T, fn func(mock *mockConn) Client) {
957957
}
958958
m.DoMultiCacheFn = makeDoMultiCacheFn(
959959
[]RedisResult{newErrResult(ErrClosing)},
960-
[]RedisResult{newResult(RedisMessage{typ: '+', string: "Do"}, nil)},
960+
[]RedisResult{newResult(strmsg('+', "Do"), nil)},
961961
)
962962
if v, err := c.DoMultiCache(context.Background(), CT(c.B().Get().Key("Do").Cache(), 0))[0].ToString(); !errors.Is(err, ErrClosing) {
963963
t.Fatalf("unexpected response %v %v", v, err)
@@ -1024,7 +1024,7 @@ func SetupClientRetry(t *testing.T, fn func(mock *mockConn) Client) {
10241024
c, m := setup()
10251025
m.DoFn = makeDoFn(
10261026
newErrResult(ErrClosing),
1027-
newResult(RedisMessage{typ: '+', string: "Do"}, nil),
1027+
newResult(strmsg('+', "Do"), nil),
10281028
)
10291029
m.AcquireFn = func() wire { return &mockWire{DoFn: m.DoFn} }
10301030
if ret := c.Dedicated(func(cc DedicatedClient) error {
@@ -1066,7 +1066,7 @@ func SetupClientRetry(t *testing.T, fn func(mock *mockConn) Client) {
10661066
c, m := setup()
10671067
m.DoFn = makeDoFn(
10681068
newErrResult(ErrClosing),
1069-
newResult(RedisMessage{typ: '+', string: "Do"}, nil),
1069+
newResult(strmsg('+', "Do"), nil),
10701070
)
10711071
m.AcquireFn = func() wire { return &mockWire{DoFn: m.DoFn} }
10721072
if ret := c.Dedicated(func(cc DedicatedClient) error {
@@ -1109,7 +1109,7 @@ func SetupClientRetry(t *testing.T, fn func(mock *mockConn) Client) {
11091109
c, m := setup()
11101110
m.DoMultiFn = makeDoMultiFn(
11111111
[]RedisResult{newErrResult(ErrClosing)},
1112-
[]RedisResult{newResult(RedisMessage{typ: '+', string: "Do"}, nil)},
1112+
[]RedisResult{newResult(strmsg('+', "Do"), nil)},
11131113
)
11141114
m.AcquireFn = func() wire { return &mockWire{DoMultiFn: m.DoMultiFn} }
11151115
if ret := c.Dedicated(func(cc DedicatedClient) error {
@@ -1151,7 +1151,7 @@ func SetupClientRetry(t *testing.T, fn func(mock *mockConn) Client) {
11511151
c, m := setup()
11521152
m.DoMultiFn = makeDoMultiFn(
11531153
[]RedisResult{newErrResult(ErrClosing)},
1154-
[]RedisResult{newResult(RedisMessage{typ: '+', string: "Do"}, nil)},
1154+
[]RedisResult{newResult(strmsg('+', "Do"), nil)},
11551155
)
11561156
m.AcquireFn = func() wire { return &mockWire{DoMultiFn: m.DoMultiFn} }
11571157
if ret := c.Dedicated(func(cc DedicatedClient) error {
@@ -1282,9 +1282,9 @@ func TestSingleClientLoadingRetry(t *testing.T) {
12821282
m.DoFn = func(cmd Completed) RedisResult {
12831283
attempts++
12841284
if attempts == 1 {
1285-
return newResult(RedisMessage{typ: '-', string: "LOADING Redis is loading the dataset in memory"}, nil)
1285+
return newResult(strmsg('-', "LOADING Redis is loading the dataset in memory"), nil)
12861286
}
1287-
return newResult(RedisMessage{typ: '+', string: "OK"}, nil)
1287+
return newResult(strmsg('+', "OK"), nil)
12881288
}
12891289

12901290
if v, err := client.Do(context.Background(), client.B().Get().Key("test").Build()).ToString(); err != nil || v != "OK" {
@@ -1301,9 +1301,9 @@ func TestSingleClientLoadingRetry(t *testing.T) {
13011301
m.DoFn = func(cmd Completed) RedisResult {
13021302
attempts++
13031303
if attempts == 1 {
1304-
return newResult(RedisMessage{typ: '-', string: "ERR some other error"}, nil)
1304+
return newResult(strmsg('-', "ERR some other error"), nil)
13051305
}
1306-
return newResult(RedisMessage{typ: '+', string: "OK"}, nil)
1306+
return newResult(strmsg('+', "OK"), nil)
13071307
}
13081308

13091309
if err := client.Do(context.Background(), client.B().Get().Key("test").Build()).Error(); err == nil {
@@ -1320,9 +1320,9 @@ func TestSingleClientLoadingRetry(t *testing.T) {
13201320
m.DoMultiFn = func(multi ...Completed) *redisresults {
13211321
attempts++
13221322
if attempts == 1 {
1323-
return &redisresults{s: []RedisResult{newResult(RedisMessage{typ: '-', string: "LOADING Redis is loading the dataset in memory"}, nil)}}
1323+
return &redisresults{s: []RedisResult{newResult(strmsg('-', "LOADING Redis is loading the dataset in memory"), nil)}}
13241324
}
1325-
return &redisresults{s: []RedisResult{newResult(RedisMessage{typ: '+', string: "OK"}, nil)}}
1325+
return &redisresults{s: []RedisResult{newResult(strmsg('+', "OK"), nil)}}
13261326
}
13271327

13281328
cmd := client.B().Get().Key("test").Build()
@@ -1341,9 +1341,9 @@ func TestSingleClientLoadingRetry(t *testing.T) {
13411341
m.DoCacheFn = func(cmd Cacheable, ttl time.Duration) RedisResult {
13421342
attempts++
13431343
if attempts == 1 {
1344-
return newResult(RedisMessage{typ: '-', string: "LOADING Redis is loading the dataset in memory"}, nil)
1344+
return newResult(strmsg('-', "LOADING Redis is loading the dataset in memory"), nil)
13451345
}
1346-
return newResult(RedisMessage{typ: '+', string: "OK"}, nil)
1346+
return newResult(strmsg('+', "OK"), nil)
13471347
}
13481348

13491349
cmd := client.B().Get().Key("test").Cache()
@@ -1358,9 +1358,9 @@ func TestSingleClientLoadingRetry(t *testing.T) {
13581358
m.DoMultiCacheFn = func(multi ...CacheableTTL) *redisresults {
13591359
attempts++
13601360
if attempts == 1 {
1361-
return &redisresults{s: []RedisResult{newResult(RedisMessage{typ: '-', string: "LOADING Redis is loading the dataset in memory"}, nil)}}
1361+
return &redisresults{s: []RedisResult{newResult(strmsg('-', "LOADING Redis is loading the dataset in memory"), nil)}}
13621362
}
1363-
return &redisresults{s: []RedisResult{newResult(RedisMessage{typ: '+', string: "OK"}, nil)}}
1363+
return &redisresults{s: []RedisResult{newResult(strmsg('+', "OK"), nil)}}
13641364
}
13651365

13661366
cmd := client.B().Get().Key("test").Cache()
@@ -1379,9 +1379,9 @@ func TestSingleClientLoadingRetry(t *testing.T) {
13791379
m.DoFn = func(cmd Completed) RedisResult {
13801380
attempts++
13811381
if attempts == 1 {
1382-
return newResult(RedisMessage{typ: '-', string: "LOADING Redis is loading the dataset in memory"}, nil)
1382+
return newResult(strmsg('-', "LOADING Redis is loading the dataset in memory"), nil)
13831383
}
1384-
return newResult(RedisMessage{typ: '+', string: "OK"}, nil)
1384+
return newResult(strmsg('+', "OK"), nil)
13851385
}
13861386
m.AcquireFn = func() wire { return &mockWire{DoFn: m.DoFn} }
13871387

@@ -1402,9 +1402,9 @@ func TestSingleClientLoadingRetry(t *testing.T) {
14021402
m.DoMultiFn = func(multi ...Completed) *redisresults {
14031403
attempts++
14041404
if attempts == 1 {
1405-
return &redisresults{s: []RedisResult{newResult(RedisMessage{typ: '-', string: "LOADING Redis is loading the dataset in memory"}, nil)}}
1405+
return &redisresults{s: []RedisResult{newResult(strmsg('-', "LOADING Redis is loading the dataset in memory"), nil)}}
14061406
}
1407-
return &redisresults{s: []RedisResult{newResult(RedisMessage{typ: '+', string: "OK"}, nil)}}
1407+
return &redisresults{s: []RedisResult{newResult(strmsg('+', "OK"), nil)}}
14081408
}
14091409
m.AcquireFn = func() wire { return &mockWire{DoMultiFn: m.DoMultiFn} }
14101410

0 commit comments

Comments
 (0)