2020-04-11 11:33:10 +03:00
|
|
|
// Package syncs will sync multiple stores
|
|
|
|
package sync
|
|
|
|
|
|
|
|
import (
|
2020-09-17 15:41:49 +03:00
|
|
|
"context"
|
2020-04-11 11:33:10 +03:00
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ef-ds/deque"
|
2020-08-20 15:23:41 +03:00
|
|
|
"github.com/unistack-org/micro/v3/store"
|
2020-04-11 11:33:10 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// Sync implements a sync in for stores
|
|
|
|
type Sync interface {
|
|
|
|
// Implements the store interface
|
|
|
|
store.Store
|
|
|
|
// Force a full sync
|
|
|
|
Sync() error
|
|
|
|
}
|
2020-04-11 11:34:04 +03:00
|
|
|
|
2020-04-11 11:33:10 +03:00
|
|
|
type syncStore struct {
|
|
|
|
storeOpts store.Options
|
|
|
|
syncOpts Options
|
|
|
|
pendingWrites []*deque.Deque
|
|
|
|
pendingWriteTickers []*time.Ticker
|
|
|
|
sync.RWMutex
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewSync returns a new Sync
|
|
|
|
func NewSync(opts ...Option) Sync {
|
|
|
|
c := &syncStore{}
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&c.syncOpts)
|
|
|
|
}
|
|
|
|
if c.syncOpts.SyncInterval == 0 {
|
|
|
|
c.syncOpts.SyncInterval = 1 * time.Minute
|
|
|
|
}
|
|
|
|
if c.syncOpts.SyncMultiplier == 0 {
|
|
|
|
c.syncOpts.SyncMultiplier = 5
|
|
|
|
}
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2020-10-16 09:38:57 +03:00
|
|
|
func (c *syncStore) Connect(ctx context.Context) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *syncStore) Disconnect(ctx context.Context) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-17 15:41:49 +03:00
|
|
|
func (c *syncStore) Close(ctx context.Context) error {
|
2020-04-11 11:33:10 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Init initialises the storeOptions
|
2020-10-16 09:38:57 +03:00
|
|
|
func (c *syncStore) Init(opts ...store.Option) error {
|
2020-04-11 11:33:10 +03:00
|
|
|
for _, o := range opts {
|
|
|
|
o(&c.storeOpts)
|
|
|
|
}
|
|
|
|
if len(c.syncOpts.Stores) == 0 {
|
2020-09-17 15:41:49 +03:00
|
|
|
return fmt.Errorf("the sync has no stores")
|
2020-04-11 11:33:10 +03:00
|
|
|
}
|
|
|
|
for _, s := range c.syncOpts.Stores {
|
2020-10-16 09:38:57 +03:00
|
|
|
if err := s.Init(); err != nil {
|
2020-08-25 13:44:41 +03:00
|
|
|
return fmt.Errorf("Store %s failed to Init(): %w", s.String(), err)
|
2020-04-11 11:33:10 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
c.pendingWrites = make([]*deque.Deque, len(c.syncOpts.Stores)-1)
|
|
|
|
c.pendingWriteTickers = make([]*time.Ticker, len(c.syncOpts.Stores)-1)
|
|
|
|
for i := 0; i < len(c.pendingWrites); i++ {
|
|
|
|
c.pendingWrites[i] = deque.New()
|
|
|
|
c.pendingWrites[i].Init()
|
|
|
|
c.pendingWriteTickers[i] = time.NewTicker(c.syncOpts.SyncInterval * time.Duration(intpow(c.syncOpts.SyncMultiplier, int64(i))))
|
|
|
|
}
|
|
|
|
go c.syncManager()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Options returns the sync's store options
|
|
|
|
func (c *syncStore) Options() store.Options {
|
|
|
|
return c.storeOpts
|
|
|
|
}
|
|
|
|
|
|
|
|
// String returns a printable string describing the sync
|
|
|
|
func (c *syncStore) String() string {
|
|
|
|
backends := make([]string, len(c.syncOpts.Stores))
|
|
|
|
for i, s := range c.syncOpts.Stores {
|
|
|
|
backends[i] = s.String()
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("sync %v", backends)
|
|
|
|
}
|
|
|
|
|
2020-09-17 15:41:49 +03:00
|
|
|
func (c *syncStore) List(ctx context.Context, opts ...store.ListOption) ([]string, error) {
|
|
|
|
return c.syncOpts.Stores[0].List(ctx, opts...)
|
2020-04-11 11:33:10 +03:00
|
|
|
}
|
|
|
|
|
2020-12-10 22:08:56 +03:00
|
|
|
func (c *syncStore) Exists(ctx context.Context, key string) error {
|
|
|
|
return c.syncOpts.Stores[0].Exists(ctx, key)
|
2020-04-11 11:33:10 +03:00
|
|
|
}
|
|
|
|
|
2020-12-10 22:08:56 +03:00
|
|
|
func (c *syncStore) Read(ctx context.Context, key string, val interface{}, opts ...store.ReadOption) error {
|
|
|
|
return c.syncOpts.Stores[0].Read(ctx, key, val, opts...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *syncStore) Write(ctx context.Context, key string, val interface{}, opts ...store.WriteOption) error {
|
|
|
|
return c.syncOpts.Stores[0].Write(ctx, key, val, opts...)
|
2020-04-11 11:33:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete removes a key from the sync
|
2020-09-17 15:41:49 +03:00
|
|
|
func (c *syncStore) Delete(ctx context.Context, key string, opts ...store.DeleteOption) error {
|
|
|
|
return c.syncOpts.Stores[0].Delete(ctx, key, opts...)
|
2020-04-11 11:33:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *syncStore) Sync() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type internalRecord struct {
|
|
|
|
key string
|
|
|
|
value []byte
|
|
|
|
expiresAt time.Time
|
|
|
|
}
|