-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathserver.go
97 lines (80 loc) · 2.04 KB
/
server.go
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package server
import (
"fmt"
"net"
"sync"
"time"
log "github.com/Sirupsen/logrus"
google_protobuf "github.com/golang/protobuf/ptypes/empty"
"github.com/radu-matei/kube-toolkit/pkg/rpc"
"github.com/radu-matei/kube-toolkit/pkg/version"
"golang.org/x/net/context"
"google.golang.org/grpc"
)
// Config contains all configuration for the server
type Config struct {
ListenAddress string
}
// Server contains all methods and config for the server
type Server struct {
Config *Config
RPC *grpc.Server
}
// NewServer returns a new instance of the server
func NewServer(cfg *Config) *Server {
return &Server{
Config: cfg,
RPC: grpc.NewServer(),
}
}
// Serve starts the server and listens on ListenAddress
func (server *Server) Serve(ctx context.Context) error {
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", 10000))
if err != nil {
return fmt.Errorf("failed to start listening: %v", err)
}
rpc.RegisterGRPCServer(server.RPC, server)
_, cancel := context.WithCancel(ctx)
var wg sync.WaitGroup
errc := make(chan error, 1)
wg.Add(1)
go func() {
errc <- server.RPC.Serve(lis)
log.Debugf("starting to serve...")
close(errc)
wg.Done()
}()
defer func() {
server.RPC.Stop()
log.Debugf("stopping the server")
cancel()
wg.Wait()
}()
select {
case <-ctx.Done():
return ctx.Err()
case err := <-errc:
return err
}
}
// GetVersion returns the current version of the server.
func (server *Server) GetVersion(ctx context.Context, _ *google_protobuf.Empty) (*rpc.Version, error) {
log.Debugf("executing GetVersion")
return &rpc.Version{
SemVer: version.SemVer,
GitCommit: version.GitCommit}, nil
}
// ServerStream starts a new stream from the server
func (server *Server) ServerStream(_ *google_protobuf.Empty, stream rpc.GRPC_ServerStreamServer) error {
log.Debugf("received server stream command")
for i := 0; i < 5; i++ {
err := stream.Send(&rpc.Message{
Message: fmt.Sprintf("Sending stream back to client, iteration: %d", i),
})
if err != nil {
return err
}
time.Sleep(2 * time.Second)
}
return nil
}