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

View File

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

View File

@@ -95,9 +95,10 @@ type ZConn struct { //ZConn
} }
type Lock struct { type Lock struct {
key string cmd string // lock|trylock|unlock
uuid string key string // lock key
duration int uuid string // apply for unique identification
duration int // lock duration (seconds)
timer *time.Timer timer *time.Timer
start int64 start int64
//stop time.Time //stop time.Time
@@ -432,7 +433,12 @@ func (s *ZBus) _lock(lock *Lock) {
} }
}() }()
} else { } else {
s.locks[lock.key] = append(locks, lock) 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) { func (s *ZBus) _unlock(l Lock) {

View File

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