fix store context issues
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
@@ -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...)
|
||||
}
|
||||
|
@@ -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
|
||||
|
@@ -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 {
|
||||
|
@@ -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 {
|
||||
|
Reference in New Issue
Block a user