123456789101112131415161718192021222324252627282930313233343536373839404142 |
- package common
- import (
- "app.yhyue.com/moapp/MessageCenter/rpc/internal/config"
- "fmt"
- "hash/fnv"
- "sync"
- )
- var UserLocksMap *UserLocks
- type UserLocks struct {
- locks []*sync.Mutex
- mutex sync.Mutex
- }
- func NewUserLocks() *UserLocks {
- locks := make([]*sync.Mutex, config.ConfigJson.ThreadCount)
- for i := range locks {
- locks[i] = &sync.Mutex{}
- }
- return &UserLocks{
- locks: locks,
- }
- }
- func (ul *UserLocks) GetLock(userID string) *sync.Mutex {
- hash := hashUserID(userID)
- ul.mutex.Lock()
- defer ul.mutex.Unlock()
- index := hash % uint32(len(ul.locks))
- fmt.Println("index", index)
- return ul.locks[index]
- }
- func hashUserID(userID string) uint32 {
- h := fnv.New32a()
- h.Write([]byte(userID))
- return h.Sum32()
- }
|