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

     1  package zk
     2  
     3  import (
     4  	"time"
     5  )
     6  
     7  const baseKey = `/protoactor`
     8  
     9  type Option func(*config)
    10  
    11  // WithAuth set zk auth
    12  func WithAuth(scheme string, credential string) Option {
    13  	return func(o *config) {
    14  		o.Auth = authConfig{Scheme: scheme, Credential: credential}
    15  	}
    16  }
    17  
    18  // WithBaseKey set actors base key
    19  func WithBaseKey(key string) Option {
    20  	return func(o *config) {
    21  		if isStrBlank(key) {
    22  			o.BaseKey = baseKey
    23  		} else {
    24  			o.BaseKey = formatBaseKey(key)
    25  		}
    26  	}
    27  }
    28  
    29  // WithSessionTimeout set zk session timeout
    30  func WithSessionTimeout(tm time.Duration) Option {
    31  	return func(o *config) {
    32  		o.SessionTimeout = tm
    33  	}
    34  }
    35  
    36  // WithRoleChangedListener triggered on self role changed
    37  func WithRoleChangedListener(l RoleChangedListener) Option {
    38  	return func(o *config) {
    39  		o.RoleChanged = l
    40  	}
    41  }
    42  
    43  func WithRoleChangedFunc(f OnRoleChangedFunc) Option {
    44  	return func(o *config) {
    45  		o.RoleChanged = f
    46  	}
    47  }
    48  
    49  func withEndpoints(e []string) Option {
    50  	return func(o *config) {
    51  		o.Endpoints = e
    52  	}
    53  }
    54  
    55  type authConfig struct {
    56  	Scheme     string
    57  	Credential string
    58  }
    59  
    60  func (za authConfig) isEmpty() bool {
    61  	return za.Scheme == "" && za.Credential == ""
    62  }
    63  
    64  type RoleChangedListener interface {
    65  	OnRoleChanged(RoleType)
    66  }
    67  
    68  type OnRoleChangedFunc func(RoleType)
    69  
    70  func (fn OnRoleChangedFunc) OnRoleChanged(rt RoleType) {
    71  	fn(rt)
    72  }
    73  
    74  type config struct {
    75  	BaseKey        string
    76  	Endpoints      []string
    77  	SessionTimeout time.Duration
    78  	Auth           authConfig
    79  	RoleChanged    RoleChangedListener
    80  }
    81  
    82  func defaultConfig() *config {
    83  	return &config{
    84  		BaseKey:        baseKey,
    85  		SessionTimeout: time.Second * 10,
    86  	}
    87  }