micro/util/sync/sync.go

130 lines
3.2 KiB
Go
Raw Normal View History

// Package sync will sync multiple stores
2020-04-11 11:33:10 +03:00
package sync
import (
"context"
2020-04-11 11:33:10 +03:00
"fmt"
"sync"
"time"
"github.com/ef-ds/deque"
"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
}
func (c *syncStore) Connect(ctx context.Context) error {
return nil
}
func (c *syncStore) Disconnect(ctx context.Context) error {
return nil
}
func (c *syncStore) Close(ctx context.Context) error {
2020-04-11 11:33:10 +03:00
return nil
}
// Init initialises the storeOptions
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 {
return fmt.Errorf("the sync has no stores")
2020-04-11 11:33:10 +03:00
}
for _, s := range c.syncOpts.Stores {
if err := s.Init(); err != nil {
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
}
// Name returns the store name
func (c *syncStore) Name() string {
return c.storeOpts.Name
}
2020-04-11 11:33:10 +03:00
// 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)
}
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
}
func (c *syncStore) Exists(ctx context.Context, key string, opts ...store.ExistsOption) error {
return c.syncOpts.Stores[0].Exists(ctx, key)
2020-04-11 11:33:10 +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
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
}