From da1b9deee3e36a8312b03b8fabdd8dc881c857bc Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Fri, 8 Jul 2022 23:17:14 +0300 Subject: [PATCH] limit timeout Signed-off-by: Vasiliy Tolstov --- go.mod | 4 +++- go.sum | 6 ++++++ redis.go | 30 ++++++++++++++++++++++++++---- 3 files changed, 35 insertions(+), 5 deletions(-) diff --git a/go.mod b/go.mod index a4bcd95..2f49513 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,8 @@ go 1.16 require ( github.com/go-redis/redis/v8 v8.11.5 - go.unistack.org/micro/v3 v3.9.10 + github.com/google/gnostic v0.6.9 // indirect + go.unistack.org/micro/v3 v3.9.11 golang.org/x/net v0.0.0-20220225172249-27dd8689420f // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 2d704ea..113e9de 100644 --- a/go.sum +++ b/go.sum @@ -55,6 +55,8 @@ github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiu github.com/google/gnostic v0.6.6/go.mod h1:Nm8234We1lq6iB9OmlgNv3nH91XLLVZHCDayfA3xq+E= github.com/google/gnostic v0.6.8 h1:bT56GPYBWh1tvBuBEd94qcS3+60b+y0HQur0ITkGuCk= github.com/google/gnostic v0.6.8/go.mod h1:Nm8234We1lq6iB9OmlgNv3nH91XLLVZHCDayfA3xq+E= +github.com/google/gnostic v0.6.9 h1:ZK/5VhkoX835RikCHpSUJV9a+S3e1zLh59YnyWeBW+0= +github.com/google/gnostic v0.6.9/go.mod h1:Nm8234We1lq6iB9OmlgNv3nH91XLLVZHCDayfA3xq+E= github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= @@ -110,6 +112,8 @@ go.unistack.org/micro-proto/v3 v3.2.7 h1:zG6d69kHc+oij2lwQ3AfrCgdjiEVRG2A7Tlsxju go.unistack.org/micro-proto/v3 v3.2.7/go.mod h1:ZltVWNECD5yK+40+OCONzGw4OtmSdTpVi8/KFgo9dqM= go.unistack.org/micro/v3 v3.9.10 h1:MOLOM8sm3PthhkJVKiraUlOMDyQLAofKNCJakPN/JBw= go.unistack.org/micro/v3 v3.9.10/go.mod h1:LrBm9Fsf4MbY8DlUbdwRTYJosMkN8wrtOQHoYBlMkz8= +go.unistack.org/micro/v3 v3.9.11 h1:dsZVss3nvfByL1ZDJNnUVQB1N8w6qn4pr9vIkeBiii8= +go.unistack.org/micro/v3 v3.9.11/go.mod h1:LrBm9Fsf4MbY8DlUbdwRTYJosMkN8wrtOQHoYBlMkz8= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= @@ -223,5 +227,7 @@ gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776/go.mod h1:K4uyk7z7BCEPqu6E+C gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.0 h1:hjy8E9ON/egN1tAYqKb61G10WtihqetD4sz2H+8nIeA= gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= diff --git a/redis.go b/redis.go index 0f99749..6051e42 100644 --- a/redis.go +++ b/redis.go @@ -14,6 +14,7 @@ type rkv struct { cli redisClient } +// TODO: add ability to set some redis options https://pkg.go.dev/github.com/go-redis/redis/v8#Options type redisClient interface { Get(ctx context.Context, key string) *redis.StringCmd Del(ctx context.Context, keys ...string) *redis.IntCmd @@ -44,6 +45,11 @@ func (r *rkv) Exists(ctx context.Context, key string, opts ...store.ExistsOption if len(options.Namespace) == 0 { options.Namespace = r.opts.Namespace } + if r.opts.Timeout > 0 { + var cancel context.CancelFunc + ctx, cancel = context.WithTimeout(ctx, r.opts.Timeout) + defer cancel() + } rkey := fmt.Sprintf("%s%s", options.Namespace, key) st, err := r.cli.Exists(ctx, rkey).Result() if err != nil { @@ -60,7 +66,11 @@ func (r *rkv) Read(ctx context.Context, key string, val interface{}, opts ...sto if len(options.Namespace) == 0 { options.Namespace = r.opts.Namespace } - + if r.opts.Timeout > 0 { + var cancel context.CancelFunc + ctx, cancel = context.WithTimeout(ctx, r.opts.Timeout) + defer cancel() + } rkey := fmt.Sprintf("%s%s", options.Namespace, key) buf, err := r.cli.Get(ctx, rkey).Bytes() if err != nil && err == redis.Nil { @@ -92,7 +102,11 @@ func (r *rkv) Delete(ctx context.Context, key string, opts ...store.DeleteOption if len(options.Namespace) == 0 { options.Namespace = r.opts.Namespace } - + if r.opts.Timeout > 0 { + var cancel context.CancelFunc + ctx, cancel = context.WithTimeout(ctx, r.opts.Timeout) + defer cancel() + } rkey := fmt.Sprintf("%s%s", options.Namespace, key) return r.cli.Del(ctx, rkey).Err() } @@ -108,7 +122,11 @@ func (r *rkv) Write(ctx context.Context, key string, val interface{}, opts ...st if err != nil { return err } - + if r.opts.Timeout > 0 { + var cancel context.CancelFunc + ctx, cancel = context.WithTimeout(ctx, r.opts.Timeout) + defer cancel() + } return r.cli.Set(ctx, rkey, buf, options.TTL).Err() } @@ -117,7 +135,11 @@ func (r *rkv) List(ctx context.Context, opts ...store.ListOption) ([]string, err if len(options.Namespace) == 0 { options.Namespace = r.opts.Namespace } - + if r.opts.Timeout > 0 { + var cancel context.CancelFunc + ctx, cancel = context.WithTimeout(ctx, r.opts.Timeout) + defer cancel() + } // TODO: add support for prefix/suffix/limit keys, err := r.cli.Keys(ctx, "*").Result() if err != nil { -- 2.45.2