Skip to content

Commit

Permalink
Add Close method to redis Resource to close the redis Client (#90)
Browse files Browse the repository at this point in the history
* add close method to redis resource

re: AB#10021
---------

Co-authored-by: jgough <joe.gough@datatrails.ai>
  • Loading branch information
honourfish and jgough authored Oct 25, 2024
1 parent 3e9eae2 commit 4f7d8a4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
5 changes: 5 additions & 0 deletions redis/client_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ func (mc *mockClient) Set(ctx context.Context, key string, value any, expiration
return arguments.Get(0).(*redis.StatusCmd)
}

func (mc *mockClient) Close() error {
arguments := mc.Called()
return arguments.Get(0).(error)
}

func (mc *mockClient) SetNX(ctx context.Context, key string, value any, expiration time.Duration) (reply *redis.BoolCmd) {

arguments := mc.Called(key, value, expiration)
Expand Down
10 changes: 10 additions & 0 deletions redis/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ type Client interface {
Do(ctx context.Context, args ...any) *redis.Cmd
Set(ctx context.Context, key string, value any, expiration time.Duration) *redis.StatusCmd
SetNX(ctx context.Context, key string, value any, expiration time.Duration) *redis.BoolCmd
Close() error
}

type ClientContext struct {
Expand Down Expand Up @@ -319,3 +320,12 @@ func (r *Resource) nakedWrite(ctx context.Context, count int64, tenantID string)

return nil
}

// Close closes the resource and client connection to redis
func (r *Resource) Close() error {
if r.client != nil {
return r.client.Close()
}

return nil
}

0 comments on commit 4f7d8a4

Please # to comment.