Remove unused streaming endpoints

This commit is contained in:
Jeffrey Picard 2021-10-29 17:27:27 -04:00
parent 58a44bfce3
commit a67f9b43d1
5 changed files with 34 additions and 269 deletions

View file

@ -10,7 +10,6 @@ service Hub {
rpc Ping (EmptyMessage) returns (StringValue) {}
rpc Hello (HelloMessage) returns (HelloMessage) {}
rpc AddPeer (ServerMessage) returns (StringValue) {}
rpc PeerSubscribeStreaming (ServerMessage) returns (stream ServerMessage) {}
rpc PeerSubscribe (ServerMessage) returns (StringValue) {}
rpc Version (EmptyMessage) returns (StringValue) {}
rpc Features (EmptyMessage) returns (StringValue) {}

View file

@ -1137,7 +1137,7 @@ var file_hub_proto_rawDesc = []byte{
0x61, 0x74, 0x65, 0x73, 0x18, 0x39, 0x20, 0x01, 0x28, 0x08, 0x52, 0x10, 0x72, 0x65, 0x6d, 0x6f,
0x76, 0x65, 0x44, 0x75, 0x70, 0x6c, 0x69, 0x63, 0x61, 0x74, 0x65, 0x73, 0x12, 0x1b, 0x0a, 0x09,
0x6e, 0x6f, 0x5f, 0x74, 0x6f, 0x74, 0x61, 0x6c, 0x73, 0x18, 0x3a, 0x20, 0x01, 0x28, 0x08, 0x52,
0x08, 0x6e, 0x6f, 0x54, 0x6f, 0x74, 0x61, 0x6c, 0x73, 0x32, 0xcc, 0x03, 0x0a, 0x03, 0x48, 0x75,
0x08, 0x6e, 0x6f, 0x54, 0x6f, 0x74, 0x61, 0x6c, 0x73, 0x32, 0x88, 0x03, 0x0a, 0x03, 0x48, 0x75,
0x62, 0x12, 0x2a, 0x0a, 0x06, 0x53, 0x65, 0x61, 0x72, 0x63, 0x68, 0x12, 0x11, 0x2e, 0x70, 0x62,
0x2e, 0x53, 0x65, 0x61, 0x72, 0x63, 0x68, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x0b,
0x2e, 0x70, 0x62, 0x2e, 0x4f, 0x75, 0x74, 0x70, 0x75, 0x74, 0x73, 0x22, 0x00, 0x12, 0x2b, 0x0a,
@ -1149,27 +1149,23 @@ var file_hub_proto_rawDesc = []byte{
0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x22, 0x00, 0x12, 0x2f, 0x0a, 0x07, 0x41, 0x64, 0x64,
0x50, 0x65, 0x65, 0x72, 0x12, 0x11, 0x2e, 0x70, 0x62, 0x2e, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72,
0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x1a, 0x0f, 0x2e, 0x70, 0x62, 0x2e, 0x53, 0x74, 0x72,
0x69, 0x6e, 0x67, 0x56, 0x61, 0x6c, 0x75, 0x65, 0x22, 0x00, 0x12, 0x42, 0x0a, 0x16, 0x50, 0x65,
0x65, 0x72, 0x53, 0x75, 0x62, 0x73, 0x63, 0x72, 0x69, 0x62, 0x65, 0x53, 0x74, 0x72, 0x65, 0x61,
0x6d, 0x69, 0x6e, 0x67, 0x12, 0x11, 0x2e, 0x70, 0x62, 0x2e, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72,
0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x1a, 0x11, 0x2e, 0x70, 0x62, 0x2e, 0x53, 0x65, 0x72,
0x76, 0x65, 0x72, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x22, 0x00, 0x30, 0x01, 0x12, 0x35,
0x0a, 0x0d, 0x50, 0x65, 0x65, 0x72, 0x53, 0x75, 0x62, 0x73, 0x63, 0x72, 0x69, 0x62, 0x65, 0x12,
0x11, 0x2e, 0x70, 0x62, 0x2e, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x4d, 0x65, 0x73, 0x73, 0x61,
0x67, 0x65, 0x1a, 0x0f, 0x2e, 0x70, 0x62, 0x2e, 0x53, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x56, 0x61,
0x6c, 0x75, 0x65, 0x22, 0x00, 0x12, 0x2e, 0x0a, 0x07, 0x56, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e,
0x12, 0x10, 0x2e, 0x70, 0x62, 0x2e, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x4d, 0x65, 0x73, 0x73, 0x61,
0x67, 0x65, 0x1a, 0x0f, 0x2e, 0x70, 0x62, 0x2e, 0x53, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x56, 0x61,
0x6c, 0x75, 0x65, 0x22, 0x00, 0x12, 0x2f, 0x0a, 0x08, 0x46, 0x65, 0x61, 0x74, 0x75, 0x72, 0x65,
0x73, 0x12, 0x10, 0x2e, 0x70, 0x62, 0x2e, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x4d, 0x65, 0x73, 0x73,
0x61, 0x67, 0x65, 0x1a, 0x0f, 0x2e, 0x70, 0x62, 0x2e, 0x53, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x56,
0x61, 0x6c, 0x75, 0x65, 0x22, 0x00, 0x12, 0x30, 0x0a, 0x09, 0x42, 0x72, 0x6f, 0x61, 0x64, 0x63,
0x61, 0x73, 0x74, 0x12, 0x10, 0x2e, 0x70, 0x62, 0x2e, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x4d, 0x65,
0x73, 0x73, 0x61, 0x67, 0x65, 0x1a, 0x0f, 0x2e, 0x70, 0x62, 0x2e, 0x55, 0x49, 0x6e, 0x74, 0x33,
0x32, 0x56, 0x61, 0x6c, 0x75, 0x65, 0x22, 0x00, 0x42, 0x26, 0x5a, 0x24, 0x67, 0x69, 0x74, 0x68,
0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x6c, 0x62, 0x72, 0x79, 0x69, 0x6f, 0x2f, 0x68, 0x75,
0x62, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2f, 0x67, 0x6f, 0x2f, 0x70, 0x62,
0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
0x69, 0x6e, 0x67, 0x56, 0x61, 0x6c, 0x75, 0x65, 0x22, 0x00, 0x12, 0x35, 0x0a, 0x0d, 0x50, 0x65,
0x65, 0x72, 0x53, 0x75, 0x62, 0x73, 0x63, 0x72, 0x69, 0x62, 0x65, 0x12, 0x11, 0x2e, 0x70, 0x62,
0x2e, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x1a, 0x0f,
0x2e, 0x70, 0x62, 0x2e, 0x53, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x56, 0x61, 0x6c, 0x75, 0x65, 0x22,
0x00, 0x12, 0x2e, 0x0a, 0x07, 0x56, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x12, 0x10, 0x2e, 0x70,
0x62, 0x2e, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x1a, 0x0f,
0x2e, 0x70, 0x62, 0x2e, 0x53, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x56, 0x61, 0x6c, 0x75, 0x65, 0x22,
0x00, 0x12, 0x2f, 0x0a, 0x08, 0x46, 0x65, 0x61, 0x74, 0x75, 0x72, 0x65, 0x73, 0x12, 0x10, 0x2e,
0x70, 0x62, 0x2e, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x1a,
0x0f, 0x2e, 0x70, 0x62, 0x2e, 0x53, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x56, 0x61, 0x6c, 0x75, 0x65,
0x22, 0x00, 0x12, 0x30, 0x0a, 0x09, 0x42, 0x72, 0x6f, 0x61, 0x64, 0x63, 0x61, 0x73, 0x74, 0x12,
0x10, 0x2e, 0x70, 0x62, 0x2e, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67,
0x65, 0x1a, 0x0f, 0x2e, 0x70, 0x62, 0x2e, 0x55, 0x49, 0x6e, 0x74, 0x33, 0x32, 0x56, 0x61, 0x6c,
0x75, 0x65, 0x22, 0x00, 0x42, 0x26, 0x5a, 0x24, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63,
0x6f, 0x6d, 0x2f, 0x6c, 0x62, 0x72, 0x79, 0x69, 0x6f, 0x2f, 0x68, 0x75, 0x62, 0x2f, 0x70, 0x72,
0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2f, 0x67, 0x6f, 0x2f, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72,
0x6f, 0x74, 0x6f, 0x33,
}
var (
@ -1228,22 +1224,20 @@ var file_hub_proto_depIdxs = []int32{
1, // 25: pb.Hub.Ping:input_type -> pb.EmptyMessage
3, // 26: pb.Hub.Hello:input_type -> pb.HelloMessage
2, // 27: pb.Hub.AddPeer:input_type -> pb.ServerMessage
2, // 28: pb.Hub.PeerSubscribeStreaming:input_type -> pb.ServerMessage
2, // 29: pb.Hub.PeerSubscribe:input_type -> pb.ServerMessage
1, // 30: pb.Hub.Version:input_type -> pb.EmptyMessage
1, // 31: pb.Hub.Features:input_type -> pb.EmptyMessage
1, // 32: pb.Hub.Broadcast:input_type -> pb.EmptyMessage
10, // 33: pb.Hub.Search:output_type -> pb.Outputs
5, // 34: pb.Hub.Ping:output_type -> pb.StringValue
3, // 35: pb.Hub.Hello:output_type -> pb.HelloMessage
5, // 36: pb.Hub.AddPeer:output_type -> pb.StringValue
2, // 37: pb.Hub.PeerSubscribeStreaming:output_type -> pb.ServerMessage
5, // 38: pb.Hub.PeerSubscribe:output_type -> pb.StringValue
5, // 39: pb.Hub.Version:output_type -> pb.StringValue
5, // 40: pb.Hub.Features:output_type -> pb.StringValue
7, // 41: pb.Hub.Broadcast:output_type -> pb.UInt32Value
33, // [33:42] is the sub-list for method output_type
24, // [24:33] is the sub-list for method input_type
2, // 28: pb.Hub.PeerSubscribe:input_type -> pb.ServerMessage
1, // 29: pb.Hub.Version:input_type -> pb.EmptyMessage
1, // 30: pb.Hub.Features:input_type -> pb.EmptyMessage
1, // 31: pb.Hub.Broadcast:input_type -> pb.EmptyMessage
10, // 32: pb.Hub.Search:output_type -> pb.Outputs
5, // 33: pb.Hub.Ping:output_type -> pb.StringValue
3, // 34: pb.Hub.Hello:output_type -> pb.HelloMessage
5, // 35: pb.Hub.AddPeer:output_type -> pb.StringValue
5, // 36: pb.Hub.PeerSubscribe:output_type -> pb.StringValue
5, // 37: pb.Hub.Version:output_type -> pb.StringValue
5, // 38: pb.Hub.Features:output_type -> pb.StringValue
7, // 39: pb.Hub.Broadcast:output_type -> pb.UInt32Value
32, // [32:40] is the sub-list for method output_type
24, // [24:32] is the sub-list for method input_type
24, // [24:24] is the sub-list for extension type_name
24, // [24:24] is the sub-list for extension extendee
0, // [0:24] is the sub-list for field type_name

View file

@ -22,7 +22,6 @@ type HubClient interface {
Ping(ctx context.Context, in *EmptyMessage, opts ...grpc.CallOption) (*StringValue, error)
Hello(ctx context.Context, in *HelloMessage, opts ...grpc.CallOption) (*HelloMessage, error)
AddPeer(ctx context.Context, in *ServerMessage, opts ...grpc.CallOption) (*StringValue, error)
PeerSubscribeStreaming(ctx context.Context, in *ServerMessage, opts ...grpc.CallOption) (Hub_PeerSubscribeStreamingClient, error)
PeerSubscribe(ctx context.Context, in *ServerMessage, opts ...grpc.CallOption) (*StringValue, error)
Version(ctx context.Context, in *EmptyMessage, opts ...grpc.CallOption) (*StringValue, error)
Features(ctx context.Context, in *EmptyMessage, opts ...grpc.CallOption) (*StringValue, error)
@ -73,38 +72,6 @@ func (c *hubClient) AddPeer(ctx context.Context, in *ServerMessage, opts ...grpc
return out, nil
}
func (c *hubClient) PeerSubscribeStreaming(ctx context.Context, in *ServerMessage, opts ...grpc.CallOption) (Hub_PeerSubscribeStreamingClient, error) {
stream, err := c.cc.NewStream(ctx, &Hub_ServiceDesc.Streams[0], "/pb.Hub/PeerSubscribeStreaming", opts...)
if err != nil {
return nil, err
}
x := &hubPeerSubscribeStreamingClient{stream}
if err := x.ClientStream.SendMsg(in); err != nil {
return nil, err
}
if err := x.ClientStream.CloseSend(); err != nil {
return nil, err
}
return x, nil
}
type Hub_PeerSubscribeStreamingClient interface {
Recv() (*ServerMessage, error)
grpc.ClientStream
}
type hubPeerSubscribeStreamingClient struct {
grpc.ClientStream
}
func (x *hubPeerSubscribeStreamingClient) Recv() (*ServerMessage, error) {
m := new(ServerMessage)
if err := x.ClientStream.RecvMsg(m); err != nil {
return nil, err
}
return m, nil
}
func (c *hubClient) PeerSubscribe(ctx context.Context, in *ServerMessage, opts ...grpc.CallOption) (*StringValue, error) {
out := new(StringValue)
err := c.cc.Invoke(ctx, "/pb.Hub/PeerSubscribe", in, out, opts...)
@ -149,7 +116,6 @@ type HubServer interface {
Ping(context.Context, *EmptyMessage) (*StringValue, error)
Hello(context.Context, *HelloMessage) (*HelloMessage, error)
AddPeer(context.Context, *ServerMessage) (*StringValue, error)
PeerSubscribeStreaming(*ServerMessage, Hub_PeerSubscribeStreamingServer) error
PeerSubscribe(context.Context, *ServerMessage) (*StringValue, error)
Version(context.Context, *EmptyMessage) (*StringValue, error)
Features(context.Context, *EmptyMessage) (*StringValue, error)
@ -173,9 +139,6 @@ func (UnimplementedHubServer) Hello(context.Context, *HelloMessage) (*HelloMessa
func (UnimplementedHubServer) AddPeer(context.Context, *ServerMessage) (*StringValue, error) {
return nil, status.Errorf(codes.Unimplemented, "method AddPeer not implemented")
}
func (UnimplementedHubServer) PeerSubscribeStreaming(*ServerMessage, Hub_PeerSubscribeStreamingServer) error {
return status.Errorf(codes.Unimplemented, "method PeerSubscribeStreaming not implemented")
}
func (UnimplementedHubServer) PeerSubscribe(context.Context, *ServerMessage) (*StringValue, error) {
return nil, status.Errorf(codes.Unimplemented, "method PeerSubscribe not implemented")
}
@ -273,27 +236,6 @@ func _Hub_AddPeer_Handler(srv interface{}, ctx context.Context, dec func(interfa
return interceptor(ctx, in, info, handler)
}
func _Hub_PeerSubscribeStreaming_Handler(srv interface{}, stream grpc.ServerStream) error {
m := new(ServerMessage)
if err := stream.RecvMsg(m); err != nil {
return err
}
return srv.(HubServer).PeerSubscribeStreaming(m, &hubPeerSubscribeStreamingServer{stream})
}
type Hub_PeerSubscribeStreamingServer interface {
Send(*ServerMessage) error
grpc.ServerStream
}
type hubPeerSubscribeStreamingServer struct {
grpc.ServerStream
}
func (x *hubPeerSubscribeStreamingServer) Send(m *ServerMessage) error {
return x.ServerStream.SendMsg(m)
}
func _Hub_PeerSubscribe_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(ServerMessage)
if err := dec(in); err != nil {
@ -406,12 +348,6 @@ var Hub_ServiceDesc = grpc.ServiceDesc{
Handler: _Hub_Broadcast_Handler,
},
},
Streams: []grpc.StreamDesc{
{
StreamName: "PeerSubscribeStreaming",
Handler: _Hub_PeerSubscribeStreaming_Handler,
ServerStreams: true,
},
},
Streams: []grpc.StreamDesc{},
Metadata: "hub.proto",
}

View file

@ -4,7 +4,6 @@ import (
"bufio"
"context"
"log"
"math"
"os"
"strings"
"time"
@ -13,13 +12,6 @@ import (
"google.golang.org/grpc"
)
// sub is an internal structure holding information about an open connection
// with a peer.
type sub struct {
stream pb.Hub_PeerSubscribeStreamingServer
done chan<- bool
}
// peerAddMsg is an internal structure for use in the channel communicating
// to the peerAdder gorountine.
type peerAddMsg struct {
@ -167,9 +159,7 @@ func helloPeer(server *FederatedServer, args *Args) bool {
return true
}
// writePeers writes our current known peers to disk
// FIXME: This is probably inefficient, we just truncate the file and write
// the entire thing every time. Maybe use some sort of mmap?
// writePeers writes our current known peers to disk.
func (s *Server) writePeers() {
if !s.Args.WritePeers {
return
@ -223,7 +213,6 @@ func (s *Server) peerAdder(ctx context.Context) {
Port: msg.Port,
Ts: time.Now(),
}
log.Println(!ping)
if !ping || helloPeer(newServer, s.Args) {
s.Servers[k] = newServer
s.writePeers()
@ -240,123 +229,6 @@ func (s *Server) peerAdder(ctx context.Context) {
}
}
// getFastestPeer determines the fastest peer in its list of peers by sending
// out udp pings and seeing who responds first. This is currently not
// implemented.
func (s *Server) getFastestPeer() *FederatedServer {
log.Println(s.Servers)
if len(s.Servers) == 0 {
return nil
}
for _, peer := range s.Servers {
return peer
}
return nil
}
// subscribeToPeer subscribes to a given peer hub in a streaming fashion.
func (s *Server) subscribeToPeer(peer *FederatedServer) {
var msg *pb.ServerMessage
log.Println("Subscribing to peer: ", peer)
if peer == nil {
return
}
conn, err := grpc.Dial(
peer.Address+":"+peer.Port,
grpc.WithInsecure(),
grpc.WithBlock(),
)
if err != nil {
log.Println(err)
return
}
defer conn.Close()
c := pb.NewHubClient(conn)
var retries = 0
var stream pb.Hub_PeerSubscribeStreamingClient
for retries <= 3 {
if stream == nil {
stream, err = c.PeerSubscribeStreaming(
context.Background(),
&pb.ServerMessage{Address: s.Args.Host, Port: s.Args.Port},
)
}
if err != nil {
goto retry
}
err = stream.RecvMsg(msg)
if err != nil {
goto retry
}
s.addPeer(msg, false)
continue
retry:
retries = retries + 1
time.Sleep(time.Second * time.Duration(int(math.Pow(10, float64(retries)))))
}
}
// subscribeToFastestPeer is a convenience function to find and subscribe to
// the fastest peer we know about in a streaming fashion.
func (s *Server) subscribeToFastestPeer(keepSubscribed bool) {
for {
peer := s.getFastestPeer()
s.subscribeToPeer(peer)
if !keepSubscribed {
return
}
// Put in a sleep, so we aren't looping like crazy if we have no peers
time.Sleep(time.Second * 5)
}
}
// notifyPeerSubsStreaming notifies peer subs of new peers in a streaming
// fashion.
func (s *Server) notifyPeerSubsStreaming(newServer *FederatedServer) {
msg := &pb.ServerMessage{
Address: newServer.Address,
Port: newServer.Port,
}
var unsubscribe []string
s.PeerSubs.Range(func(k, v interface{}) bool {
key, ok := k.(string)
if !ok {
log.Println("Failed to cast subscriber key: ", v)
return true
}
peer, ok := v.(sub)
if !ok {
log.Println("Failed to cast subscriber value: ", v)
return true
}
log.Printf("Notifying peer %s of new node %+v\n", key, msg)
err := peer.stream.Send(msg)
if err != nil {
log.Println("Failed to send data to ", key)
select {
case peer.done <- true:
log.Println("Unsubscribed ", key)
default:
}
unsubscribe = append(unsubscribe, key)
}
return true
})
for _, key := range unsubscribe {
s.PeerSubs.Delete(key)
}
}
func (s *Server) notifyPeerSubs(newServer *FederatedServer) {
var unsubscribe []string
s.PeerSubs.Range(func(k, v interface{}) bool {

View file

@ -213,42 +213,6 @@ func (s *Server) PeerSubscribe(ctx context.Context, in *pb.ServerMessage) (*pb.S
return &pb.StringValue{Value: "Success"}, nil
}
// PeerSubscribeStreaming is a streaming grpc endpoint that allows another hub to
// subscribe to this hub for peer updates. This first loops through all the
// peers that this hub knows about and sends them to the connecting hub,
// it then stores the peer, along with a channel to indicate when it's finished
// and it's stream context in our map of peer subs. Finally, it waits on the
// context to finish, or a message in the done channel. This function cannot
// exit while the peer is subscribe or the context will die. Communicating with
// the peer is handled by the peerAdder goroutine in federation.go, which
// listens on a channel of new peers and notifies all our connected peers when
// we find out about a new one.
func (s *Server) PeerSubscribeStreaming(in *pb.ServerMessage, stream pb.Hub_PeerSubscribeStreamingServer) error {
for _, server := range s.Servers {
err := stream.Send(&pb.ServerMessage{
Address: server.Address,
Port: server.Port,
})
if err != nil {
return err
}
}
done := make(chan bool)
ctx := stream.Context()
s.PeerSubs.Store(peerKey(in), &sub{stream: stream, done: done})
for {
select {
case <-done:
log.Println("Removing client:", in)
return nil
case <-ctx.Done():
log.Println("Client disconnected: ", in)
return nil
}
}
}
// AddPeer is a grpc endpoint to tell this hub about another hub in the network.
func (s *Server) AddPeer(ctx context.Context, args *pb.ServerMessage) (*pb.StringValue, error) {
s.addPeer(args, true)