Add Network.Services handler

This commit is contained in:
Asim Aslam 2019-09-30 07:51:13 +01:00
parent 1322fb0d9d
commit b90871c241
4 changed files with 200 additions and 29 deletions

View File

@ -123,3 +123,23 @@ func (n *Network) Routes(ctx context.Context, req *pbNet.RoutesRequest, resp *pb
return nil
}
// Services returns a list of services based on the routing table
func (n *Network) Services(ctx context.Context, req *pbNet.ServicesRequest, resp *pbNet.ServicesResponse) error {
routes, err := n.Network.Options().Router.Table().List()
if err != nil {
return errors.InternalServerError("go.micro.network", "failed to list services: %s", err)
}
services := make(map[string]bool)
for _, route := range routes {
if _, ok := services[route.Service]; ok {
continue
}
services[route.Service] = true
resp.Services = append(resp.Services, route.Service)
}
return nil
}

View File

@ -35,9 +35,14 @@ var _ server.Option
// Client API for Network service
type NetworkService interface {
// Returns the entire network graph
Graph(ctx context.Context, in *GraphRequest, opts ...client.CallOption) (*GraphResponse, error)
// Returns a list of known nodes in the network
Nodes(ctx context.Context, in *NodesRequest, opts ...client.CallOption) (*NodesResponse, error)
// Returns a list of known routes in the network
Routes(ctx context.Context, in *RoutesRequest, opts ...client.CallOption) (*RoutesResponse, error)
// Returns a list of known services based on routes
Services(ctx context.Context, in *ServicesRequest, opts ...client.CallOption) (*ServicesResponse, error)
}
type networkService struct {
@ -88,12 +93,27 @@ func (c *networkService) Routes(ctx context.Context, in *RoutesRequest, opts ...
return out, nil
}
func (c *networkService) Services(ctx context.Context, in *ServicesRequest, opts ...client.CallOption) (*ServicesResponse, error) {
req := c.c.NewRequest(c.name, "Network.Services", in)
out := new(ServicesResponse)
err := c.c.Call(ctx, req, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
// Server API for Network service
type NetworkHandler interface {
// Returns the entire network graph
Graph(context.Context, *GraphRequest, *GraphResponse) error
// Returns a list of known nodes in the network
Nodes(context.Context, *NodesRequest, *NodesResponse) error
// Returns a list of known routes in the network
Routes(context.Context, *RoutesRequest, *RoutesResponse) error
// Returns a list of known services based on routes
Services(context.Context, *ServicesRequest, *ServicesResponse) error
}
func RegisterNetworkHandler(s server.Server, hdlr NetworkHandler, opts ...server.HandlerOption) error {
@ -101,6 +121,7 @@ func RegisterNetworkHandler(s server.Server, hdlr NetworkHandler, opts ...server
Graph(ctx context.Context, in *GraphRequest, out *GraphResponse) error
Nodes(ctx context.Context, in *NodesRequest, out *NodesResponse) error
Routes(ctx context.Context, in *RoutesRequest, out *RoutesResponse) error
Services(ctx context.Context, in *ServicesRequest, out *ServicesResponse) error
}
type Network struct {
network
@ -124,3 +145,7 @@ func (h *networkHandler) Nodes(ctx context.Context, in *NodesRequest, out *Nodes
func (h *networkHandler) Routes(ctx context.Context, in *RoutesRequest, out *RoutesResponse) error {
return h.NetworkHandler.Routes(ctx, in, out)
}
func (h *networkHandler) Services(ctx context.Context, in *ServicesRequest, out *ServicesResponse) error {
return h.NetworkHandler.Services(ctx, in, out)
}

View File

@ -254,6 +254,76 @@ func (m *RoutesResponse) GetRoutes() []*proto1.Route {
return nil
}
type ServicesRequest struct {
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
}
func (m *ServicesRequest) Reset() { *m = ServicesRequest{} }
func (m *ServicesRequest) String() string { return proto.CompactTextString(m) }
func (*ServicesRequest) ProtoMessage() {}
func (*ServicesRequest) Descriptor() ([]byte, []int) {
return fileDescriptor_0b7953b26a7c4730, []int{6}
}
func (m *ServicesRequest) XXX_Unmarshal(b []byte) error {
return xxx_messageInfo_ServicesRequest.Unmarshal(m, b)
}
func (m *ServicesRequest) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
return xxx_messageInfo_ServicesRequest.Marshal(b, m, deterministic)
}
func (m *ServicesRequest) XXX_Merge(src proto.Message) {
xxx_messageInfo_ServicesRequest.Merge(m, src)
}
func (m *ServicesRequest) XXX_Size() int {
return xxx_messageInfo_ServicesRequest.Size(m)
}
func (m *ServicesRequest) XXX_DiscardUnknown() {
xxx_messageInfo_ServicesRequest.DiscardUnknown(m)
}
var xxx_messageInfo_ServicesRequest proto.InternalMessageInfo
type ServicesResponse struct {
Services []string `protobuf:"bytes,1,rep,name=services,proto3" json:"services,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
}
func (m *ServicesResponse) Reset() { *m = ServicesResponse{} }
func (m *ServicesResponse) String() string { return proto.CompactTextString(m) }
func (*ServicesResponse) ProtoMessage() {}
func (*ServicesResponse) Descriptor() ([]byte, []int) {
return fileDescriptor_0b7953b26a7c4730, []int{7}
}
func (m *ServicesResponse) XXX_Unmarshal(b []byte) error {
return xxx_messageInfo_ServicesResponse.Unmarshal(m, b)
}
func (m *ServicesResponse) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
return xxx_messageInfo_ServicesResponse.Marshal(b, m, deterministic)
}
func (m *ServicesResponse) XXX_Merge(src proto.Message) {
xxx_messageInfo_ServicesResponse.Merge(m, src)
}
func (m *ServicesResponse) XXX_Size() int {
return xxx_messageInfo_ServicesResponse.Size(m)
}
func (m *ServicesResponse) XXX_DiscardUnknown() {
xxx_messageInfo_ServicesResponse.DiscardUnknown(m)
}
var xxx_messageInfo_ServicesResponse proto.InternalMessageInfo
func (m *ServicesResponse) GetServices() []string {
if m != nil {
return m.Services
}
return nil
}
// Node is network node
type Node struct {
// node id
@ -269,7 +339,7 @@ func (m *Node) Reset() { *m = Node{} }
func (m *Node) String() string { return proto.CompactTextString(m) }
func (*Node) ProtoMessage() {}
func (*Node) Descriptor() ([]byte, []int) {
return fileDescriptor_0b7953b26a7c4730, []int{6}
return fileDescriptor_0b7953b26a7c4730, []int{8}
}
func (m *Node) XXX_Unmarshal(b []byte) error {
@ -317,7 +387,7 @@ func (m *Connect) Reset() { *m = Connect{} }
func (m *Connect) String() string { return proto.CompactTextString(m) }
func (*Connect) ProtoMessage() {}
func (*Connect) Descriptor() ([]byte, []int) {
return fileDescriptor_0b7953b26a7c4730, []int{7}
return fileDescriptor_0b7953b26a7c4730, []int{9}
}
func (m *Connect) XXX_Unmarshal(b []byte) error {
@ -358,7 +428,7 @@ func (m *Close) Reset() { *m = Close{} }
func (m *Close) String() string { return proto.CompactTextString(m) }
func (*Close) ProtoMessage() {}
func (*Close) Descriptor() ([]byte, []int) {
return fileDescriptor_0b7953b26a7c4730, []int{8}
return fileDescriptor_0b7953b26a7c4730, []int{10}
}
func (m *Close) XXX_Unmarshal(b []byte) error {
@ -401,7 +471,7 @@ func (m *Peer) Reset() { *m = Peer{} }
func (m *Peer) String() string { return proto.CompactTextString(m) }
func (*Peer) ProtoMessage() {}
func (*Peer) Descriptor() ([]byte, []int) {
return fileDescriptor_0b7953b26a7c4730, []int{9}
return fileDescriptor_0b7953b26a7c4730, []int{11}
}
func (m *Peer) XXX_Unmarshal(b []byte) error {
@ -443,6 +513,8 @@ func init() {
proto.RegisterType((*GraphResponse)(nil), "go.micro.network.GraphResponse")
proto.RegisterType((*RoutesRequest)(nil), "go.micro.network.RoutesRequest")
proto.RegisterType((*RoutesResponse)(nil), "go.micro.network.RoutesResponse")
proto.RegisterType((*ServicesRequest)(nil), "go.micro.network.ServicesRequest")
proto.RegisterType((*ServicesResponse)(nil), "go.micro.network.ServicesResponse")
proto.RegisterType((*Node)(nil), "go.micro.network.Node")
proto.RegisterType((*Connect)(nil), "go.micro.network.Connect")
proto.RegisterType((*Close)(nil), "go.micro.network.Close")
@ -454,31 +526,33 @@ func init() {
}
var fileDescriptor_0b7953b26a7c4730 = []byte{
// 371 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x94, 0x93, 0xd1, 0x4b, 0x3a, 0x41,
0x10, 0xc7, 0x3d, 0xf5, 0x94, 0xdf, 0xfc, 0x3a, 0x8b, 0x25, 0xe2, 0xf0, 0xa1, 0x64, 0xe9, 0x41,
0xa2, 0xce, 0x50, 0x7c, 0x8a, 0x20, 0xf0, 0x21, 0x08, 0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 0xd7, 0xd0, 0x34, 0x26, 0x58, 0x07, 0xea, 0x49, 0x4c, 0x43, 0xfc, 0x13, 0xf5,
0x24, 0x66, 0x21, 0xb4, 0x67, 0x71, 0xac, 0x64, 0x9e, 0x87, 0x75, 0x4a, 0x16, 0x21, 0x1f, 0x43,
0x7b, 0x82, 0x69, 0x2a, 0x17, 0xda, 0xcc, 0x6e, 0xbc, 0x57, 0xcf, 0x4e, 0xef, 0x43, 0x0c, 0x1f,
0x81, 0x3f, 0x79, 0x45, 0x6b, 0xf8, 0xd7, 0x45, 0xcf, 0xd0, 0x34, 0xf6, 0xff, 0x52, 0x63, 0xb6,
0x96, 0x49, 0xa9, 0xcc, 0xdc, 0x8d, 0x3d, 0x2f, 0x6a, 0xa1, 0xe1, 0x87, 0x07, 0xed, 0xa9, 0xcd,
0xb3, 0x07, 0xf0, 0x69, 0x37, 0xec, 0xf4, 0x67, 0xcd, 0xf6, 0x6a, 0xbb, 0x67, 0x95, 0xff, 0xdb,
0x2d, 0xf0, 0x9a, 0xd1, 0xa2, 0x63, 0x2a, 0xd3, 0xda, 0xbe, 0xc5, 0x32, 0xad, 0x9d, 0x2b, 0xe4,
0x35, 0xf6, 0x08, 0x2d, 0xbb, 0x65, 0x56, 0x02, 0xef, 0x1c, 0x44, 0xb7, 0x57, 0x0d, 0x14, 0x72,
0xf3, 0x16, 0x7d, 0x14, 0xa3, 0xcf, 0x00, 0x00, 0x00, 0xff, 0xff, 0x21, 0x74, 0xcb, 0x32, 0x94,
0x03, 0x00, 0x00,
// 416 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x94, 0x93, 0x5d, 0x6f, 0xda, 0x30,
0x14, 0x86, 0x21, 0x10, 0x3e, 0xce, 0x16, 0x60, 0xd6, 0x34, 0x45, 0xb9, 0xd8, 0x98, 0xb5, 0x0b,
0x34, 0x6d, 0x66, 0x02, 0x71, 0x35, 0x4d, 0x9a,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xff, 0x3a, 0x63, 0x7a, 0x7b, 0x2c, 0x04, 0x00, 0x00,
}
// Reference imports to suppress errors if they are not otherwise used.
@ -493,9 +567,14 @@ const _ = grpc.SupportPackageIsVersion4
//
// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://godoc.org/google.golang.org/grpc#ClientConn.NewStream.
type NetworkClient interface {
// Returns the entire network graph
Graph(ctx context.Context, in *GraphRequest, opts ...grpc.CallOption) (*GraphResponse, error)
// Returns a list of known nodes in the network
Nodes(ctx context.Context, in *NodesRequest, opts ...grpc.CallOption) (*NodesResponse, error)
// Returns a list of known routes in the network
Routes(ctx context.Context, in *RoutesRequest, opts ...grpc.CallOption) (*RoutesResponse, error)
// Returns a list of known services based on routes
Services(ctx context.Context, in *ServicesRequest, opts ...grpc.CallOption) (*ServicesResponse, error)
}
type networkClient struct {
@ -533,11 +612,25 @@ func (c *networkClient) Routes(ctx context.Context, in *RoutesRequest, opts ...g
return out, nil
}
func (c *networkClient) Services(ctx context.Context, in *ServicesRequest, opts ...grpc.CallOption) (*ServicesResponse, error) {
out := new(ServicesResponse)
err := c.cc.Invoke(ctx, "/go.micro.network.Network/Services", in, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
// NetworkServer is the server API for Network service.
type NetworkServer interface {
// Returns the entire network graph
Graph(context.Context, *GraphRequest) (*GraphResponse, error)
// Returns a list of known nodes in the network
Nodes(context.Context, *NodesRequest) (*NodesResponse, error)
// Returns a list of known routes in the network
Routes(context.Context, *RoutesRequest) (*RoutesResponse, error)
// Returns a list of known services based on routes
Services(context.Context, *ServicesRequest) (*ServicesResponse, error)
}
func RegisterNetworkServer(s *grpc.Server, srv NetworkServer) {
@ -598,6 +691,24 @@ func _Network_Routes_Handler(srv interface{}, ctx context.Context, dec func(inte
return interceptor(ctx, in, info, handler)
}
func _Network_Services_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(ServicesRequest)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(NetworkServer).Services(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: "/go.micro.network.Network/Services",
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(NetworkServer).Services(ctx, req.(*ServicesRequest))
}
return interceptor(ctx, in, info, handler)
}
var _Network_serviceDesc = grpc.ServiceDesc{
ServiceName: "go.micro.network.Network",
HandlerType: (*NetworkServer)(nil),
@ -614,6 +725,10 @@ var _Network_serviceDesc = grpc.ServiceDesc{
MethodName: "Routes",
Handler: _Network_Routes_Handler,
},
{
MethodName: "Services",
Handler: _Network_Services_Handler,
},
},
Streams: []grpc.StreamDesc{},
Metadata: "github.com/micro/go-micro/network/proto/network.proto",

View File

@ -6,9 +6,14 @@ import "github.com/micro/go-micro/router/proto/router.proto";
// Network service is usesd to gain visibility into networks
service Network {
// Returns the entire network graph
rpc Graph(GraphRequest) returns (GraphResponse) {};
// Returns a list of known nodes in the network
rpc Nodes(NodesRequest) returns (NodesResponse) {};
// Returns a list of known routes in the network
rpc Routes(RoutesRequest) returns (RoutesResponse) {};
// Returns a list of known services based on routes
rpc Services(ServicesRequest) returns (ServicesResponse) {};
}
// PeerRequest requests list of peers
@ -39,6 +44,12 @@ message RoutesResponse {
repeated go.micro.router.Route routes = 1;
}
message ServicesRequest {}
message ServicesResponse {
repeated string services = 1;
}
// Node is network node
message Node {
// node id