Skip to content

Commit 8335d9e

Browse files
committed
feat(context): adding ctx as first argument
To maintain backward compatibility addint WithContext set of functions instead of changing the signatures of existing ones. The Ctx parameter is not used yet in many functions yet. The usage will come in the following PR after interface is changed. Signed-off-by: Boris Glimcher <Boris.Glimcher@emc.com>
1 parent 99127de commit 8335d9e

24 files changed

Lines changed: 442 additions & 21 deletions

File tree

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,11 @@ Features
3636
```go
3737
type Store interface {
3838
Set(k string, v interface{}) error
39+
SetWithContext(ctx context.Context, k string, v interface{}) error
3940
Get(k string, v interface{}) (found bool, err error)
41+
GetWithContext(ctx context.Context, k string, v interface{}) (found bool, err error)
4042
Delete(k string) error
43+
DeleteWithContext(ctx context.Context, k string) error
4144
Close() error
4245
}
4346
```

badgerdb/badgerdb.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package badgerdb
22

33
import (
4+
"context"
45
"github.com/dgraph-io/badger"
56

67
"github.com/philippgille/gokv/encoding"
@@ -17,6 +18,12 @@ type Store struct {
1718
// Values are automatically marshalled to JSON or gob (depending on the configuration).
1819
// The key must not be "" and the value must not be nil.
1920
func (s Store) Set(k string, v interface{}) error {
21+
ctx := context.Background()
22+
return s.SetWithContext(ctx, k, v)
23+
}
24+
25+
// SetWithContext is exactly like Set function just with added context as first argument.
26+
func (s Store) SetWithContext(_ context.Context, k string, v interface{}) error {
2027
if err := util.CheckKeyAndValue(k, v); err != nil {
2128
return err
2229
}
@@ -43,6 +50,12 @@ func (s Store) Set(k string, v interface{}) error {
4350
// If no value is found it returns (false, nil).
4451
// The key must not be "" and the pointer must not be nil.
4552
func (s Store) Get(k string, v interface{}) (found bool, err error) {
53+
ctx := context.Background()
54+
return s.GetWithContext(ctx, k, v)
55+
}
56+
57+
// GetWithContext is exactly like Get function just with added context as first argument.
58+
func (s Store) GetWithContext(_ context.Context, k string, v interface{}) (found bool, err error) {
4659
if err := util.CheckKeyAndValue(k, v); err != nil {
4760
return false, err
4861
}
@@ -77,6 +90,12 @@ func (s Store) Get(k string, v interface{}) (found bool, err error) {
7790
// Deleting a non-existing key-value pair does NOT lead to an error.
7891
// The key must not be "".
7992
func (s Store) Delete(k string) error {
93+
ctx := context.Background()
94+
return s.DeleteWithContext(ctx, k)
95+
}
96+
97+
// DeleteWithContext is exactly like Delete function just with added context as first argument.
98+
func (s Store) DeleteWithContext(_ context.Context, k string) error {
8099
if err := util.CheckKey(k); err != nil {
81100
return err
82101
}

bbolt/bbolt.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package bbolt
22

33
import (
4+
"context"
45
bolt "go.etcd.io/bbolt"
56

67
"github.com/philippgille/gokv/encoding"
@@ -18,6 +19,12 @@ type Store struct {
1819
// Values are automatically marshalled to JSON or gob (depending on the configuration).
1920
// The key must not be "" and the value must not be nil.
2021
func (s Store) Set(k string, v interface{}) error {
22+
ctx := context.Background()
23+
return s.SetWithContext(ctx, k, v)
24+
}
25+
26+
// SetWithContext is exactly like Set function just with added context as first argument.
27+
func (s Store) SetWithContext(_ context.Context, k string, v interface{}) error {
2128
if err := util.CheckKeyAndValue(k, v); err != nil {
2229
return err
2330
}
@@ -45,6 +52,12 @@ func (s Store) Set(k string, v interface{}) error {
4552
// If no value is found it returns (false, nil).
4653
// The key must not be "" and the pointer must not be nil.
4754
func (s Store) Get(k string, v interface{}) (found bool, err error) {
55+
ctx := context.Background()
56+
return s.GetWithContext(ctx, k, v)
57+
}
58+
59+
// GetWithContext is exactly like Get function just with added context as first argument.
60+
func (s Store) GetWithContext(_ context.Context, k string, v interface{}) (found bool, err error) {
4861
if err := util.CheckKeyAndValue(k, v); err != nil {
4962
return false, err
5063
}
@@ -80,6 +93,12 @@ func (s Store) Get(k string, v interface{}) (found bool, err error) {
8093
// Deleting a non-existing key-value pair does NOT lead to an error.
8194
// The key must not be "".
8295
func (s Store) Delete(k string) error {
96+
ctx := context.Background()
97+
return s.DeleteWithContext(ctx, k)
98+
}
99+
100+
// DeleteWithContext is exactly like Delete function just with added context as first argument.
101+
func (s Store) DeleteWithContext(_ context.Context, k string) error {
83102
if err := util.CheckKey(k); err != nil {
84103
return err
85104
}

bigcache/bigcache.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package bigcache
22

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

67
"github.com/allegro/bigcache/v2"
@@ -19,6 +20,12 @@ type Store struct {
1920
// Values are automatically marshalled to JSON or gob (depending on the configuration).
2021
// The key must not be "" and the value must not be nil.
2122
func (s Store) Set(k string, v interface{}) error {
23+
ctx := context.Background()
24+
return s.SetWithContext(ctx, k, v)
25+
}
26+
27+
// SetWithContext is exactly like Set function just with added context as first argument.
28+
func (s Store) SetWithContext(_ context.Context, k string, v interface{}) error {
2229
if err := util.CheckKeyAndValue(k, v); err != nil {
2330
return err
2431
}
@@ -38,6 +45,12 @@ func (s Store) Set(k string, v interface{}) error {
3845
// If no value is found it returns (false, nil).
3946
// The key must not be "" and the pointer must not be nil.
4047
func (s Store) Get(k string, v interface{}) (found bool, err error) {
48+
ctx := context.Background()
49+
return s.GetWithContext(ctx, k, v)
50+
}
51+
52+
// GetWithContext is exactly like Get function just with added context as first argument.
53+
func (s Store) GetWithContext(_ context.Context, k string, v interface{}) (found bool, err error) {
4154
if err := util.CheckKeyAndValue(k, v); err != nil {
4255
return false, err
4356
}
@@ -57,6 +70,12 @@ func (s Store) Get(k string, v interface{}) (found bool, err error) {
5770
// Deleting a non-existing key-value pair does NOT lead to an error.
5871
// The key must not be "".
5972
func (s Store) Delete(k string) error {
73+
ctx := context.Background()
74+
return s.DeleteWithContext(ctx, k)
75+
}
76+
77+
// DeleteWithContext is exactly like Delete function just with added context as first argument.
78+
func (s Store) DeleteWithContext(_ context.Context, k string) error {
6079
if err := util.CheckKey(k); err != nil {
6180
return err
6281
}

consul/consul.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package consul
22

33
import (
4+
"context"
45
"github.com/hashicorp/consul/api"
56

67
"github.com/philippgille/gokv/encoding"
@@ -18,6 +19,12 @@ type Client struct {
1819
// Values are automatically marshalled to JSON or gob (depending on the configuration).
1920
// The key must not be "" and the value must not be nil.
2021
func (c Client) Set(k string, v interface{}) error {
22+
ctx := context.Background()
23+
return c.SetWithContext(ctx, k, v)
24+
}
25+
26+
// SetWithContext is exactly like Set function just with added context as first argument.
27+
func (c Client) SetWithContext(_ context.Context, k string, v interface{}) error {
2128
if err := util.CheckKeyAndValue(k, v); err != nil {
2229
return err
2330
}
@@ -50,6 +57,12 @@ func (c Client) Set(k string, v interface{}) error {
5057
// If no value is found it returns (false, nil).
5158
// The key must not be "" and the pointer must not be nil.
5259
func (c Client) Get(k string, v interface{}) (found bool, err error) {
60+
ctx := context.Background()
61+
return c.GetWithContext(ctx, k, v)
62+
}
63+
64+
// GetWithContext is exactly like Get function just with added context as first argument.
65+
func (c Client) GetWithContext(_ context.Context, k string, v interface{}) (found bool, err error) {
5366
if err := util.CheckKeyAndValue(k, v); err != nil {
5467
return false, err
5568
}
@@ -74,6 +87,12 @@ func (c Client) Get(k string, v interface{}) (found bool, err error) {
7487
// Deleting a non-existing key-value pair does NOT lead to an error.
7588
// The key must not be "".
7689
func (c Client) Delete(k string) error {
90+
ctx := context.Background()
91+
return c.DeleteWithContext(ctx, k)
92+
}
93+
94+
// DeleteWithContext is exactly like Delete function just with added context as first argument.
95+
func (c Client) DeleteWithContext(_ context.Context, k string) error {
7796
if err := util.CheckKey(k); err != nil {
7897
return err
7998
}

datastore/datastore.go

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ type Client struct {
3232
// Values are automatically marshalled to JSON or gob (depending on the configuration).
3333
// The key must not be "" and the value must not be nil.
3434
func (c Client) Set(k string, v interface{}) error {
35+
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
36+
defer cancel()
37+
return c.SetWithContext(ctx, k, v)
38+
}
39+
40+
// SetWithContext is exactly like Set function just with added context as first argument.
41+
func (c Client) SetWithContext(ctx context.Context, k string, v interface{}) error {
3542
if err := util.CheckKeyAndValue(k, v); err != nil {
3643
return err
3744
}
@@ -42,16 +49,14 @@ func (c Client) Set(k string, v interface{}) error {
4249
return err
4350
}
4451

45-
tctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
46-
defer cancel()
4752
key := datastore.Key{
4853
Kind: kind,
4954
Name: k,
5055
}
5156
src := entity{
5257
V: data,
5358
}
54-
_, err = c.c.Put(tctx, &key, &src)
59+
_, err = c.c.Put(ctx, &key, &src)
5560

5661
return err
5762
}
@@ -63,18 +68,23 @@ func (c Client) Set(k string, v interface{}) error {
6368
// If no value is found it returns (false, nil).
6469
// The key must not be "" and the pointer must not be nil.
6570
func (c Client) Get(k string, v interface{}) (found bool, err error) {
71+
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
72+
defer cancel()
73+
return c.GetWithContext(ctx, k, v)
74+
}
75+
76+
// GetWithContext is exactly like Get function just with added context as first argument.
77+
func (c Client) GetWithContext(ctx context.Context, k string, v interface{}) (found bool, err error) {
6678
if err := util.CheckKeyAndValue(k, v); err != nil {
6779
return false, err
6880
}
6981

70-
tctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
71-
defer cancel()
7282
key := datastore.Key{
7383
Kind: kind,
7484
Name: k,
7585
}
7686
dst := new(entity)
77-
err = c.c.Get(tctx, &key, dst)
87+
err = c.c.Get(ctx, &key, dst)
7888
if err != nil {
7989
if err == datastore.ErrNoSuchEntity {
8090
return false, nil
@@ -90,17 +100,22 @@ func (c Client) Get(k string, v interface{}) (found bool, err error) {
90100
// Deleting a non-existing key-value pair does NOT lead to an error.
91101
// The key must not be "".
92102
func (c Client) Delete(k string) error {
103+
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
104+
defer cancel()
105+
return c.DeleteWithContext(ctx, k)
106+
}
107+
108+
// DeleteWithContext is exactly like Delete function just with added context as first argument.
109+
func (c Client) DeleteWithContext(ctx context.Context, k string) error {
93110
if err := util.CheckKey(k); err != nil {
94111
return err
95112
}
96113

97-
tctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
98-
defer cancel()
99114
key := datastore.Key{
100115
Kind: kind,
101116
Name: k,
102117
}
103-
return c.c.Delete(tctx, &key)
118+
return c.c.Delete(ctx, &key)
104119
}
105120

106121
// Close closes the client.

dynamodb/dynamodb.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ type Client struct {
3232
// Values are automatically marshalled to JSON or gob (depending on the configuration).
3333
// The key must not be "" and the value must not be nil.
3434
func (c Client) Set(k string, v interface{}) error {
35+
ctx := context.Background()
36+
return c.SetWithContext(ctx, k, v)
37+
}
38+
39+
// SetWithContext is exactly like Set function just with added context as first argument.
40+
func (c Client) SetWithContext(_ context.Context, k string, v interface{}) error {
3541
if err := util.CheckKeyAndValue(k, v); err != nil {
3642
return err
3743
}
@@ -67,6 +73,12 @@ func (c Client) Set(k string, v interface{}) error {
6773
// If no value is found it returns (false, nil).
6874
// The key must not be "" and the pointer must not be nil.
6975
func (c Client) Get(k string, v interface{}) (found bool, err error) {
76+
ctx := context.Background()
77+
return c.GetWithContext(ctx, k, v)
78+
}
79+
80+
// GetWithContext is exactly like Get function just with added context as first argument.
81+
func (c Client) GetWithContext(_ context.Context, k string, v interface{}) (found bool, err error) {
7082
if err := util.CheckKeyAndValue(k, v); err != nil {
7183
return false, err
7284
}
@@ -101,6 +113,12 @@ func (c Client) Get(k string, v interface{}) (found bool, err error) {
101113
// Deleting a non-existing key-value pair does NOT lead to an error.
102114
// The key must not be "".
103115
func (c Client) Delete(k string) error {
116+
ctx := context.Background()
117+
return c.DeleteWithContext(ctx, k)
118+
}
119+
120+
// DeleteWithContext is exactly like Delete function just with added context as first argument.
121+
func (c Client) DeleteWithContext(_ context.Context, k string) error {
104122
if err := util.CheckKey(k); err != nil {
105123
return err
106124
}

etcd/etcd.go

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ type Client struct {
2424
// Values are automatically marshalled to JSON or gob (depending on the configuration).
2525
// The key must not be "" and the value must not be nil.
2626
func (c Client) Set(k string, v interface{}) error {
27+
ctx, cancel := context.WithTimeout(context.Background(), c.timeOut)
28+
defer cancel()
29+
return c.SetWithContext(ctx, k, v)
30+
}
31+
32+
// SetWithContext is exactly like Set function just with added context as first argument.
33+
func (c Client) SetWithContext(ctx context.Context, k string, v interface{}) error {
2734
if err := util.CheckKeyAndValue(k, v); err != nil {
2835
return err
2936
}
@@ -34,9 +41,7 @@ func (c Client) Set(k string, v interface{}) error {
3441
return err
3542
}
3643

37-
ctxWithTimeout, cancel := context.WithTimeout(context.Background(), c.timeOut)
38-
defer cancel()
39-
_, err = c.c.Put(ctxWithTimeout, k, string(data))
44+
_, err = c.c.Put(ctx, k, string(data))
4045
if err != nil {
4146
return err
4247
}
@@ -51,13 +56,18 @@ func (c Client) Set(k string, v interface{}) error {
5156
// If no value is found it returns (false, nil).
5257
// The key must not be "" and the pointer must not be nil.
5358
func (c Client) Get(k string, v interface{}) (found bool, err error) {
59+
ctx, cancel := context.WithTimeout(context.Background(), c.timeOut)
60+
defer cancel()
61+
return c.GetWithContext(ctx, k, v)
62+
}
63+
64+
// GetWithContext is exactly like Get function just with added context as first argument.
65+
func (c Client) GetWithContext(ctx context.Context, k string, v interface{}) (found bool, err error) {
5466
if err := util.CheckKeyAndValue(k, v); err != nil {
5567
return false, err
5668
}
5769

58-
ctxWithTimeout, cancel := context.WithTimeout(context.Background(), c.timeOut)
59-
defer cancel()
60-
getRes, err := c.c.Get(ctxWithTimeout, k)
70+
getRes, err := c.c.Get(ctx, k)
6171
if err != nil {
6272
return false, err
6373
}
@@ -75,13 +85,18 @@ func (c Client) Get(k string, v interface{}) (found bool, err error) {
7585
// Deleting a non-existing key-value pair does NOT lead to an error.
7686
// The key must not be "".
7787
func (c Client) Delete(k string) error {
88+
ctx, cancel := context.WithTimeout(context.Background(), c.timeOut)
89+
defer cancel()
90+
return c.DeleteWithContext(ctx, k)
91+
}
92+
93+
// DeleteWithContext is exactly like Delete function just with added context as first argument.
94+
func (c Client) DeleteWithContext(ctx context.Context, k string) error {
7895
if err := util.CheckKey(k); err != nil {
7996
return err
8097
}
8198

82-
ctxWithTimeout, cancel := context.WithTimeout(context.Background(), c.timeOut)
83-
defer cancel()
84-
_, err := c.c.Delete(ctxWithTimeout, k)
99+
_, err := c.c.Delete(ctx, k)
85100
return err
86101
}
87102

0 commit comments

Comments
 (0)