diff options
Diffstat (limited to 'service')
-rw-r--r-- | service/http/config.go | 10 | ||||
-rw-r--r-- | service/http/service.go | 53 | ||||
-rw-r--r-- | service/http/ssl_test.go | 43 | ||||
-rw-r--r-- | service/rpc/service_test.go | 2 |
4 files changed, 56 insertions, 52 deletions
diff --git a/service/http/config.go b/service/http/config.go index 13a2cfc9..81fcd16c 100644 --- a/service/http/config.go +++ b/service/http/config.go @@ -201,19 +201,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 +225,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/http/ssl_test.go b/service/http/ssl_test.go index b82aa75c..c650a266 100644 --- a/service/http/ssl_test.go +++ b/service/http/ssl_test.go @@ -53,20 +53,13 @@ func Test_SSL_Service_Echo(t *testing.T) { t.Errorf("error during the Serve: error %v", err) } }() - time.Sleep(time.Millisecond * 100) - defer c.Stop() + time.Sleep(time.Millisecond * 500) req, err := http.NewRequest("GET", "https://localhost:6900?hello=world", nil) assert.NoError(t, err) r, err := sslClient.Do(req) assert.NoError(t, err) - defer func() { - err := r.Body.Close() - if err != nil { - t.Errorf("fail to close the Body: error %v", err) - } - }() b, err := ioutil.ReadAll(r.Body) assert.NoError(t, err) @@ -74,6 +67,13 @@ func Test_SSL_Service_Echo(t *testing.T) { assert.NoError(t, err) assert.Equal(t, 201, r.StatusCode) assert.Equal(t, "WORLD", string(b)) + + err2 := r.Body.Close() + if err2 != nil { + t.Errorf("fail to close the Body: error %v", err2) + } + + c.Stop() } func Test_SSL_Service_NoRedirect(t *testing.T) { @@ -110,16 +110,13 @@ func Test_SSL_Service_NoRedirect(t *testing.T) { } }() - time.Sleep(time.Second) + time.Sleep(time.Millisecond * 500) req, err := http.NewRequest("GET", "http://localhost:6030?hello=world", nil) assert.NoError(t, err) r, err := sslClient.Do(req) assert.NoError(t, err) - defer func() { - - }() assert.Nil(t, r.TLS) @@ -172,17 +169,13 @@ func Test_SSL_Service_Redirect(t *testing.T) { } }() - time.Sleep(time.Second) + time.Sleep(time.Millisecond * 500) req, err := http.NewRequest("GET", "http://localhost:6031?hello=world", nil) assert.NoError(t, err) r, err := sslClient.Do(req) assert.NoError(t, err) - defer func() { - - }() - assert.NotNil(t, r.TLS) b, err := ioutil.ReadAll(r.Body) @@ -233,20 +226,13 @@ func Test_SSL_Service_Push(t *testing.T) { t.Errorf("error during the Serve: error %v", err) } }() - time.Sleep(time.Millisecond * 100) - defer c.Stop() + time.Sleep(time.Millisecond * 500) req, err := http.NewRequest("GET", "https://localhost:6903?hello=world", nil) assert.NoError(t, err) r, err := sslClient.Do(req) assert.NoError(t, err) - defer func() { - err := r.Body.Close() - if err != nil { - t.Errorf("fail to close the Body: error %v", err) - } - }() assert.NotNil(t, r.TLS) @@ -258,4 +244,11 @@ func Test_SSL_Service_Push(t *testing.T) { assert.NoError(t, err) assert.Equal(t, 201, r.StatusCode) assert.Equal(t, "WORLD", string(b)) + + + err2 := r.Body.Close() + if err2 != nil { + t.Errorf("fail to close the Body: error %v", err2) + } + c.Stop() } 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) |