From ba615b2d10dd8c9888eade132ac5d4d7974f40b6 Mon Sep 17 00:00:00 2001 From: Koala Yeung Date: Thu, 8 Feb 2018 17:21:00 +0800 Subject: [PATCH] [BREAKING] NewRequest signature change * remove the id field from NewRequest follow the field Request.ID retirement. --- authorizer.go | 2 +- authorizer_test.go | 2 +- client.go | 18 +++--------------- client_test.go | 4 ++-- host.go | 2 +- session_test.go | 2 +- 6 files changed, 9 insertions(+), 21 deletions(-) diff --git a/authorizer.go b/authorizer.go index c6e0cbe..7d2d4c1 100644 --- a/authorizer.go +++ b/authorizer.go @@ -9,7 +9,7 @@ import ( // NewAuthRequest returns a new *http.Request // and a *Request with the body buffered // into new NopReader. -func NewAuthRequest(id uint16, orgl *http.Request) (r *http.Request, req *Request, err error) { +func NewAuthRequest(orgl *http.Request) (r *http.Request, req *Request, err error) { // new request struct that inherits orgl values r = &http.Request{} diff --git a/authorizer_test.go b/authorizer_test.go index 5bf9aab..f5d4268 100644 --- a/authorizer_test.go +++ b/authorizer_test.go @@ -22,7 +22,7 @@ func TestNewAuthRequest(t *testing.T) { return } // clone the request into 2 - r, req, err := gofast.NewAuthRequest(0, raw) + r, req, err := gofast.NewAuthRequest(raw) if err != nil { t.Errorf("unexpected error: %s", err) return diff --git a/client.go b/client.go index f0ee1b7..ef184bd 100644 --- a/client.go +++ b/client.go @@ -35,7 +35,7 @@ const ( // NewRequest returns a standard FastCGI request // with a unique request ID allocted by the client -func NewRequest(id uint16, r *http.Request) (req *Request) { +func NewRequest(r *http.Request) (req *Request) { req = &Request{ Raw: r, Role: RoleResponder, @@ -76,7 +76,7 @@ func (p *idPool) Alloc() uint16 { func (p *idPool) Release(id uint16) { go func() { // release the ID back to channel for reuse - // use goroutine to prevent blocking ReleaseID + // use goroutine to prev0, ent blocking ReleaseID p.IDs <- id }() } @@ -114,17 +114,6 @@ type client struct { ids idPool } -// AllocID implements Client.AllocID -func (c *client) AllocID() (reqID uint16) { - reqID = c.ids.Alloc() - return -} - -// ReleaseID implements Client.ReleaseID -func (c *client) ReleaseID(reqID uint16) { - c.ids.Release(reqID) -} - // writeRequest writes params and stdin to the FastCGI application func (c *client) writeRequest(reqID uint16, req *Request) (err error) { @@ -197,7 +186,6 @@ func (c *client) writeRequest(reqID uint16, req *Request) (err error) { _, err = dataWriter.Write(p[:count]) if err != nil { - c.AllocID() return } } @@ -255,7 +243,6 @@ func (c *client) Do(req *Request) (resp *ResponsePipe, err error) { // validate the request // if role is a filter, it has to have Data stream if req.Role == RoleFilter { - c.AllocID() // validate the request if req.Data == nil { err = fmt.Errorf("filter request requries a data stream") @@ -275,6 +262,7 @@ func (c *client) Do(req *Request) (resp *ResponsePipe, err error) { } } + // allocate request ID reqID := c.ids.Alloc() // create response pipe diff --git a/client_test.go b/client_test.go index 12fdf96..6b539a9 100644 --- a/client_test.go +++ b/client_test.go @@ -54,7 +54,7 @@ func TestClient_canceled(t *testing.T) { } } - req = gofast.NewRequest(0, r) + req = gofast.NewRequest(r) req.Params["CONTENT_TYPE"] = r.Header.Get("Content-Type") req.Params["CONTENT_LENGTH"] = r.Header.Get("Content-Length") req.Params["HTTPS"] = isHTTPS @@ -177,7 +177,7 @@ func TestClient_StdErr(t *testing.T) { p.network, p.address, err.Error()) return } - req := gofast.NewRequest(0, nil) + req := gofast.NewRequest(nil) // Some required paramters with invalid values req.Params["REQUEST_METHOD"] = "" diff --git a/host.go b/host.go index debf2a0..51208b2 100644 --- a/host.go +++ b/host.go @@ -48,7 +48,7 @@ func (h *defaultHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { } // handle the session - resp, err := h.sessionHandler(c, NewRequest(0, r)) + resp, err := h.sessionHandler(c, NewRequest(r)) if err != nil { http.Error(w, "failed to process request", http.StatusInternalServerError) log.Printf("gofast: unable to process request %s", diff --git a/session_test.go b/session_test.go index 9ee7055..72c6333 100644 --- a/session_test.go +++ b/session_test.go @@ -170,7 +170,7 @@ func TestMapFilterRequest(t *testing.T) { return } c := NopClient(1) - _, err = sess(c, gofast.NewRequest(0, r)) + _, err = sess(c, gofast.NewRequest(r)) if err != nil { t.Errorf("unexpected error: %s", err) return