diff --git a/api/server/acme/certmagic/storage.go b/api/server/acme/certmagic/storage.go index 384d1b97..ea8b4b1b 100644 --- a/api/server/acme/certmagic/storage.go +++ b/api/server/acme/certmagic/storage.go @@ -52,14 +52,14 @@ func (s *storage) Store(key string, value []byte) error { Key: key, Value: buf.Bytes(), } - return s.store.Write(r) + return s.store.Write(s.store.Options().Context, r) } func (s *storage) Load(key string) ([]byte, error) { if !s.Exists(key) { return nil, certmagic.ErrNotExist(errors.New(key + " doesn't exist")) } - records, err := s.store.Read(key) + records, err := s.store.Read(s.store.Options().Context, key) if err != nil { return nil, err } @@ -77,18 +77,18 @@ func (s *storage) Load(key string) ([]byte, error) { } func (s *storage) Delete(key string) error { - return s.store.Delete(key) + return s.store.Delete(s.store.Options().Context, key) } func (s *storage) Exists(key string) bool { - if _, err := s.store.Read(key); err != nil { + if _, err := s.store.Read(s.store.Options().Context, key); err != nil { return false } return true } func (s *storage) List(prefix string, recursive bool) ([]string, error) { - keys, err := s.store.List() + keys, err := s.store.List(s.store.Options().Context) if err != nil { return nil, err } @@ -116,7 +116,7 @@ func (s *storage) List(prefix string, recursive bool) ([]string, error) { } func (s *storage) Stat(key string) (certmagic.KeyInfo, error) { - records, err := s.store.Read(key) + records, err := s.store.Read(s.store.Options().Context, key) if err != nil { return certmagic.KeyInfo{}, err } diff --git a/service.go b/service.go index 6c740a36..01636c6e 100644 --- a/service.go +++ b/service.go @@ -86,7 +86,7 @@ func (s *service) Init(opts ...Option) { } if s.opts.Store != nil { - if err := s.opts.Store.Init(); err != nil { + if err := s.opts.Store.Init(s.opts.Context); err != nil { logger.Fatalf("[cmd] init failed: %v", err) } } diff --git a/util/scope/scope.go b/util/scope/scope.go index 4467d08b..ad01334e 100644 --- a/util/scope/scope.go +++ b/util/scope/scope.go @@ -1,6 +1,7 @@ package scope import ( + "context" "fmt" "github.com/unistack-org/micro/v3/store" @@ -23,22 +24,22 @@ func (s *Scope) Options() store.Options { return o } -func (s *Scope) Read(key string, opts ...store.ReadOption) ([]*store.Record, error) { +func (s *Scope) Read(ctx context.Context, key string, opts ...store.ReadOption) ([]*store.Record, error) { key = fmt.Sprintf("%v/%v", s.prefix, key) - return s.Store.Read(key, opts...) + return s.Store.Read(ctx, key, opts...) } -func (s *Scope) Write(r *store.Record, opts ...store.WriteOption) error { +func (s *Scope) Write(ctx context.Context, r *store.Record, opts ...store.WriteOption) error { r.Key = fmt.Sprintf("%v/%v", s.prefix, r.Key) - return s.Store.Write(r, opts...) + return s.Store.Write(ctx, r, opts...) } -func (s *Scope) Delete(key string, opts ...store.DeleteOption) error { +func (s *Scope) Delete(ctx context.Context, key string, opts ...store.DeleteOption) error { key = fmt.Sprintf("%v/%v", s.prefix, key) - return s.Store.Delete(key, opts...) + return s.Store.Delete(ctx, key, opts...) } -func (s *Scope) List(opts ...store.ListOption) ([]string, error) { +func (s *Scope) List(ctx context.Context, opts ...store.ListOption) ([]string, error) { var lops store.ListOptions for _, o := range opts { o(&lops) @@ -47,5 +48,5 @@ func (s *Scope) List(opts ...store.ListOption) ([]string, error) { key := fmt.Sprintf("%v/%v", s.prefix, lops.Prefix) opts = append(opts, store.ListPrefix(key)) - return s.Store.List(opts...) + return s.Store.List(ctx, opts...) } diff --git a/util/sync/manager.go b/util/sync/manager.go index b43b19b8..c9dc6938 100644 --- a/util/sync/manager.go +++ b/util/sync/manager.go @@ -50,7 +50,7 @@ func (c *syncStore) processQueue(index int) { nr.Expiry = time.Until(ir.expiresAt) } // Todo = internal queue also has to hold the corresponding store.WriteOptions - if err := c.syncOpts.Stores[index+1].Write(nr); err != nil { + if err := c.syncOpts.Stores[index+1].Write(c.storeOpts.Context, nr); err != nil { // some error, so queue for retry and bail q.PushBack(ir) return diff --git a/util/sync/sync.go b/util/sync/sync.go index 518fe454..38bf4b36 100644 --- a/util/sync/sync.go +++ b/util/sync/sync.go @@ -2,7 +2,7 @@ package sync import ( - "errors" + "context" "fmt" "sync" "time" @@ -42,23 +42,20 @@ func NewSync(opts ...Option) Sync { return c } -func (c *syncStore) Close() error { +func (c *syncStore) Close(ctx context.Context) error { return nil } // Init initialises the storeOptions -func (c *syncStore) Init(opts ...store.Option) error { +func (c *syncStore) Init(ctx context.Context, opts ...store.Option) error { for _, o := range opts { o(&c.storeOpts) } if len(c.syncOpts.Stores) == 0 { - return errors.New("the sync has no stores") - } - if c.storeOpts.Context == nil { - return errors.New("please provide a context to the sync. Cancelling the context signals that the sync is being disposed and syncs the sync") + return fmt.Errorf("the sync has no stores") } for _, s := range c.syncOpts.Stores { - if err := s.Init(); err != nil { + if err := s.Init(ctx); err != nil { return fmt.Errorf("Store %s failed to Init(): %w", s.String(), err) } } @@ -87,21 +84,21 @@ func (c *syncStore) String() string { return fmt.Sprintf("sync %v", backends) } -func (c *syncStore) List(opts ...store.ListOption) ([]string, error) { - return c.syncOpts.Stores[0].List(opts...) +func (c *syncStore) List(ctx context.Context, opts ...store.ListOption) ([]string, error) { + return c.syncOpts.Stores[0].List(ctx, opts...) } -func (c *syncStore) Read(key string, opts ...store.ReadOption) ([]*store.Record, error) { - return c.syncOpts.Stores[0].Read(key, opts...) +func (c *syncStore) Read(ctx context.Context, key string, opts ...store.ReadOption) ([]*store.Record, error) { + return c.syncOpts.Stores[0].Read(ctx, key, opts...) } -func (c *syncStore) Write(r *store.Record, opts ...store.WriteOption) error { - return c.syncOpts.Stores[0].Write(r, opts...) +func (c *syncStore) Write(ctx context.Context, r *store.Record, opts ...store.WriteOption) error { + return c.syncOpts.Stores[0].Write(ctx, r, opts...) } // Delete removes a key from the sync -func (c *syncStore) Delete(key string, opts ...store.DeleteOption) error { - return c.syncOpts.Stores[0].Delete(key, opts...) +func (c *syncStore) Delete(ctx context.Context, key string, opts ...store.DeleteOption) error { + return c.syncOpts.Stores[0].Delete(ctx, key, opts...) } func (c *syncStore) Sync() error { diff --git a/util/token/basic/basic.go b/util/token/basic/basic.go index 72818b48..77b89546 100644 --- a/util/token/basic/basic.go +++ b/util/token/basic/basic.go @@ -1,6 +1,7 @@ package basic import ( + "context" "encoding/json" "fmt" "time" @@ -46,7 +47,7 @@ func (b *Basic) Generate(acc *auth.Account, opts ...token.GenerateOption) (*toke // write to the store key := uuid.New().String() - err = b.store.Write(&store.Record{ + err = b.store.Write(context.Background(), &store.Record{ Key: fmt.Sprintf("%v%v", StorePrefix, key), Value: bytes, Expiry: options.Expiry, @@ -66,7 +67,7 @@ func (b *Basic) Generate(acc *auth.Account, opts ...token.GenerateOption) (*toke // Inspect a token func (b *Basic) Inspect(t string) (*auth.Account, error) { // lookup the token in the store - recs, err := b.store.Read(StorePrefix + t) + recs, err := b.store.Read(context.Background(), StorePrefix+t) if err == store.ErrNotFound { return nil, token.ErrInvalidToken } else if err != nil {