github.com/asynkron/protoactor-go@v0.0.0-20240308120642-ef91a6abee75/cluster/member_strategy.go (about)

     1  package cluster
     2  
     3  type MemberStrategy interface {
     4  	GetAllMembers() Members
     5  	AddMember(member *Member)
     6  	RemoveMember(member *Member)
     7  	GetPartition(key string) string
     8  	GetActivator(senderAddress string) string
     9  }
    10  
    11  type simpleMemberStrategy struct {
    12  	members Members
    13  	rr      *SimpleRoundRobin
    14  	rdv     *Rendezvous
    15  }
    16  
    17  func newDefaultMemberStrategy(cluster *Cluster, kind string) MemberStrategy {
    18  	ms := &simpleMemberStrategy{members: make(Members, 0)}
    19  	ms.rr = NewSimpleRoundRobin(MemberStrategy(ms))
    20  	ms.rdv = NewRendezvous()
    21  	return ms
    22  }
    23  
    24  func (m *simpleMemberStrategy) AddMember(member *Member) {
    25  	m.members = append(m.members, member)
    26  	m.rdv.UpdateMembers(m.members)
    27  }
    28  
    29  func (m *simpleMemberStrategy) UpdateMember(member *Member) {
    30  	for i, mb := range m.members {
    31  		if mb.Address() == member.Address() {
    32  			m.members[i] = member
    33  			return
    34  		}
    35  	}
    36  }
    37  
    38  func (m *simpleMemberStrategy) RemoveMember(member *Member) {
    39  	for i, mb := range m.members {
    40  		if mb.Address() == member.Address() {
    41  			m.members = append(m.members[:i], m.members[i+1:]...)
    42  			m.rdv.UpdateMembers(m.members)
    43  			return
    44  		}
    45  	}
    46  }
    47  
    48  func (m *simpleMemberStrategy) GetAllMembers() Members {
    49  	return m.members
    50  }
    51  
    52  func (m *simpleMemberStrategy) GetPartition(key string) string {
    53  	return m.rdv.GetByIdentity(key)
    54  }
    55  
    56  func (m *simpleMemberStrategy) GetActivator(senderAddress string) string {
    57  	return m.rr.GetByRoundRobin()
    58  }