blob: ca6d870d17c1a73ebf6aefa6798d4638daee769f (
plain)
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
|
package bootstrap
import (
"net"
"sync"
"gitlab.com/gitlab-org/gitaly/internal/rubyserver"
"gitlab.com/gitlab-org/gitaly/internal/server"
"google.golang.org/grpc"
)
// GitalyServerFactory is a factory of gitaly grpc servers
type GitalyServerFactory struct {
ruby *rubyserver.Server
secure, insecure *grpc.Server
}
// GracefulStoppableServer allows to serve contents on a net.Listener, Stop serving and performing a GracefulStop
type GracefulStoppableServer interface {
GracefulStop()
Stop()
Serve(l net.Listener, secure bool) error
}
// NewGitalyServerFactory initializes a rubyserver and then lazily initializes both secure and insecure grpc.Server
func NewGitalyServerFactory() *GitalyServerFactory {
return &GitalyServerFactory{ruby: &rubyserver.Server{}}
}
// StartRuby starts the ruby process
func (s *GitalyServerFactory) StartRuby() error {
return s.ruby.Start()
}
// Stop stops both the secure and insecure servers
func (s *GitalyServerFactory) Stop() {
for _, srv := range s.all() {
srv.Stop()
}
s.ruby.Stop()
}
// GracefulStop stops both the secure and insecure servers gracefully
func (s *GitalyServerFactory) GracefulStop() {
wg := sync.WaitGroup{}
for _, srv := range s.all() {
wg.Add(1)
go func(s *grpc.Server) {
s.GracefulStop()
wg.Done()
}(srv)
}
wg.Wait()
}
// Serve starts serving the listener
func (s *GitalyServerFactory) Serve(l net.Listener, secure bool) error {
srv := s.get(secure)
return srv.Serve(l)
}
func (s *GitalyServerFactory) get(secure bool) *grpc.Server {
if secure {
if s.secure == nil {
s.secure = server.NewSecure(s.ruby)
}
return s.secure
}
if s.insecure == nil {
s.insecure = server.NewInsecure(s.ruby)
}
return s.insecure
}
func (s *GitalyServerFactory) all() []*grpc.Server {
var servers []*grpc.Server
if s.secure != nil {
servers = append(servers, s.secure)
}
if s.insecure != nil {
servers = append(servers, s.insecure)
}
return servers
}
|