diff options
author | Wolfy-J <[email protected]> | 2018-06-12 20:45:24 +0300 |
---|---|---|
committer | Wolfy-J <[email protected]> | 2018-06-12 20:45:24 +0300 |
commit | 99c1121d8fd31b7b61b6c22e181dc3c05a3f9f82 (patch) | |
tree | 25e6baf4eaf4e4ebd37c2431e485dfe3f4e3ac11 /service | |
parent | f8d8e9a55d31c6ee98b1f292e49b31b474883a4f (diff) |
static pool logic update, better watching techniques
Diffstat (limited to 'service')
-rw-r--r-- | service/http/handler.go (renamed from service/http/server.go) | 48 | ||||
-rw-r--r-- | service/http/handler_test.go (renamed from service/http/server_test.go) | 18 | ||||
-rw-r--r-- | service/http/rpc.go | 7 | ||||
-rw-r--r-- | service/http/service.go | 4 |
4 files changed, 41 insertions, 36 deletions
diff --git a/service/http/server.go b/service/http/handler.go index cad2e46d..1df00e5a 100644 --- a/service/http/server.go +++ b/service/http/handler.go @@ -30,84 +30,84 @@ type Event struct { Error error } -// Server serves http connections to underlying PHP application using PSR-7 protocol. Context will include request headers, +// Handler serves http connections to underlying PHP application using PSR-7 protocol. Context will include request headers, // parsed files and query, payload will include parsed form dataTree (if any). -type Server struct { +type Handler struct { cfg *Config listener func(event int, ctx interface{}) rr *roadrunner.Server } // AddListener attaches pool event watcher. -func (s *Server) Listen(l func(event int, ctx interface{})) { - s.listener = l +func (h *Handler) Listen(l func(event int, ctx interface{})) { + h.listener = l } // Handle serve using PSR-7 requests passed to underlying application. Attempts to serve static files first if enabled. -func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { +func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { // validating request size - if s.cfg.MaxRequest != 0 { + if h.cfg.MaxRequest != 0 { if length := r.Header.Get("content-length"); length != "" { if size, err := strconv.ParseInt(length, 10, 64); err != nil { - s.handleError(w, r, err) + h.handleError(w, r, err) return - } else if size > s.cfg.MaxRequest*1024*1024 { - s.handleError(w, r, errors.New("request body max size is exceeded")) + } else if size > h.cfg.MaxRequest*1024*1024 { + h.handleError(w, r, errors.New("request body max size is exceeded")) return } } } - req, err := NewRequest(r, s.cfg.Uploads) + req, err := NewRequest(r, h.cfg.Uploads) if err != nil { - s.handleError(w, r, err) + h.handleError(w, r, err) return } if err = req.Open(); err != nil { - s.handleError(w, r, err) + h.handleError(w, r, err) return } defer req.Close() p, err := req.Payload() if err != nil { - s.handleError(w, r, err) + h.handleError(w, r, err) return } - rsp, err := s.rr.Exec(p) + rsp, err := h.rr.Exec(p) if err != nil { - s.handleError(w, r, err) + h.handleError(w, r, err) return } resp, err := NewResponse(rsp) if err != nil { - s.handleError(w, r, err) + h.handleError(w, r, err) return } - s.handleResponse(req, resp) + h.handleResponse(req, resp) resp.Write(w) } // handleResponse triggers response event. -func (s *Server) handleResponse(req *Request, resp *Response) { - s.throw(EventResponse, &Event{Method: req.Method, Uri: req.Uri, Status: resp.Status}) +func (h *Handler) handleResponse(req *Request, resp *Response) { + h.throw(EventResponse, &Event{Method: req.Method, Uri: req.Uri, Status: resp.Status}) } // handleError sends error. -func (s *Server) handleError(w http.ResponseWriter, r *http.Request, err error) { - s.throw(EventError, &Event{Method: r.Method, Uri: uri(r), Status: 500, Error: err}) +func (h *Handler) handleError(w http.ResponseWriter, r *http.Request, err error) { + h.throw(EventError, &Event{Method: r.Method, Uri: uri(r), Status: 500, Error: err}) w.WriteHeader(500) w.Write([]byte(err.Error())) } // throw invokes event srv if any. -func (s *Server) throw(event int, ctx interface{}) { - if s.listener != nil { - s.listener(event, ctx) +func (h *Handler) throw(event int, ctx interface{}) { + if h.listener != nil { + h.listener(event, ctx) } } diff --git a/service/http/server_test.go b/service/http/handler_test.go index 35ee940c..73e587cc 100644 --- a/service/http/server_test.go +++ b/service/http/handler_test.go @@ -26,7 +26,7 @@ func get(url string) (string, *http.Response, error) { } func TestServer_Echo(t *testing.T) { - st := &Server{ + st := &Handler{ cfg: &Config{ MaxRequest: 1024, Uploads: &UploadsConfig{ @@ -60,7 +60,7 @@ func TestServer_Echo(t *testing.T) { } func TestServer_Headers(t *testing.T) { - st := &Server{ + st := &Handler{ cfg: &Config{ MaxRequest: 1024, Uploads: &UploadsConfig{ @@ -106,7 +106,7 @@ func TestServer_Headers(t *testing.T) { } func TestServer_Cookies(t *testing.T) { - st := &Server{ + st := &Handler{ cfg: &Config{ MaxRequest: 1024, Uploads: &UploadsConfig{ @@ -156,7 +156,7 @@ func TestServer_Cookies(t *testing.T) { } func TestServer_JsonPayload_POST(t *testing.T) { - st := &Server{ + st := &Handler{ cfg: &Config{ MaxRequest: 1024, Uploads: &UploadsConfig{ @@ -205,7 +205,7 @@ func TestServer_JsonPayload_POST(t *testing.T) { } func TestServer_JsonPayload_PUT(t *testing.T) { - st := &Server{ + st := &Handler{ cfg: &Config{ MaxRequest: 1024, Uploads: &UploadsConfig{ @@ -254,7 +254,7 @@ func TestServer_JsonPayload_PUT(t *testing.T) { } func TestServer_JsonPayload_PATCH(t *testing.T) { - st := &Server{ + st := &Handler{ cfg: &Config{ MaxRequest: 1024, Uploads: &UploadsConfig{ @@ -303,7 +303,7 @@ func TestServer_JsonPayload_PATCH(t *testing.T) { } func TestServer_FormData_POST(t *testing.T) { - st := &Server{ + st := &Handler{ cfg: &Config{ MaxRequest: 1024, Uploads: &UploadsConfig{ @@ -364,7 +364,7 @@ func TestServer_FormData_POST(t *testing.T) { } func TestServer_FormData_PUT(t *testing.T) { - st := &Server{ + st := &Handler{ cfg: &Config{ MaxRequest: 1024, Uploads: &UploadsConfig{ @@ -425,7 +425,7 @@ func TestServer_FormData_PUT(t *testing.T) { } func TestServer_FormData_PATCH(t *testing.T) { - st := &Server{ + st := &Handler{ cfg: &Config{ MaxRequest: 1024, Uploads: &UploadsConfig{ diff --git a/service/http/rpc.go b/service/http/rpc.go index aa68a826..fc6aa65b 100644 --- a/service/http/rpc.go +++ b/service/http/rpc.go @@ -34,7 +34,12 @@ func (rpc *rpcServer) Reset(reset bool, r *string) error { } *r = "OK" - return rpc.svc.srv.rr.Reset() + + for _, w := range rpc.svc.rr.Workers() { + w.Kill() + } + + return nil //rpc.svc.rr.Reset() } // Workers returns list of active workers and their stats. diff --git a/service/http/service.go b/service/http/service.go index 40eec579..a3773468 100644 --- a/service/http/service.go +++ b/service/http/service.go @@ -22,7 +22,7 @@ type Service struct { listeners []func(event int, ctx interface{}) middleware []middleware rr *roadrunner.Server - srv *Server + srv *Handler http *http.Server } @@ -68,7 +68,7 @@ func (s *Service) Serve() error { rr := roadrunner.NewServer(s.cfg.Workers) s.rr = rr - s.srv = &Server{cfg: s.cfg, rr: s.rr} + s.srv = &Handler{cfg: s.cfg, rr: s.rr} s.http = &http.Server{Addr: s.cfg.Address} s.rr.Listen(s.listener) |