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

     1  package cluster
     2  
     3  import (
     4  	"sync/atomic"
     5  
     6  	"github.com/asynkron/protoactor-go/actor"
     7  )
     8  
     9  // Kind represents the kinds of actors a cluster can manage
    10  type Kind struct {
    11  	Kind            string
    12  	Props           *actor.Props
    13  	StrategyBuilder func(*Cluster) MemberStrategy
    14  }
    15  
    16  // NewKind creates a new instance of a kind
    17  func NewKind(kind string, props *actor.Props) *Kind {
    18  	// add cluster middleware
    19  	p := props.Clone(withClusterReceiveMiddleware())
    20  	return &Kind{
    21  		Kind:            kind,
    22  		Props:           p,
    23  		StrategyBuilder: nil,
    24  	}
    25  }
    26  
    27  func (k *Kind) WithMemberStrategy(strategyBuilder func(*Cluster) MemberStrategy) {
    28  	k.StrategyBuilder = strategyBuilder
    29  }
    30  
    31  func (k *Kind) Build(cluster *Cluster) *ActivatedKind {
    32  	var strategy MemberStrategy = nil
    33  	if k.StrategyBuilder != nil {
    34  		strategy = k.StrategyBuilder(cluster)
    35  	}
    36  
    37  	return &ActivatedKind{
    38  		Kind:     k.Kind,
    39  		Props:    k.Props,
    40  		Strategy: strategy,
    41  	}
    42  }
    43  
    44  type ActivatedKind struct {
    45  	Kind     string
    46  	Props    *actor.Props
    47  	Strategy MemberStrategy
    48  	count    int32
    49  }
    50  
    51  func (ak *ActivatedKind) Inc() {
    52  	atomic.AddInt32(&ak.count, 1)
    53  }
    54  
    55  func (ak *ActivatedKind) Dev() {
    56  	atomic.AddInt32(&ak.count, -1)
    57  }