-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathnode_service.go
More file actions
48 lines (38 loc) · 1.17 KB
/
node_service.go
File metadata and controls
48 lines (38 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package nodehub
import (
"context"
"github.com/joyparty/nodehub/cluster"
"github.com/joyparty/nodehub/logger"
"github.com/joyparty/nodehub/proto/nh"
"github.com/oklog/ulid/v2"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/emptypb"
)
// 节点管理服务,每个节点都会自动注册
type nodeService struct {
nh.UnimplementedNodeServer
node *Node
}
// 改变节点状态
func (ns *nodeService) ChangeState(_ context.Context, req *nh.ChangeStateRequest) (*emptypb.Empty, error) {
state := req.GetState()
if state == "" {
return nil, status.Error(codes.InvalidArgument, "state is empty")
}
return nh.EmptyReply, ns.node.ChangeState(cluster.NodeState(state))
}
// 关闭服务
func (ns *nodeService) Shutdown(context.Context, *emptypb.Empty) (*emptypb.Empty, error) {
logger.Info("node shutdown by rpc command")
ns.node.Shutdown()
return nh.EmptyReply, nil
}
// NewNodeClient 节点管理服务客户端
func NewNodeClient(registry *cluster.Registry, nodeID ulid.ULID) (nh.NodeClient, error) {
conn, err := registry.GetGRPCConn(nodeID)
if err != nil {
return nil, err
}
return nh.NewNodeClient(conn), nil
}