Skip to content

Commit c99a521

Browse files
committed
reactor: modify storage inteaface and add contxt.Context
1 parent 5406560 commit c99a521

File tree

18 files changed

+159
-148
lines changed

18 files changed

+159
-148
lines changed

app.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -41,23 +41,23 @@ type Map map[string]interface{}
4141
type Storage interface {
4242
// Get gets the value for the given key.
4343
// `nil, nil` is returned when the key does not exist
44-
Get(key string) ([]byte, error)
44+
Get(ctx context.Context, key string) ([]byte, error)
4545

4646
// Set stores the given value for the given key along
4747
// with an expiration value, 0 means no expiration.
4848
// Empty key or value will be ignored without an error.
49-
Set(key string, val []byte, exp time.Duration) error
49+
Set(ctx context.Context, key string, val []byte, exp time.Duration) error
5050

5151
// Delete deletes the value for the given key.
5252
// It returns no error if the storage does not contain the key,
53-
Delete(key string) error
53+
Delete(ctx context.Context, key string) error
5454

5555
// Reset resets the storage and delete all keys.
56-
Reset() error
56+
Reset(ctx context.Context) error
5757

5858
// Close closes the storage and will stop any running garbage
5959
// collectors and open connections.
60-
Close() error
60+
Close(ctx context.Context) error
6161
}
6262

6363
// ErrorHandler defines a function that will process all errors

ctx.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1495,7 +1495,7 @@ func (c *Ctx) SaveFileToStorage(fileheader *multipart.FileHeader, path string, s
14951495
return err
14961496
}
14971497

1498-
return storage.Set(path, content, 0)
1498+
return storage.Set(c.Context(), path, content, 0)
14991499
}
15001500

15011501
// Secure returns whether a secure connection was established.

ctx_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -2251,11 +2251,11 @@ func Test_Ctx_SaveFileToStorage(t *testing.T) {
22512251
err = c.SaveFileToStorage(fh, "test", storage)
22522252
utils.AssertEqual(t, nil, err)
22532253

2254-
file, err := storage.Get("test")
2254+
file, err := storage.Get(c.Context(), "test")
22552255
utils.AssertEqual(t, []byte("hello world"), file)
22562256
utils.AssertEqual(t, nil, err)
22572257

2258-
err = storage.Delete("test")
2258+
err = storage.Delete(c.Context(), "test")
22592259
utils.AssertEqual(t, nil, err)
22602260

22612261
return nil

internal/memory/memory.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
package memory
44

55
import (
6+
"context"
67
"sync"
78
"sync/atomic"
89
"time"
@@ -31,7 +32,7 @@ func New() *Storage {
3132
}
3233

3334
// Get value by key
34-
func (s *Storage) Get(key string) interface{} {
35+
func (s *Storage) Get(_ context.Context, key string) interface{} {
3536
s.RLock()
3637
v, ok := s.data[key]
3738
s.RUnlock()
@@ -42,7 +43,7 @@ func (s *Storage) Get(key string) interface{} {
4243
}
4344

4445
// Set key with value
45-
func (s *Storage) Set(key string, val interface{}, ttl time.Duration) {
46+
func (s *Storage) Set(_ context.Context, key string, val interface{}, ttl time.Duration) {
4647
var exp uint32
4748
if ttl > 0 {
4849
exp = uint32(ttl.Seconds()) + atomic.LoadUint32(&utils.Timestamp)
@@ -54,14 +55,14 @@ func (s *Storage) Set(key string, val interface{}, ttl time.Duration) {
5455
}
5556

5657
// Delete key by key
57-
func (s *Storage) Delete(key string) {
58+
func (s *Storage) Delete(_ context.Context, key string) {
5859
s.Lock()
5960
delete(s.data, key)
6061
s.Unlock()
6162
}
6263

6364
// Reset all keys
64-
func (s *Storage) Reset() {
65+
func (s *Storage) Reset(_ context.Context) {
6566
nd := make(map[string]item)
6667
s.Lock()
6768
s.data = nd

internal/memory/memory_test.go

+19-18
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package memory
22

33
import (
4+
"context"
45
"testing"
56
"time"
67

@@ -18,37 +19,37 @@ func Test_Memory(t *testing.T) {
1819
exp = 1 * time.Second
1920
)
2021

21-
store.Set(key, val, 0)
22-
store.Set(key, val, 0)
22+
store.Set(context.TODO(), key, val, 0)
23+
store.Set(context.TODO(), key, val, 0)
2324

24-
result := store.Get(key)
25+
result := store.Get(context.TODO(), key)
2526
utils.AssertEqual(t, val, result)
2627

27-
result = store.Get("empty")
28+
result = store.Get(context.TODO(), "empty")
2829
utils.AssertEqual(t, nil, result)
2930

30-
store.Set(key, val, exp)
31+
store.Set(context.TODO(), key, val, exp)
3132
time.Sleep(1100 * time.Millisecond)
3233

33-
result = store.Get(key)
34+
result = store.Get(context.TODO(), key)
3435
utils.AssertEqual(t, nil, result)
3536

36-
store.Set(key, val, 0)
37-
result = store.Get(key)
37+
store.Set(context.TODO(), key, val, 0)
38+
result = store.Get(context.TODO(), key)
3839
utils.AssertEqual(t, val, result)
3940

40-
store.Delete(key)
41-
result = store.Get(key)
41+
store.Delete(context.TODO(), key)
42+
result = store.Get(context.TODO(), key)
4243
utils.AssertEqual(t, nil, result)
4344

44-
store.Set("john", val, 0)
45-
store.Set("doe", val, 0)
46-
store.Reset()
45+
store.Set(context.TODO(), "john", val, 0)
46+
store.Set(context.TODO(), "doe", val, 0)
47+
store.Reset(context.TODO())
4748

48-
result = store.Get("john")
49+
result = store.Get(context.TODO(), "john")
4950
utils.AssertEqual(t, nil, result)
5051

51-
result = store.Get("doe")
52+
result = store.Get(context.TODO(), "doe")
5253
utils.AssertEqual(t, nil, result)
5354
}
5455

@@ -68,13 +69,13 @@ func Benchmark_Memory(b *testing.B) {
6869
b.ResetTimer()
6970
for n := 0; n < b.N; n++ {
7071
for _, key := range keys {
71-
d.Set(key, value, ttl)
72+
d.Set(context.TODO(), key, value, ttl)
7273
}
7374
for _, key := range keys {
74-
_ = d.Get(key)
75+
_ = d.Get(context.TODO(), key)
7576
}
7677
for _, key := range keys {
77-
d.Delete(key)
78+
d.Delete(context.TODO(), key)
7879

7980
}
8081
}

internal/storage/memory/memory.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
package memory
44

55
import (
6+
"context"
67
"sync"
78
"sync/atomic"
89
"time"
@@ -44,7 +45,7 @@ func New(config ...Config) *Storage {
4445
}
4546

4647
// Get value by key
47-
func (s *Storage) Get(key string) ([]byte, error) {
48+
func (s *Storage) Get(_ context.Context, key string) ([]byte, error) {
4849
if len(key) <= 0 {
4950
return nil, nil
5051
}
@@ -59,7 +60,7 @@ func (s *Storage) Get(key string) ([]byte, error) {
5960
}
6061

6162
// Set key with value
62-
func (s *Storage) Set(key string, val []byte, exp time.Duration) error {
63+
func (s *Storage) Set(_ context.Context, key string, val []byte, exp time.Duration) error {
6364
// Ain't Nobody Got Time For That
6465
if len(key) <= 0 || len(val) <= 0 {
6566
return nil
@@ -78,7 +79,7 @@ func (s *Storage) Set(key string, val []byte, exp time.Duration) error {
7879
}
7980

8081
// Delete key by key
81-
func (s *Storage) Delete(key string) error {
82+
func (s *Storage) Delete(_ context.Context, key string) error {
8283
// Ain't Nobody Got Time For That
8384
if len(key) <= 0 {
8485
return nil
@@ -90,7 +91,7 @@ func (s *Storage) Delete(key string) error {
9091
}
9192

9293
// Reset all keys
93-
func (s *Storage) Reset() error {
94+
func (s *Storage) Reset(_ context.Context) error {
9495
ndb := make(map[string]entry)
9596
s.mux.Lock()
9697
s.db = ndb
@@ -99,7 +100,7 @@ func (s *Storage) Reset() error {
99100
}
100101

101102
// Close the memory storage
102-
func (s *Storage) Close() error {
103+
func (s *Storage) Close(_ context.Context) error {
103104
s.done <- struct{}{}
104105
return nil
105106
}
@@ -137,7 +138,7 @@ func (s *Storage) gc() {
137138
}
138139
}
139140

140-
// Return database client
141+
// Conn Return database client
141142
func (s *Storage) Conn() map[string]entry {
142143
return s.db
143144
}

internal/storage/memory/memory_test.go

+21-20
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package memory
22

33
import (
4+
"context"
45
"testing"
56
"time"
67

@@ -16,7 +17,7 @@ func Test_Storage_Memory_Set(t *testing.T) {
1617
val = []byte("doe")
1718
)
1819

19-
err := testStore.Set(key, val, 0)
20+
err := testStore.Set(context.TODO(), key, val, 0)
2021
utils.AssertEqual(t, nil, err)
2122
}
2223

@@ -27,10 +28,10 @@ func Test_Storage_Memory_Set_Override(t *testing.T) {
2728
val = []byte("doe")
2829
)
2930

30-
err := testStore.Set(key, val, 0)
31+
err := testStore.Set(context.TODO(), key, val, 0)
3132
utils.AssertEqual(t, nil, err)
3233

33-
err = testStore.Set(key, val, 0)
34+
err = testStore.Set(context.TODO(), key, val, 0)
3435
utils.AssertEqual(t, nil, err)
3536
}
3637

@@ -41,10 +42,10 @@ func Test_Storage_Memory_Get(t *testing.T) {
4142
val = []byte("doe")
4243
)
4344

44-
err := testStore.Set(key, val, 0)
45+
err := testStore.Set(context.TODO(), key, val, 0)
4546
utils.AssertEqual(t, nil, err)
4647

47-
result, err := testStore.Get(key)
48+
result, err := testStore.Get(context.TODO(), key)
4849
utils.AssertEqual(t, nil, err)
4950
utils.AssertEqual(t, val, result)
5051
}
@@ -57,7 +58,7 @@ func Test_Storage_Memory_Set_Expiration(t *testing.T) {
5758
exp = 1 * time.Second
5859
)
5960

60-
err := testStore.Set(key, val, exp)
61+
err := testStore.Set(context.TODO(), key, val, exp)
6162
utils.AssertEqual(t, nil, err)
6263

6364
time.Sleep(1100 * time.Millisecond)
@@ -68,15 +69,15 @@ func Test_Storage_Memory_Get_Expired(t *testing.T) {
6869
key = "john"
6970
)
7071

71-
result, err := testStore.Get(key)
72+
result, err := testStore.Get(context.TODO(), key)
7273
utils.AssertEqual(t, nil, err)
7374
utils.AssertEqual(t, true, len(result) == 0)
7475
}
7576

7677
func Test_Storage_Memory_Get_NotExist(t *testing.T) {
7778
t.Parallel()
7879

79-
result, err := testStore.Get("notexist")
80+
result, err := testStore.Get(context.TODO(), "notexist")
8081
utils.AssertEqual(t, nil, err)
8182
utils.AssertEqual(t, true, len(result) == 0)
8283
}
@@ -88,13 +89,13 @@ func Test_Storage_Memory_Delete(t *testing.T) {
8889
val = []byte("doe")
8990
)
9091

91-
err := testStore.Set(key, val, 0)
92+
err := testStore.Set(context.TODO(), key, val, 0)
9293
utils.AssertEqual(t, nil, err)
9394

94-
err = testStore.Delete(key)
95+
err = testStore.Delete(context.TODO(), key)
9596
utils.AssertEqual(t, nil, err)
9697

97-
result, err := testStore.Get(key)
98+
result, err := testStore.Get(context.TODO(), key)
9899
utils.AssertEqual(t, nil, err)
99100
utils.AssertEqual(t, true, len(result) == 0)
100101
}
@@ -105,27 +106,27 @@ func Test_Storage_Memory_Reset(t *testing.T) {
105106
val = []byte("doe")
106107
)
107108

108-
err := testStore.Set("john1", val, 0)
109+
err := testStore.Set(context.TODO(), "john1", val, 0)
109110
utils.AssertEqual(t, nil, err)
110111

111-
err = testStore.Set("john2", val, 0)
112+
err = testStore.Set(context.TODO(), "john2", val, 0)
112113
utils.AssertEqual(t, nil, err)
113114

114-
err = testStore.Reset()
115+
err = testStore.Reset(context.TODO())
115116
utils.AssertEqual(t, nil, err)
116117

117-
result, err := testStore.Get("john1")
118+
result, err := testStore.Get(context.TODO(), "john1")
118119
utils.AssertEqual(t, nil, err)
119120
utils.AssertEqual(t, true, len(result) == 0)
120121

121-
result, err = testStore.Get("john2")
122+
result, err = testStore.Get(context.TODO(), "john2")
122123
utils.AssertEqual(t, nil, err)
123124
utils.AssertEqual(t, true, len(result) == 0)
124125
}
125126

126127
func Test_Storage_Memory_Close(t *testing.T) {
127128
t.Parallel()
128-
utils.AssertEqual(t, nil, testStore.Close())
129+
utils.AssertEqual(t, nil, testStore.Close(context.TODO()))
129130
}
130131

131132
func Test_Storage_Memory_Conn(t *testing.T) {
@@ -149,13 +150,13 @@ func Benchmark_Storage_Memory(b *testing.B) {
149150
b.ResetTimer()
150151
for n := 0; n < b.N; n++ {
151152
for _, key := range keys {
152-
d.Set(key, value, ttl)
153+
d.Set(context.TODO(), key, value, ttl)
153154
}
154155
for _, key := range keys {
155-
_, _ = d.Get(key)
156+
_, _ = d.Get(context.TODO(), key)
156157
}
157158
for _, key := range keys {
158-
d.Delete(key)
159+
d.Delete(context.TODO(), key)
159160
}
160161
}
161162
})

0 commit comments

Comments
 (0)