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