Fix MpoolLocker

Signed-off-by: Jakub Sztandera <kubuxu@protocol.ai>
This commit is contained in:
Jakub Sztandera
2020-08-11 19:32:31 +02:00
parent a467deede6
commit a45febc065
3 changed files with 42 additions and 25 deletions
+35
View File
@@ -0,0 +1,35 @@
package dtypes
import (
"context"
"sync"
"github.com/filecoin-project/go-address"
)
type MpoolLocker struct {
m map[address.Address]chan struct{}
lk sync.Mutex
}
func (ml *MpoolLocker) TakeLock(ctx context.Context, a address.Address) (func(), error) {
ml.lk.Lock()
if ml.m == nil {
ml.m = make(map[address.Address]chan struct{})
}
lk, ok := ml.m[a]
if !ok {
lk = make(chan struct{}, 1)
ml.m[a] = lk
}
ml.lk.Unlock()
select {
case lk <- struct{}{}:
case <-ctx.Done():
return nil, ctx.Err()
}
return func() {
<-lk
}, nil
}