github.com/neatlab/neatio@v1.7.3-0.20220425043230-d903e92fcc75/internal/neatapi/addrlock.go (about)

     1  package neatapi
     2  
     3  import (
     4  	"sync"
     5  
     6  	"github.com/neatlab/neatio/utilities/common"
     7  )
     8  
     9  type AddrLocker struct {
    10  	mu    sync.Mutex
    11  	locks map[common.Address]*sync.Mutex
    12  }
    13  
    14  func (l *AddrLocker) lock(address common.Address) *sync.Mutex {
    15  	l.mu.Lock()
    16  	defer l.mu.Unlock()
    17  	if l.locks == nil {
    18  		l.locks = make(map[common.Address]*sync.Mutex)
    19  	}
    20  	if _, ok := l.locks[address]; !ok {
    21  		l.locks[address] = new(sync.Mutex)
    22  	}
    23  	return l.locks[address]
    24  }
    25  
    26  func (l *AddrLocker) LockAddr(address common.Address) {
    27  	l.lock(address).Lock()
    28  }
    29  
    30  func (l *AddrLocker) UnlockAddr(address common.Address) {
    31  	l.lock(address).Unlock()
    32  }