You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Open-IM-Server/internal/rpc/msg/lock.go

56 lines
1.5 KiB

2 years ago
package msg
import (
2 years ago
"context"
2 years ago
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/db/cache"
2 years ago
"time"
)
const GlOBLLOCK = "GLOBAL_LOCK"
2 years ago
type MessageLocker interface {
2 years ago
LockMessageTypeKey(ctx context.Context, clientMsgID, typeKey string) (err error)
UnLockMessageTypeKey(ctx context.Context, clientMsgID string, typeKey string) error
LockGlobalMessage(ctx context.Context, clientMsgID string) (err error)
UnLockGlobalMessage(ctx context.Context, clientMsgID string) (err error)
2 years ago
}
2 years ago
type LockerMessage struct {
2 years ago
cache cache.MsgModel
2 years ago
}
2 years ago
2 years ago
func NewLockerMessage(cache cache.MsgModel) *LockerMessage {
2 years ago
return &LockerMessage{cache: cache}
2 years ago
}
2 years ago
func (l *LockerMessage) LockMessageTypeKey(ctx context.Context, clientMsgID, typeKey string) (err error) {
2 years ago
for i := 0; i < 3; i++ {
2 years ago
err = l.cache.LockMessageTypeKey(ctx, clientMsgID, typeKey)
2 years ago
if err != nil {
time.Sleep(time.Millisecond * 100)
continue
} else {
break
}
}
return err
}
2 years ago
func (l *LockerMessage) LockGlobalMessage(ctx context.Context, clientMsgID string) (err error) {
for i := 0; i < 3; i++ {
2 years ago
err = l.cache.LockMessageTypeKey(ctx, clientMsgID, GlOBLLOCK)
if err != nil {
time.Sleep(time.Millisecond * 100)
continue
} else {
break
}
}
return err
2 years ago
}
2 years ago
func (l *LockerMessage) UnLockMessageTypeKey(ctx context.Context, clientMsgID string, typeKey string) error {
return l.cache.UnLockMessageTypeKey(ctx, clientMsgID, typeKey)
2 years ago
}
2 years ago
func (l *LockerMessage) UnLockGlobalMessage(ctx context.Context, clientMsgID string) error {
return l.cache.UnLockMessageTypeKey(ctx, clientMsgID, GlOBLLOCK)
}