新增:trylock 尝试获取锁,并立即返回加锁结果

This commit is contained in:
2023-10-21 11:57:29 +08:00
parent 4fc7121b28
commit b80dc78cce
4 changed files with 36 additions and 18 deletions

View File

@@ -153,7 +153,7 @@ func (c *Client) ping() {
c.send("ping")
}
//Publish -------------------------------------- pub-sub --------------------------------------
// Publish -------------------------------------- pub-sub --------------------------------------
func (c *Client) Publish(topic string, message string) error {
return c.send("publish", topic, message)
}
@@ -168,10 +168,12 @@ func (c *Client) Delay(topic string, message string, delay int) error {
/*
Timer
func (c *Client) Timer(topic string, expr string, fun func()) {
func (c *Client) Timer(topic string, expr string, fun func()) {
c.timerFun[topic] = fun
c.send("timer", topic, expr, "x")
}*/
}
*/
func (c *Client) Timer(topic string, fun func()) {
if fun != nil {
c.timerFun[topic] = fun
@@ -195,28 +197,37 @@ func (c *Client) Close() {
c.conn.Close()
}
// TryLock
func TryLock(key string, duration int) {
/*uuid := uuid.New()
TODO
return Lock{Key: key, Value: uuid}*/
}
// Lock Key
func (c *Client) Lock(key string, duration int) Lock {
v := uuid.New()
c.send("v", key, v, strconv.Itoa(duration))
uuid := uuid.New()
c.send("uuid", key, uuid, strconv.Itoa(duration))
lockChan := make(chan int, 2)
go func() {
c.wlock.Lock()
defer c.wlock.Unlock()
c.lockFlag[v] = &Lock{
c.lockFlag[uuid] = &Lock{
Key: key,
Value: v,
Value: uuid,
flagChan: lockChan,
}
}()
select {
case <-lockChan:
log.Println("v-ok", time.Now().UnixNano()/1e6, v)
log.Println("lock-ok", time.Now().UnixNano()/1e6, uuid)
}
return Lock{Key: key, Value: v}
return Lock{Key: key, Value: uuid}
}
func (c *Client) Unlock(l Lock) {

View File

@@ -203,21 +203,21 @@ func messageHandler(v Message) {
}
Bus.shutdown()
}
case "lock":
case "lock", "trylock":
// lock key uuid 5
if len(rcmd) != 4 {
c.send("-Error: lock para number![" + strings.Join(rcmd, " ") + "]")
return
}
d, _ := strconv.Atoi(rcmd[3])
Bus._lock(&Lock{key: rcmd[1], uuid: rcmd[2], duration: d})
Bus._lock(&Lock{cmd: cmd, key: rcmd[1], uuid: rcmd[2], duration: d})
case "unlock":
// unlock key uuid
if len(rcmd) != 3 {
c.send("-Error: unlock para number![" + strings.Join(rcmd, " ") + "]")
return
}
Bus._unlock(Lock{key: rcmd[1], uuid: rcmd[2]})
Bus._unlock(Lock{cmd: cmd, key: rcmd[1], uuid: rcmd[2]})
default:
c.send("-Error: default not supported:[" + strings.Join(rcmd, " ") + "]")
return

View File

@@ -95,9 +95,10 @@ type ZConn struct { //ZConn
}
type Lock struct {
key string
uuid string
duration int
cmd string // lock|trylock|unlock
key string // lock key
uuid string // apply for unique identification
duration int // lock duration (seconds)
timer *time.Timer
start int64
//stop time.Time
@@ -432,8 +433,13 @@ func (s *ZBus) _lock(lock *Lock) {
}
}()
} else {
switch lock.cmd {
case "trylock": // send trylock fail message
s.broadcast("trylock", lock.uuid)
case "lock":
s.locks[lock.key] = append(locks, lock)
}
}
}
func (s *ZBus) _unlock(l Lock) {
locks := s.locks[l.key]

View File

@@ -161,6 +161,7 @@ func (s *ZBus) loadLock() {
}
s._lock(&Lock{
cmd: "lock",
key: split[0],
uuid: split[1],
duration: duration,