@@ -21,7 +21,7 @@ type ConsulStorage struct {
2121 certmagic.Storage
2222 ConsulClient * consul.Client
2323 logger * zap.SugaredLogger
24- muLocks sync.Mutex
24+ muLocks sync.RWMutex
2525 locks map [string ]* consul.Lock
2626
2727 Address string `json:"address"`
@@ -53,12 +53,8 @@ func (cs *ConsulStorage) prefixKey(key string) string {
5353}
5454
5555// Lock acquires a distributed lock for the given key or blocks until it gets one
56- func (cs ConsulStorage ) Lock (ctx context.Context , key string ) error {
57- cs .muLocks .Lock ()
58- defer cs .muLocks .Unlock ()
59-
60- // if we already hold the lock, return early
61- if _ , exists := cs .locks [key ]; exists {
56+ func (cs * ConsulStorage ) Lock (ctx context.Context , key string ) error {
57+ if cs .IsLocked (key ) {
6258 return nil
6359 }
6460
@@ -88,8 +84,20 @@ func (cs ConsulStorage) Lock(ctx context.Context, key string) error {
8884 return nil
8985}
9086
87+ func (cs * ConsulStorage ) IsLocked (key string ) bool {
88+ cs .muLocks .RLock ()
89+ defer cs .muLocks .RUnlock ()
90+
91+ // if we already hold the lock, return early
92+ if _ , exists := cs .locks [key ]; exists {
93+ return true
94+ }
95+
96+ return false
97+ }
98+
9199// Unlock releases a specific lock
92- func (cs ConsulStorage ) Unlock (key string ) error {
100+ func (cs * ConsulStorage ) Unlock (key string ) error {
93101 cs .muLocks .Lock ()
94102 defer cs .muLocks .Unlock ()
95103
0 commit comments