Documentation
¶
Index ¶
- Variables
- func CallFuncAs[T any](c ICluster, fc *FuncSpec) (T, error)
- func GetResultAs[T any](fs *FuncSpec) (T, error)
- type Caller
- type Cluster
- func (c *Cluster) AddJobTracker(v JobTracker) error
- func (c *Cluster) CallFunc(f *FuncSpec) (interface{}, error)deprecated
- func (c *Cluster) Close()
- func (c *Cluster) GetAliveNodeCount() int
- func (c *Cluster) GetAliveNodeNames() (aliveNames []string)
- func (c *Cluster) GetAllNodeCount() int
- func (c *Cluster) GetAllNodeNames() (allNames []string)
- func (c *Cluster) GetGRPCClient(nodeName string) (grpc.ClientConnInterface, error)
- func (c *Cluster) GetLeaderName() string
- func (c *Cluster) GetLeaderNode() *Node
- func (c *Cluster) GetLostNodeNames() (lostNames []string)
- func (c *Cluster) GetMyAddress() string
- func (c *Cluster) GetMyName() string
- func (c *Cluster) GetMyNode() *Node
- func (c *Cluster) GetMyTerm() int
- func (c *Cluster) GetNodeByName(name string) (node *Node)
- func (c *Cluster) IsCandidate() bool
- func (c *Cluster) IsClose() bool
- func (c *Cluster) IsClosed() bool
- func (c *Cluster) IsEnable() bool
- func (c *Cluster) IsFollower() bool
- func (c *Cluster) IsLeader() bool
- func (c *Cluster) IsReady() bool
- func (c *Cluster) Name() string
- func (c *Cluster) RegisterFunc(funcName string, fn func(data interface{}) (interface{}, error))
- func (c *Cluster) RegisterGRPCService(sd *grpc.ServiceDesc, ss interface{}) error
- func (c *Cluster) RemoveJobTracker(v JobTracker)
- func (c *Cluster) Start() error
- type ClusterOption
- func WithClientStreamInterceptors(interceptors ...grpc.StreamClientInterceptor) ClusterOption
- func WithClientUnaryInterceptors(interceptors ...grpc.UnaryClientInterceptor) ClusterOption
- func WithLogger(logger logger.ILog) ClusterOption
- func WithServerStreamInterceptors(interceptors ...grpc.StreamServerInterceptor) ClusterOption
- func WithServerUnaryInterceptors(interceptors ...grpc.UnaryServerInterceptor) ClusterOption
- type Config
- type DefaultCaller
- type DefaultJobTracker
- type FuncSpec
- type ICluster
- type JobTracker
- type JobTrackerConfig
- type Node
- type NodeInfo
- type RedisDiscovery
- type ReplicasDiscovery
- type TLSConfig
Constants ¶
This section is empty.
Variables ¶
var ErrResultNotReady = fmt.Errorf("remote call result not ready")
Functions ¶
func GetResultAs ¶
Types ¶
type Cluster ¶
type Cluster struct {
Redis redisv2.RedisClient `autowired:"" conditional_on_property:"default.cluster.mode=redis"` // redis
// contains filtered or unexported fields
}
func NewCluster ¶
func NewCluster(config Config, options ...ClusterOption) (*Cluster, error)
func NewClusterWithArgs ¶
func NewClusterWithArgs(config Config, options ...ClusterOption) (*Cluster, error)
func (*Cluster) AddJobTracker ¶
func (c *Cluster) AddJobTracker(v JobTracker) error
func (*Cluster) GetAliveNodeCount ¶
func (*Cluster) GetAliveNodeNames ¶
func (*Cluster) GetAllNodeCount ¶
func (*Cluster) GetAllNodeNames ¶
func (*Cluster) GetGRPCClient ¶
func (c *Cluster) GetGRPCClient(nodeName string) (grpc.ClientConnInterface, error)
func (*Cluster) GetLeaderName ¶
func (*Cluster) GetLeaderNode ¶
func (*Cluster) GetLostNodeNames ¶
func (*Cluster) GetMyAddress ¶
func (*Cluster) GetNodeByName ¶
func (*Cluster) IsCandidate ¶
func (*Cluster) IsFollower ¶
func (*Cluster) RegisterFunc ¶
func (*Cluster) RegisterGRPCService ¶
func (c *Cluster) RegisterGRPCService(sd *grpc.ServiceDesc, ss interface{}) error
func (*Cluster) RemoveJobTracker ¶
func (c *Cluster) RemoveJobTracker(v JobTracker)
type ClusterOption ¶
type ClusterOption func(*Cluster)
func WithClientStreamInterceptors ¶
func WithClientStreamInterceptors(interceptors ...grpc.StreamClientInterceptor) ClusterOption
WithClientStreamInterceptors appends user gRPC stream interceptors to cluster node clients, after the built-in traceId interceptor.
func WithClientUnaryInterceptors ¶
func WithClientUnaryInterceptors(interceptors ...grpc.UnaryClientInterceptor) ClusterOption
WithClientUnaryInterceptors appends user gRPC unary interceptors to cluster node clients, after the built-in traceId interceptor.
func WithLogger ¶
func WithLogger(logger logger.ILog) ClusterOption
WithLogger sets the logger used by the cluster.
func WithServerStreamInterceptors ¶
func WithServerStreamInterceptors(interceptors ...grpc.StreamServerInterceptor) ClusterOption
WithServerStreamInterceptors appends user gRPC stream interceptors to the cluster server, after the built-in traceId interceptor.
func WithServerUnaryInterceptors ¶
func WithServerUnaryInterceptors(interceptors ...grpc.UnaryServerInterceptor) ClusterOption
WithServerUnaryInterceptors appends user gRPC unary interceptors to the cluster server, after the built-in traceId interceptor.
type Config ¶
type Config struct {
Mode string `yaml:"mode" default:"cluster" json:"mode"`
Timeout time.Duration `yaml:"timeout" default:"5s" json:"timeout"`
Enable string `yaml:"enable" default:"true" json:"enable"`
TraceIDKey string `yaml:"traceIdKey" default:"X-Request-ID" json:"traceIdKey"`
TLS TLSConfig `yaml:"tls" json:"tls"`
Nodes []*struct {
Name string
IP string
Port int
Local bool
} `yaml:"nodes" json:"nodes"`
RedisDiscovery RedisDiscovery `yaml:"redisDiscovery" json:"redisDiscovery"`
ReplicasDiscovery ReplicasDiscovery `yaml:"replicasDiscovery" json:"replicasDiscovery"`
}
type DefaultCaller ¶
type DefaultCaller struct {
}
func (*DefaultCaller) MasterCall ¶
func (dc *DefaultCaller) MasterCall()
func (*DefaultCaller) OnStartedFollowing ¶
func (dc *DefaultCaller) OnStartedFollowing(leaderName string)
func (*DefaultCaller) OnStartedLeading ¶
func (dc *DefaultCaller) OnStartedLeading()
func (*DefaultCaller) OnStoppedFollowing ¶
func (dc *DefaultCaller) OnStoppedFollowing()
func (*DefaultCaller) OnStoppedLeading ¶
func (dc *DefaultCaller) OnStoppedLeading()
func (*DefaultCaller) SlaverCall ¶
func (dc *DefaultCaller) SlaverCall(leaderName string)
type DefaultJobTracker ¶
type DefaultJobTracker struct {
Interval int
Blocking bool
// contains filtered or unexported fields
}
func NewDefaultJobTracker ¶
func NewDefaultJobTracker(interval int, caller ...Caller) *DefaultJobTracker
func NewJobTracker ¶
func NewJobTracker(config JobTrackerConfig, caller ...Caller) *DefaultJobTracker
NewJobTracker creates a tracker with explicit scheduling options.
func (*DefaultJobTracker) Close ¶
func (t *DefaultJobTracker) Close()
func (*DefaultJobTracker) Name ¶
func (t *DefaultJobTracker) Name() string
func (*DefaultJobTracker) OnStartedFollowing ¶
func (t *DefaultJobTracker) OnStartedFollowing(leaderName string)
func (*DefaultJobTracker) OnStartedLeading ¶
func (t *DefaultJobTracker) OnStartedLeading()
func (*DefaultJobTracker) OnStoppedFollowing ¶
func (t *DefaultJobTracker) OnStoppedFollowing()
func (*DefaultJobTracker) OnStoppedLeading ¶
func (t *DefaultJobTracker) OnStoppedLeading()
type FuncSpec ¶
type FuncSpec struct {
// contains filtered or unexported fields
}
func NewAsyncFuncSpec ¶
func NewAsyncFuncSpec(nodeName, funcName string, param interface{}, timeout time.Duration) *FuncSpec
NewAsyncFuncSpec 异步调用,timeout为超时时间
func NewFuncSpec ¶
NewFuncSpec 同步调用,timeout是同步超时时间
func (*FuncSpec) GetTraceId ¶
func (*FuncSpec) IgnoreNotReady ¶
func (*FuncSpec) SetTraceId ¶
type ICluster ¶
type ICluster interface {
Name() string // 名称
Start() error // 启动
Close() // 关闭
IsClose() bool // 集群是否关闭
IsReady() bool // 集群是否就绪
IsLeader() bool // 当前节点是否是领导人
IsCandidate() bool // 当前节点是否是候选人
IsFollower() bool // 当前节点是否是群众
GetLeaderNode() *Node // 获取当前的主节点
GetLeaderName() string // 获取leader名称
GetMyNode() *Node // 获取当前本节点
GetNodeByName(name string) *Node // 根据名称获取节点
GetMyAddress() string // 获取当前节点通信地址
GetMyName() string // 获得当前本节点名称
GetMyTerm() int // 获取当前本节点任期
GetAllNodeNames() []string // 获取所有节点名称
GetAllNodeCount() int // 获取所有节点的数量
GetAliveNodeNames() []string // 获取所有活着的节点名称
GetAliveNodeCount() int // 获取在线节点数量
GetLostNodeNames() []string // 获取所有失联节点名称
AddJobTracker(v JobTracker) error // add scheduler
RemoveJobTracker(v JobTracker) // remove scheduler
RegisterFunc(funcName string, fn func(data interface{}) (interface{}, error)) // registerFunc
RegisterGRPCService(sd *grpc.ServiceDesc, ss interface{}) error // registerGRPCService
GetGRPCClient(nodeName string) (grpc.ClientConnInterface, error) // getGRPCClient
// Deprecated: Use cluster.CallFuncAs[T] instead to avoid deserialization failures with remote calls.
CallFunc(fc *FuncSpec) (interface{}, error) // callFunc
}
type JobTracker ¶
type JobTracker interface {
Name() string
// OnStartedLeading is called when a LeaderElector client starts leading
OnStartedLeading()
// OnStoppedLeading is called when a LeaderElector client stops leading
OnStoppedLeading()
// OnStartedFollowing is called when a LeaderElector client starts following
// leaderName is the name of leader
OnStartedFollowing(leaderName string)
// OnStartedFollowing is called when a LeaderElector client stops following
OnStoppedFollowing()
}
type JobTrackerConfig ¶
type JobTrackerConfig struct {
Interval int
// Blocking runs MasterCall and SlaverCall synchronously in the scheduler
// goroutine. A slow call delays the next tick. The default is false, which
// keeps the existing asynchronous behavior.
Blocking bool
}
JobTrackerConfig configures DefaultJobTracker.
type NodeInfo ¶
type NodeInfo struct {
Name string `json:"name"`
Address string `json:"address"`
Timestamp int64 `json:"timestamp"`
}
NodeInfo 节点信息结构
type RedisDiscovery ¶
type RedisDiscovery struct {
BeanName string `yaml:"beanName"` // 如果为空,则Ioc分配
DataPath string `yaml:"dataPath"` // redis key 前缀
Port int `yaml:"port" default:"8081"` // 节点通信端口
ElectionInterval time.Duration `yaml:"electionInterval" default:"15s"` //多久进行一次选主/续约
ElectionPeriod time.Duration `yaml:"electionPeriod" default:"30s"` //选主/续约后有效租期时间
SyncLeaderInterval time.Duration `yaml:"syncLeaderInterval" default:"10s"` //多久同步一次leader
NodeRegisterTTL time.Duration `yaml:"nodeRegisterTTL" default:"60s"` // 节点注册信息的过期时间
NodeSyncInterval time.Duration `yaml:"nodeSyncInterval" default:"30s"` // 多久同步一次节点信息
NodeHeartbeatPeriod time.Duration `yaml:"nodeHeartbeatPeriod" default:"20s"` // 节点心跳续约周期
}