fix add add a ctx context variable in the Redis struct. And implemet `Close() error` function
This commit is contained in:
parent
4c0aef5c4e
commit
c55d88cd98
|
@ -36,6 +36,9 @@ const (
|
||||||
type Redis struct {
|
type Redis struct {
|
||||||
client *redis.Client
|
client *redis.Client
|
||||||
logger logger.Logger
|
logger logger.Logger
|
||||||
|
|
||||||
|
ctx context.Context
|
||||||
|
cancel context.CancelFunc
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewRedis returns a new redis bindings instance
|
// NewRedis returns a new redis bindings instance
|
||||||
|
@ -66,7 +69,9 @@ func (r *Redis) Init(meta bindings.Metadata) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
r.client = redis.NewClient(opts)
|
r.client = redis.NewClient(opts)
|
||||||
_, err = r.client.Ping(context.Background()).Result()
|
r.ctx, r.cancel = context.WithCancel(context.Background())
|
||||||
|
|
||||||
|
_, err = r.client.Ping(r.ctx).Result()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("redis binding: error connecting to redis at %s: %s", m.host, err)
|
return fmt.Errorf("redis binding: error connecting to redis at %s: %s", m.host, err)
|
||||||
}
|
}
|
||||||
|
@ -124,7 +129,7 @@ func (r *Redis) Operations() []bindings.OperationKind {
|
||||||
func (r *Redis) Invoke(req *bindings.InvokeRequest) (*bindings.InvokeResponse, error) {
|
func (r *Redis) Invoke(req *bindings.InvokeRequest) (*bindings.InvokeResponse, error) {
|
||||||
if val, ok := req.Metadata["key"]; ok && val != "" {
|
if val, ok := req.Metadata["key"]; ok && val != "" {
|
||||||
key := val
|
key := val
|
||||||
_, err := r.client.Do(context.Background(), "SET", key, req.Data).Result()
|
_, err := r.client.Do(r.ctx, "SET", key, req.Data).Result()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -134,3 +139,9 @@ func (r *Redis) Invoke(req *bindings.InvokeRequest) (*bindings.InvokeResponse, e
|
||||||
|
|
||||||
return nil, errors.New("redis binding: missing key on write request metadata")
|
return nil, errors.New("redis binding: missing key on write request metadata")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *Redis) Close() error {
|
||||||
|
r.cancel()
|
||||||
|
|
||||||
|
return r.client.Close()
|
||||||
|
}
|
Loading…
Reference in New Issue