diff options
author | Valery Piashchynski <[email protected]> | 2020-03-03 18:57:52 +0300 |
---|---|---|
committer | Valery Piashchynski <[email protected]> | 2020-03-03 18:57:52 +0300 |
commit | aa0ba5496c101e41a497285b4e0f4e9f2820b5e7 (patch) | |
tree | dadbacfbbcd99e2306fa0996393b31106a0542bb /service | |
parent | 9fabf648f1c3cb797ec03377c3e2182397fb7a1a (diff) |
Fix typos
Update signals handling mechanism
http proper stopping
Diffstat (limited to 'service')
-rw-r--r-- | service/http/config.go | 21 | ||||
-rw-r--r-- | service/http/service.go | 53 | ||||
-rw-r--r-- | service/rpc/service_test.go | 2 |
3 files changed, 49 insertions, 27 deletions
diff --git a/service/http/config.go b/service/http/config.go index 13a2cfc9..d4143bb0 100644 --- a/service/http/config.go +++ b/service/http/config.go @@ -24,6 +24,9 @@ type Config struct { // HTTP2 configuration HTTP2 *HTTP2Config + // Unix socket configuration + Unix *UnixSocketsConfig + // MaxRequestSize specified max size for payload body in megabytes, set 0 to unlimited. MaxRequestSize int64 @@ -56,6 +59,14 @@ type HTTP2Config struct { MaxConcurrentStreams uint32 } +// UnixSocketsConfig is unix sockets configuration +type UnixSocketsConfig struct { + // Enabled - Enable or disable unix sockets extension + Enabled bool + // Address is a Unix socket address [rr.sock for example] + Address string +} + // InitDefaults sets default values for HTTP/2 configuration. func (cfg *HTTP2Config) InitDefaults() error { cfg.Enabled = true @@ -201,19 +212,19 @@ func (c *Config) IsTrusted(ip string) bool { // Valid validates the configuration. func (c *Config) Valid() error { if c.Uploads == nil { - return errors.New("mailformed uploads config") + return errors.New("malformed uploads config") } if c.HTTP2 == nil { - return errors.New("mailformed http2 config") + return errors.New("malformed http2 config") } if c.Workers == nil { - return errors.New("mailformed workers config") + return errors.New("malformed workers config") } if c.Workers.Pool == nil { - return errors.New("mailformed workers config (pool config is missing)") + return errors.New("malformed workers config (pool config is missing)") } if err := c.Workers.Pool.Valid(); err != nil { @@ -225,7 +236,7 @@ func (c *Config) Valid() error { } if c.Address != "" && !strings.Contains(c.Address, ":") { - return errors.New("mailformed http server address") + return errors.New("malformed http server address") } if c.EnableTLS() { diff --git a/service/http/service.go b/service/http/service.go index fb4b51df..8ffc725a 100644 --- a/service/http/service.go +++ b/service/http/service.go @@ -32,19 +32,23 @@ type middleware func(f http.HandlerFunc) http.HandlerFunc // Service manages rr, http servers. type Service struct { - cfg *Config - log *logrus.Logger - cprod roadrunner.CommandProducer - env env.Environment - lsns []func(event int, ctx interface{}) - mdwr []middleware - mu sync.Mutex + sync.Mutex + sync.WaitGroup + + cfg *Config + log *logrus.Logger + cprod roadrunner.CommandProducer + env env.Environment + lsns []func(event int, ctx interface{}) + mdwr []middleware + rr *roadrunner.Server controller roadrunner.Controller handler *Handler - http *http.Server - https *http.Server - fcgi *http.Server + + http *http.Server + https *http.Server + fcgi *http.Server } // Attach attaches controller. Currently only one controller is supported. @@ -89,7 +93,7 @@ func (s *Service) Init(cfg *Config, r *rpc.Service, e env.Environment, log *logr // Serve serves the svc. func (s *Service) Serve() error { - s.mu.Lock() + s.Lock() if s.env != nil { if err := s.env.Copy(s.cfg.Workers); err != nil { @@ -132,7 +136,7 @@ func (s *Service) Serve() error { s.fcgi = &http.Server{Handler: s} } - s.mu.Unlock() + s.Unlock() if err := s.rr.Start(); err != nil { return err @@ -161,9 +165,9 @@ func (s *Service) Serve() error { if httpErr != nil && httpErr != http.ErrServerClosed { err <- httpErr - } else { - err <- nil + return } + err <- nil }() } @@ -172,22 +176,23 @@ func (s *Service) Serve() error { httpErr := s.serveFCGI() if httpErr != nil && httpErr != http.ErrServerClosed { err <- httpErr - } else { - err <- nil + return } + err <- nil }() } - return <-err } // Stop stops the http. func (s *Service) Stop() { - s.mu.Lock() - defer s.mu.Unlock() + s.Lock() + defer s.Unlock() if s.fcgi != nil { + s.Add(1) go func() { + defer s.Done() err := s.fcgi.Shutdown(context.Background()) if err != nil && err != http.ErrServerClosed { // Stop() error @@ -199,7 +204,9 @@ func (s *Service) Stop() { } if s.https != nil { + s.Add(1) go func() { + defer s.Done() err := s.https.Shutdown(context.Background()) if err != nil && err != http.ErrServerClosed { s.log.Error(fmt.Errorf("error shutting down the https server, error: %v", err)) @@ -209,7 +216,9 @@ func (s *Service) Stop() { } if s.http != nil { + s.Add(1) go func() { + defer s.Done() err := s.http.Shutdown(context.Background()) if err != nil && err != http.ErrServerClosed { s.log.Error(fmt.Errorf("error shutting down the http server, error: %v", err)) @@ -217,12 +226,14 @@ func (s *Service) Stop() { } }() } + + s.Wait() } // Server returns associated rr server (if any). func (s *Service) Server() *roadrunner.Server { - s.mu.Lock() - defer s.mu.Unlock() + s.Lock() + defer s.Unlock() return s.rr } diff --git a/service/rpc/service_test.go b/service/rpc/service_test.go index 11e8fcb5..51c1b337 100644 --- a/service/rpc/service_test.go +++ b/service/rpc/service_test.go @@ -49,7 +49,7 @@ func Test_StopNonServing(t *testing.T) { func Test_Serve_Errors(t *testing.T) { s := &Service{} - ok, err := s.Init(&Config{Enable: true, Listen: "mailformed"}, service.NewContainer(nil), nil) + ok, err := s.Init(&Config{Enable: true, Listen: "malformed"}, service.NewContainer(nil), nil) assert.NoError(t, err) assert.True(t, ok) |