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

     1  package cluster
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/asynkron/protoactor-go/actor"
     7  )
     8  
     9  type GrainCallConfig struct {
    10  	RetryCount  int
    11  	Timeout     time.Duration
    12  	RetryAction func(n int) int
    13  	Context     actor.SenderContext
    14  }
    15  
    16  type GrainCallOption func(config *GrainCallConfig)
    17  
    18  var defaultGrainCallOptions *GrainCallConfig
    19  
    20  func DefaultGrainCallConfig(cluster *Cluster) *GrainCallConfig {
    21  	if defaultGrainCallOptions == nil {
    22  		defaultGrainCallOptions = NewGrainCallOptions(cluster)
    23  	}
    24  	return defaultGrainCallOptions
    25  }
    26  
    27  func NewGrainCallOptions(cluster *Cluster) *GrainCallConfig {
    28  	return &GrainCallConfig{
    29  		// TODO: set default in config
    30  		RetryCount: 3,
    31  		Context:    cluster.ActorSystem.Root,
    32  		Timeout:    cluster.Config.RequestTimeoutTime,
    33  		RetryAction: func(i int) int {
    34  			i++
    35  			time.Sleep(time.Duration(i * i * 50))
    36  			return i
    37  		},
    38  	}
    39  }
    40  
    41  func WithTimeout(timeout time.Duration) GrainCallOption {
    42  	return func(config *GrainCallConfig) {
    43  		config.Timeout = timeout
    44  	}
    45  }
    46  
    47  func WithRetryCount(count int) GrainCallOption {
    48  	return func(config *GrainCallConfig) {
    49  		config.RetryCount = count
    50  	}
    51  }
    52  
    53  func WithRetryAction(act func(i int) int) GrainCallOption {
    54  	return func(config *GrainCallConfig) {
    55  		config.RetryAction = act
    56  	}
    57  }
    58  
    59  func WithContext(ctx actor.SenderContext) GrainCallOption {
    60  	return func(config *GrainCallConfig) {
    61  		config.Context = ctx
    62  	}
    63  }
    64  
    65  type ClusterInit struct {
    66  	Identity *ClusterIdentity
    67  	Cluster  *Cluster
    68  }