From cf084b410f3f12d769ccc0dea3175fc8dda6ec6a Mon Sep 17 00:00:00 2001 From: ben-toogood Date: Wed, 19 Aug 2020 16:03:19 +0100 Subject: [PATCH] util/file: allow context to be passed (#1950) --- util/file/client.go | 27 +++++++++++++++++++-------- util/file/options.go | 15 +++++++++++++++ 2 files changed, 34 insertions(+), 8 deletions(-) create mode 100644 util/file/options.go diff --git a/util/file/client.go b/util/file/client.go index 5163a8ab..9c50b81e 100644 --- a/util/file/client.go +++ b/util/file/client.go @@ -28,8 +28,18 @@ type File interface { } // NewClient returns a new Client which uses a micro Client -func New(service string, c client.Client) File { - return &fc{proto.NewFileService(service, c)} +func New(service string, c client.Client, opts ...Option) File { + options := Options{ + Context: context.TODO(), + } + for _, o := range opts { + o(&options) + } + + return &fc{ + c: proto.NewFileService(service, c), + opts: options, + } } const ( @@ -37,11 +47,12 @@ const ( ) type fc struct { - c proto.FileService + c proto.FileService + opts Options } func (c *fc) Open(filename string, truncate bool) (int64, error) { - rsp, err := c.c.Open(context.TODO(), &proto.OpenRequest{ + rsp, err := c.c.Open(c.opts.Context, &proto.OpenRequest{ Filename: filename, Truncate: truncate, }) @@ -52,7 +63,7 @@ func (c *fc) Open(filename string, truncate bool) (int64, error) { } func (c *fc) Stat(filename string) (*proto.StatResponse, error) { - return c.c.Stat(context.TODO(), &proto.StatRequest{Filename: filename}) + return c.c.Stat(c.opts.Context, &proto.StatRequest{Filename: filename}) } func (c *fc) GetBlock(sessionId, blockId int64) ([]byte, error) { @@ -60,7 +71,7 @@ func (c *fc) GetBlock(sessionId, blockId int64) ([]byte, error) { } func (c *fc) ReadAt(sessionId, offset, size int64) ([]byte, error) { - rsp, err := c.c.Read(context.TODO(), &proto.ReadRequest{Id: sessionId, Size: size, Offset: offset}) + rsp, err := c.c.Read(c.opts.Context, &proto.ReadRequest{Id: sessionId, Size: size, Offset: offset}) if err != nil { return nil, err } @@ -90,7 +101,7 @@ func (c *fc) Read(sessionId int64, buf []byte) (int, error) { } func (c *fc) Write(sessionId, offset int64, data []byte) error { - _, err := c.c.Write(context.TODO(), &proto.WriteRequest{ + _, err := c.c.Write(c.opts.Context, &proto.WriteRequest{ Id: sessionId, Offset: offset, Data: data}) @@ -98,7 +109,7 @@ func (c *fc) Write(sessionId, offset int64, data []byte) error { } func (c *fc) Close(sessionId int64) error { - _, err := c.c.Close(context.TODO(), &proto.CloseRequest{Id: sessionId}) + _, err := c.c.Close(c.opts.Context, &proto.CloseRequest{Id: sessionId}) return err } diff --git a/util/file/options.go b/util/file/options.go new file mode 100644 index 00000000..8c2a6f3b --- /dev/null +++ b/util/file/options.go @@ -0,0 +1,15 @@ +package file + +import "context" + +type Options struct { + Context context.Context +} + +type Option func(o *Options) + +func WithContext(ctx context.Context) Option { + return func(o *Options) { + o.Context = ctx + } +}