diff options
author | John Cai <jcai@gitlab.com> | 2019-11-18 22:38:59 +0300 |
---|---|---|
committer | jramsay <jcai@gitlab.com> | 2019-11-27 03:22:12 +0300 |
commit | 7c4f16371b8f051f1eb3a7e989d26ea7eb962a8e (patch) | |
tree | e3e158da21a79a4ca2b4feeade654505d326785a /internal/bootstrap | |
parent | 82d31745e0901dc91c2fe02f44ff8ff659a22c09 (diff) |
Wire in bootstrap package for praefect for zero downtime deploy
Diffstat (limited to 'internal/bootstrap')
-rw-r--r-- | internal/bootstrap/server_factory.go | 28 | ||||
-rw-r--r-- | internal/bootstrap/starter/starter.go | 50 | ||||
-rw-r--r-- | internal/bootstrap/starter/starter_test.go | 38 |
3 files changed, 105 insertions, 11 deletions
diff --git a/internal/bootstrap/server_factory.go b/internal/bootstrap/server_factory.go index bb26a59d4..ca6d870d1 100644 --- a/internal/bootstrap/server_factory.go +++ b/internal/bootstrap/server_factory.go @@ -9,7 +9,8 @@ import ( "google.golang.org/grpc" ) -type serverFactory struct { +// GitalyServerFactory is a factory of gitaly grpc servers +type GitalyServerFactory struct { ruby *rubyserver.Server secure, insecure *grpc.Server } @@ -19,17 +20,20 @@ type GracefulStoppableServer interface { GracefulStop() Stop() Serve(l net.Listener, secure bool) error - StartRuby() error } -// NewServerFactory initializes a rubyserver and then lazily initializes both secure and insecure grpc.Server -func NewServerFactory() GracefulStoppableServer { - return &serverFactory{ruby: &rubyserver.Server{}} +// NewGitalyServerFactory initializes a rubyserver and then lazily initializes both secure and insecure grpc.Server +func NewGitalyServerFactory() *GitalyServerFactory { + return &GitalyServerFactory{ruby: &rubyserver.Server{}} } -func (s *serverFactory) StartRuby() error { return s.ruby.Start() } +// StartRuby starts the ruby process +func (s *GitalyServerFactory) StartRuby() error { + return s.ruby.Start() +} -func (s *serverFactory) Stop() { +// Stop stops both the secure and insecure servers +func (s *GitalyServerFactory) Stop() { for _, srv := range s.all() { srv.Stop() } @@ -37,7 +41,8 @@ func (s *serverFactory) Stop() { s.ruby.Stop() } -func (s *serverFactory) GracefulStop() { +// GracefulStop stops both the secure and insecure servers gracefully +func (s *GitalyServerFactory) GracefulStop() { wg := sync.WaitGroup{} for _, srv := range s.all() { @@ -52,13 +57,14 @@ func (s *serverFactory) GracefulStop() { wg.Wait() } -func (s *serverFactory) Serve(l net.Listener, secure bool) error { +// 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 *serverFactory) get(secure bool) *grpc.Server { +func (s *GitalyServerFactory) get(secure bool) *grpc.Server { if secure { if s.secure == nil { s.secure = server.NewSecure(s.ruby) @@ -74,7 +80,7 @@ func (s *serverFactory) get(secure bool) *grpc.Server { return s.insecure } -func (s *serverFactory) all() []*grpc.Server { +func (s *GitalyServerFactory) all() []*grpc.Server { var servers []*grpc.Server if s.secure != nil { servers = append(servers, s.secure) diff --git a/internal/bootstrap/starter/starter.go b/internal/bootstrap/starter/starter.go new file mode 100644 index 000000000..5751c033e --- /dev/null +++ b/internal/bootstrap/starter/starter.go @@ -0,0 +1,50 @@ +package starter + +import ( + "github.com/sirupsen/logrus" + "gitlab.com/gitlab-org/gitaly/internal/bootstrap" +) + +const ( + // TCP is the prefix for tcp + TCP string = "tcp" + // TLS is the prefix for tls + TLS string = "tls" + // Unix is the prefix for unix + Unix string = "unix" +) + +// Config represents a network type, and address +type Config struct { + Name, Addr string +} + +func (c *Config) isSecure() bool { + return c.Name == TLS +} + +func (c *Config) family() string { + if c.isSecure() { + return TCP + } + + return c.Name +} + +// New creates a new bootstrap.Starter from a config and a GracefulStoppableServer +func New(cfg Config, servers bootstrap.GracefulStoppableServer) bootstrap.Starter { + return func(listen bootstrap.ListenFunc, errCh chan<- error) error { + l, err := listen(cfg.family(), cfg.Addr) + if err != nil { + return err + } + + logrus.WithField("address", cfg.Addr).Infof("listening at %s address", cfg.Name) + + go func() { + errCh <- servers.Serve(l, cfg.isSecure()) + }() + + return nil + } +} diff --git a/internal/bootstrap/starter/starter_test.go b/internal/bootstrap/starter/starter_test.go new file mode 100644 index 000000000..7a6a35158 --- /dev/null +++ b/internal/bootstrap/starter/starter_test.go @@ -0,0 +1,38 @@ +package starter + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func TestIsSecure(t *testing.T) { + for _, test := range []struct { + name string + secure bool + }{ + {"tcp", false}, + {"unix", false}, + {"tls", true}, + } { + t.Run(test.name, func(t *testing.T) { + conf := Config{Name: test.name} + require.Equal(t, test.secure, conf.isSecure()) + }) + } +} + +func TestFamily(t *testing.T) { + for _, test := range []struct { + name, family string + }{ + {"tcp", "tcp"}, + {"unix", "unix"}, + {"tls", "tcp"}, + } { + t.Run(test.name, func(t *testing.T) { + conf := Config{Name: test.name} + require.Equal(t, test.family, conf.family()) + }) + } +} |