add using lazy connect #361
| @@ -4,7 +4,7 @@ import ( | |||||||
| 	"context" | 	"context" | ||||||
| 	"sort" | 	"sort" | ||||||
| 	"strings" | 	"strings" | ||||||
| 	"sync" | 	"sync/atomic" | ||||||
| 	"time" | 	"time" | ||||||
|  |  | ||||||
| 	cache "github.com/patrickmn/go-cache" | 	cache "github.com/patrickmn/go-cache" | ||||||
| @@ -40,8 +40,7 @@ type memoryStore struct { | |||||||
| 	funcDelete  store.FuncDelete | 	funcDelete  store.FuncDelete | ||||||
| 	store       *cache.Cache | 	store       *cache.Cache | ||||||
| 	opts        store.Options | 	opts        store.Options | ||||||
| 	isConnected bool | 	isConnected atomic.Int32 | ||||||
| 	mutex       sync.Mutex |  | ||||||
| } | } | ||||||
|  |  | ||||||
| func (m *memoryStore) key(prefix, key string) string { | func (m *memoryStore) key(prefix, key string) string { | ||||||
| @@ -277,8 +276,6 @@ func (m *memoryStore) fnList(ctx context.Context, opts ...store.ListOption) ([]s | |||||||
| } | } | ||||||
|  |  | ||||||
| func (m *memoryStore) connect(ctx context.Context) error { | func (m *memoryStore) connect(ctx context.Context) error { | ||||||
| 	m.mutex.Lock() | 	m.isConnected.CompareAndSwap(0, 1) | ||||||
| 	defer m.mutex.Unlock() |  | ||||||
| 	m.isConnected = true |  | ||||||
| 	return nil | 	return nil | ||||||
| } | } | ||||||
|   | |||||||
| @@ -2,7 +2,7 @@ package store | |||||||
|  |  | ||||||
| import ( | import ( | ||||||
| 	"context" | 	"context" | ||||||
| 	"sync" | 	"sync/atomic" | ||||||
|  |  | ||||||
| 	"go.unistack.org/micro/v3/options" | 	"go.unistack.org/micro/v3/options" | ||||||
| ) | ) | ||||||
| @@ -16,8 +16,7 @@ type noopStore struct { | |||||||
| 	funcList    FuncList | 	funcList    FuncList | ||||||
| 	funcDelete  FuncDelete | 	funcDelete  FuncDelete | ||||||
| 	opts        Options | 	opts        Options | ||||||
| 	isConnected bool | 	isConnected atomic.Int32 | ||||||
| 	mutex       sync.Mutex |  | ||||||
| } | } | ||||||
|  |  | ||||||
| func NewStore(opts ...Option) *noopStore { | func NewStore(opts ...Option) *noopStore { | ||||||
| @@ -173,16 +172,13 @@ func (n *noopStore) Options() Options { | |||||||
| } | } | ||||||
|  |  | ||||||
| func (n *noopStore) connect(ctx context.Context) error { | func (n *noopStore) connect(ctx context.Context) error { | ||||||
| 	n.mutex.Lock() | 	if n.isConnected.CompareAndSwap(0, 1) { | ||||||
| 	defer n.mutex.Unlock() | 		select { | ||||||
| 	if n.isConnected { | 		case <-ctx.Done(): | ||||||
| 		return nil | 			return ctx.Err() | ||||||
| 	} | 		default: | ||||||
| 	n.isConnected = true | 		} | ||||||
| 	select { |  | ||||||
| 	case <-ctx.Done(): |  | ||||||
| 		return ctx.Err() |  | ||||||
| 	default: |  | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	return nil | 	return nil | ||||||
| } | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user