cluster

package
v1.0.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 29, 2024 License: BSD-3-Clause Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const (
	EventChanCapacity = 256
	OpTimeout         = 5

	VerboseLv1 = 1
	VerboseLv2 = 2
)

Variables

View Source
var (
	ErrEmptyLease    = errors.New("empty lease")
	ErrNodeKeyExist  = errors.New("node key exist")
	ErrNoKeyDeleted  = errors.New("no key deleted")
	ErrEmptyEndpoint = errors.New("empty endpoint")
)

Functions

This section is empty.

Types

type EtcdClient

type EtcdClient struct {
	// contains filtered or unexported fields
}

EtcdClient 基于etcd的服务发现

func NewEtcdClient

func NewEtcdClient(endpoints, namespace, username, passwd string) *EtcdClient

func (*EtcdClient) Close

func (c *EtcdClient) Close()

func (*EtcdClient) DelKey

func (c *EtcdClient) DelKey(ctx context.Context, name string) error

DelKey 删除一个key

func (*EtcdClient) FormatKey

func (c *EtcdClient) FormatKey(name string) string

func (*EtcdClient) GetKey added in v1.0.4

func (c *EtcdClient) GetKey(ctx context.Context, name string) ([]byte, error)

GetKey 获取key的值

func (*EtcdClient) GetLeaseTTL

func (c *EtcdClient) GetLeaseTTL(ctx context.Context, leaseId int64) (int, error)

func (*EtcdClient) GetNode

func (c *EtcdClient) GetNode(ctx context.Context, name string) (*Node, error)

GetNode 获取节点信息

func (*EtcdClient) GrantLease

func (c *EtcdClient) GrantLease(ctx context.Context, ttl int32) (int64, error)

GrantLease 申请一个lease

func (*EtcdClient) Init

func (c *EtcdClient) Init(parentCtx context.Context) error

func (*EtcdClient) IsNodeExist

func (c *EtcdClient) IsNodeExist(ctx context.Context, name string) (bool, error)

IsNodeExist 节点是否存在

func (*EtcdClient) KeepAlive

func (c *EtcdClient) KeepAlive(ctx context.Context, stopChan chan struct{}, leaseId int64) error

KeepAlive lease保活,当lease撤销时此stopChan被激活

func (*EtcdClient) ListNodes

func (c *EtcdClient) ListNodes(ctx context.Context, prefix string) ([]Node, error)

ListNodes 列出目录下的所有节点

func (*EtcdClient) PutNode

func (c *EtcdClient) PutNode(ctx context.Context, name string, value any, leaseId int64) error

PutNode 设置节点信息

func (*EtcdClient) RegisterAndKeepAliveForever

func (c *EtcdClient) RegisterAndKeepAliveForever(ctx context.Context, name string, value any, ttl int32) (*NodeKeepAliveContext, error)

RegisterAndKeepAliveForever 注册一个节点,并永久保活

func (*EtcdClient) RegisterNode

func (c *EtcdClient) RegisterNode(rootCtx context.Context, name string, value any, ttl int32) (int64, error)

RegisterNode 注册一个节点信息,并返回一个ttl秒的lease

func (*EtcdClient) RevokeKeepAlive

func (c *EtcdClient) RevokeKeepAlive(ctx context.Context, regCtx *NodeKeepAliveContext) error

func (*EtcdClient) RevokeLease

func (c *EtcdClient) RevokeLease(ctx context.Context, leaseId int64) error

RevokeLease 撤销一个lease

func (*EtcdClient) SetVerbose

func (c *EtcdClient) SetVerbose(v int32)

func (*EtcdClient) WatchDir

func (c *EtcdClient) WatchDir(ctx context.Context, dir string) <-chan *NodeEvent

WatchDir 订阅目录下的节点变化

func (*EtcdClient) WatchDirTo

func (c *EtcdClient) WatchDirTo(ctx context.Context, dir string, nodeMap *NodeMap)

WatchDirTo 订阅目录下的所有节点变化, 并把节点变化更新到nodeMap

type Node

type Node struct {
	Type      string            `json:"type"`
	ID        uint32            `json:"id"`
	Status    uint32            `json:"status,omitempty"`
	PID       int               `json:"pid,omitempty"`
	Host      string            `json:"host,omitempty"`
	Interface string            `json:"interface,omitempty"`
	URI       string            `json:"uri,omitempty"`
	Args      map[string]string `json:"args,omitempty"`
}

Node 表示用于服务发现的节点信息

func NewNode

func NewNode(nodeType string, id uint32) Node

func (*Node) Clone added in v1.0.3

func (n *Node) Clone() Node

func (*Node) GetBool added in v1.0.3

func (n *Node) GetBool(key string) bool

func (*Node) GetFloat added in v1.0.3

func (n *Node) GetFloat(key string) float64

func (*Node) GetInt added in v1.0.3

func (n *Node) GetInt(key string) int

func (*Node) GetStr added in v1.0.4

func (n *Node) GetStr(key string) string

func (*Node) Set added in v1.0.3

func (n *Node) Set(key string, val any)

func (*Node) SetBool added in v1.0.3

func (n *Node) SetBool(key string, val bool)

func (*Node) SetFloat added in v1.0.3

func (n *Node) SetFloat(key string, val float64)

func (*Node) SetInt added in v1.0.3

func (n *Node) SetInt(key string, val int)

func (*Node) SetStr added in v1.0.4

func (n *Node) SetStr(key, val string)

func (*Node) String

func (n *Node) String() string

type NodeEvent

type NodeEvent struct {
	Type NodeEventType
	Key  string
	Node Node
}

NodeEvent 节点变化事件

func (NodeEvent) String

func (e NodeEvent) String() string

type NodeEventType

type NodeEventType int
const (
	EventUnknown NodeEventType = 0
	EventCreate  NodeEventType = 1
	EventUpdate  NodeEventType = 2
	EventDelete  NodeEventType = 3
)

func (NodeEventType) String

func (e NodeEventType) String() string

type NodeKeepAliveContext

type NodeKeepAliveContext struct {
	LeaseId    int64
	LeaseAlive atomic.Bool
	TTL        int32
	Name       string
	Value      any
	// contains filtered or unexported fields
}

NodeKeepAliveContext 用于注册并保活节点

func NewNodeKeepAliveContext

func NewNodeKeepAliveContext(name string, value any, ttl int32) *NodeKeepAliveContext

type NodeMap

type NodeMap struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

NodeMap 按服务类型区分的节点信息

func NewNodeMap

func NewNodeMap() *NodeMap

func (*NodeMap) AddNode added in v1.0.4

func (m *NodeMap) AddNode(node Node)

AddNode 添加一个节点

func (*NodeMap) Clear

func (m *NodeMap) Clear()

func (*NodeMap) Count

func (m *NodeMap) Count() int

Count 所有节点数量

func (*NodeMap) CountOf added in v1.0.4

func (m *NodeMap) CountOf(nodeType string) int

func (*NodeMap) DeleteNode

func (m *NodeMap) DeleteNode(nodeType string, id uint32)

DeleteNode 删除一个节点

func (*NodeMap) DeleteNodesOf added in v1.0.4

func (m *NodeMap) DeleteNodesOf(nodeType string)

DeleteNodesOf 删除某一类型的所有节点

func (*NodeMap) FindNodeOf added in v1.0.4

func (m *NodeMap) FindNodeOf(nodeType string, id uint32) int

func (*NodeMap) GetKeys

func (m *NodeMap) GetKeys() []string

func (*NodeMap) GetNodes

func (m *NodeMap) GetNodes(nodeType string) []Node

GetNodes 所有本类型的节点,不要修改返回值

func (*NodeMap) String

func (m *NodeMap) String() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL