-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.go
112 lines (98 loc) · 2.27 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package eidola
import (
"context"
"github.com/dormoron/eidola/internal/errs"
"github.com/dormoron/eidola/registry"
"google.golang.org/grpc"
"net"
"time"
)
type ServerOption func(s *Server)
// Server represents a gRPC server
type Server struct {
name string
registry registry.Registry
registerTimeout time.Duration
*grpc.Server
listener net.Listener
weight uint32
group string
}
// NewServer intializes a new server
func NewServer(name string, opts ...ServerOption) (*Server, error) {
s := &Server{
name: name,
Server: grpc.NewServer(),
registerTimeout: time.Second * 10,
}
for _, opt := range opts {
opt(s)
}
return s, nil
}
// Start starts the server
func (s *Server) Start(addr string) (err error) {
s.listener, err = net.Listen("tcp", addr)
if err != nil {
return errs.ErrServerListening(err)
}
// Ensure listener is closed on an error to prevent a leak.
defer func() {
if err != nil {
s.listener.Close()
}
}()
if s.registry != nil {
ctx, cancel := context.WithTimeout(context.Background(), s.registerTimeout)
defer cancel()
err = s.registry.Register(ctx, registry.ServiceInstance{
Name: s.name,
Address: s.listener.Addr().String(),
Weight: s.weight,
Group: s.group,
})
if err != nil {
return errs.ErrServerRegister(err)
}
}
err = s.Serve(s.listener)
if err != nil {
return errs.ErrFailedServe(err)
}
return nil
}
// Close gracefully stops the server
func (s *Server) Close() error {
if s.registry != nil {
err := s.registry.Close()
if err != nil {
return errs.ErrServerClose(err)
}
}
s.GracefulStop()
return nil
}
// ServerWithRegistry sets the registry for the server
func ServerWithRegistry(reg registry.Registry) ServerOption {
return func(s *Server) {
s.registry = reg
}
}
// ServerWithRegisterTimeout sets the register timeout for the server
func ServerWithRegisterTimeout(d time.Duration) ServerOption {
return func(s *Server) {
s.registerTimeout = d
}
}
// ServerWithWeight sets the weight for the server
func ServerWithWeight(weight uint32) ServerOption {
return func(s *Server) {
s.weight = weight
}
}
// ServerWithGroup sets the group for the server
func ServerWithGroup(group string) ServerOption {
return func(s *Server) {
s.group = group
}
}