Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/v2-feature-actor' into v2-featur…
Browse files Browse the repository at this point in the history
…e-actor
  • Loading branch information
dobyte committed Oct 8, 2024
2 parents aaab9d6 + 94bed47 commit 3065f6c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
19 changes: 19 additions & 0 deletions cluster/node/actor.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type Actor struct {
rw sync.RWMutex // 锁
mailbox chan Context // 邮箱
fnChan chan func() // 调用函数
binds sync.Map // 绑定的用户
}

// ID 获取Actor的ID
Expand Down Expand Up @@ -123,6 +124,13 @@ func (a *Actor) Destroy() {

a.processor.Destroy()

a.scheduler.batchUnbindActor(func(relations map[int64]map[string]*Actor) {
a.binds.Range(func(uid, _ any) bool {
delete(relations[uid.(int64)], a.Kind())
return true
})
})

a.rw.Lock()
defer a.rw.Unlock()

Expand All @@ -131,6 +139,17 @@ func (a *Actor) Destroy() {
close(a.fnChan)
}

// 绑定用户
func (a *Actor) bindUser(uid int64) {
a.binds.Store(uid, struct{}{})
}

// 解绑用户
func (a *Actor) unbindUser(uid int64) bool {
_, ok := a.binds.LoadAndDelete(uid)
return ok
}

// 分发
func (a *Actor) dispatch() {
go func() {
Expand Down
25 changes: 23 additions & 2 deletions cluster/node/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ func (s *Scheduler) bindActor(uid int64, kind, id string) error {
return errors.ErrNotFoundActor
}

act.bindUser(uid)

s.rw.Lock()
defer s.rw.Unlock()

Expand All @@ -133,9 +135,28 @@ func (s *Scheduler) unbindActor(uid int64, kind string) {
s.rw.Lock()
defer s.rw.Unlock()

if _, ok := s.relations[uid]; ok {
delete(s.relations[uid], kind)
relations, ok := s.relations[uid]
if !ok {
return
}

act, ok := relations[kind]
if !ok {
return
}

if ok = act.unbindUser(uid); !ok {
return
}

delete(s.relations[uid], kind)
}

// 批量解绑Actor
func (s *Scheduler) batchUnbindActor(fn func(relations map[int64]map[string]*Actor)) {
s.rw.Lock()
fn(s.relations)
s.rw.Unlock()
}

// 获取用户绑定的Actor
Expand Down

0 comments on commit 3065f6c

Please sign in to comment.