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

     1  /*
     2  	Copyright (C) 2017 - 2022 Asynkron.se <http://www.asynkron.se>
     3  */
     4  
     5  package remote
     6  
     7  import (
     8  	"sync"
     9  
    10  	"github.com/asynkron/gofun/set"
    11  )
    12  
    13  // TODO: document it
    14  type BlockList struct {
    15  	mu             *sync.RWMutex
    16  	blockedMembers *set.ImmutableSet[string]
    17  }
    18  
    19  func NewBlockList() *BlockList {
    20  	blocklist := BlockList{
    21  		mu:             &sync.RWMutex{},
    22  		blockedMembers: set.NewImmutable[string](),
    23  	}
    24  	return &blocklist
    25  }
    26  
    27  func (bl *BlockList) BlockedMembers() set.Set[string] {
    28  	return bl.blockedMembers
    29  }
    30  
    31  // Block adds the given memberID list to the BlockList
    32  func (bl *BlockList) Block(memberIDs ...string) {
    33  	// acquire our mutual exclusion primitive
    34  	bl.mu.Lock()
    35  	defer bl.mu.Unlock()
    36  
    37  	bl.blockedMembers = bl.blockedMembers.AddRange(memberIDs...)
    38  }
    39  
    40  // IsBlocked returns true if the given memberID string has been
    41  // ever added to the BlockList
    42  func (bl *BlockList) IsBlocked(memberID string) bool {
    43  	// acquire our mutual exclusion primitive for reading
    44  	return bl.blockedMembers.Contains(memberID)
    45  }
    46  
    47  // Len returns the number of blocked members
    48  func (bl *BlockList) Len() int {
    49  	bl.mu.RLock()
    50  	defer bl.mu.RUnlock()
    51  	return bl.blockedMembers.Size()
    52  }