Skip to content

Commit f405794

Browse files
committed
Now using sync.Pool
Signed-off-by: Vishal Rana <[email protected]>
1 parent 443a0bb commit f405794

18 files changed

+317
-235
lines changed

context.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ type (
4747
Redirect(int, string) error
4848
Error(err error)
4949
Logger() *log.Logger
50-
X() *context
50+
Context() *context
5151
}
5252

5353
context struct {
@@ -307,8 +307,8 @@ func (c *context) Logger() *log.Logger {
307307
return c.echo.logger
308308
}
309309

310-
// X returns the `context` instance.
311-
func (c *context) X() *context {
310+
// Context returns the `context` instance.
311+
func (c *context) Context() *context {
312312
return c
313313
}
314314

context_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ func TestContext(t *testing.T) {
5151
assert.Nil(t, c.Socket())
5252

5353
// Param by id
54-
c.X().pnames = []string{"id"}
55-
c.X().pvalues = []string{"1"}
54+
c.Context().pnames = []string{"id"}
55+
c.Context().pvalues = []string{"1"}
5656
assert.Equal(t, "1", c.P(0))
5757

5858
// Param by name
@@ -68,13 +68,13 @@ func TestContext(t *testing.T) {
6868

6969
// JSON
7070
testBindOk(t, c, ApplicationJSON)
71-
c.X().request = test.NewRequest(POST, "/", strings.NewReader(incorrectContent))
71+
c.Context().request = test.NewRequest(POST, "/", strings.NewReader(incorrectContent))
7272
testBindError(t, c, ApplicationJSON)
7373

7474
// XML
75-
c.X().request = test.NewRequest(POST, "/", strings.NewReader(userXML))
75+
c.Context().request = test.NewRequest(POST, "/", strings.NewReader(userXML))
7676
testBindOk(t, c, ApplicationXML)
77-
c.X().request = test.NewRequest(POST, "/", strings.NewReader(incorrectContent))
77+
c.Context().request = test.NewRequest(POST, "/", strings.NewReader(incorrectContent))
7878
testBindError(t, c, ApplicationXML)
7979

8080
// Unsupported
@@ -87,14 +87,14 @@ func TestContext(t *testing.T) {
8787
tpl := &Template{
8888
templates: template.Must(template.New("hello").Parse("Hello, {{.}}!")),
8989
}
90-
c.X().echo.SetRenderer(tpl)
90+
c.Context().echo.SetRenderer(tpl)
9191
err := c.Render(http.StatusOK, "hello", "Joe")
9292
if assert.NoError(t, err) {
9393
assert.Equal(t, http.StatusOK, rec.Status())
9494
assert.Equal(t, "Hello, Joe!", rec.Body.String())
9595
}
9696

97-
c.X().echo.renderer = nil
97+
c.Context().echo.renderer = nil
9898
err = c.Render(http.StatusOK, "hello", "Joe")
9999
assert.Error(t, err)
100100

@@ -226,12 +226,12 @@ func TestContext(t *testing.T) {
226226

227227
// Error
228228
rec = test.NewResponseRecorder()
229-
c = NewContext(req, rec, e).X()
229+
c = NewContext(req, rec, e).Context()
230230
c.Error(errors.New("error"))
231231
assert.Equal(t, http.StatusInternalServerError, c.Response().Status())
232232

233233
// reset
234-
c.X().reset(req, test.NewResponseRecorder(), e)
234+
c.Context().reset(req, test.NewResponseRecorder(), e)
235235
}
236236

237237
func TestContextPath(t *testing.T) {

echo.go

Lines changed: 17 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -554,8 +554,23 @@ func (e *Echo) SetEngine(t engine.Type) {
554554
}
555555

556556
// Run runs a server.
557-
func (e *Echo) Run(address string) {
558-
config := &engine.Config{Address: address}
557+
func (e *Echo) Run(addr string) {
558+
c := &engine.Config{Address: addr}
559+
e.RunWithConfig(c)
560+
}
561+
562+
// RunTLS runs a server with TLS configuration.
563+
func (e *Echo) RunTLS(addr, certfile, keyfile string) {
564+
c := &engine.Config{
565+
Address: addr,
566+
TLSCertfile: certfile,
567+
TLSKeyfile: keyfile,
568+
}
569+
e.RunWithConfig(c)
570+
}
571+
572+
// RunWithConfig runs a server with engine configuration.
573+
func (e *Echo) RunWithConfig(config *engine.Config) {
559574
handler := func(req engine.Request, res engine.Response) {
560575
if e.hook != nil {
561576
e.hook(req, res)
@@ -585,38 +600,6 @@ func (e *Echo) Run(address string) {
585600
}
586601

587602
e.engine.Start()
588-
589-
// e.run(e.Server(addr))
590-
}
591-
592-
// RunTLS runs a server with TLS configuration.
593-
func (e *Echo) RunTLS(addr, crtFile, keyFile string) {
594-
// e.run(e.Server(addr), crtFile, keyFile)
595-
}
596-
597-
// RunServer runs a custom server.
598-
func (e *Echo) RunServer(s *http.Server) {
599-
// e.run(s)
600-
}
601-
602-
// RunTLSServer runs a custom server with TLS configuration.
603-
func (e *Echo) RunTLSServer(s *http.Server, crtFile, keyFile string) {
604-
// e.run(s, crtFile, keyFile)
605-
}
606-
607-
func (e *Echo) run(s *http.Server, files ...string) {
608-
// s.Handler = e
609-
// // TODO: Remove in Go 1.6+
610-
// if e.http2 {
611-
// http2.ConfigureServer(s, nil)
612-
// }
613-
// if len(files) == 0 {
614-
// e.logger.Fatal(s.ListenAndServe())
615-
// } else if len(files) == 2 {
616-
// e.logger.Fatal(s.ListenAndServeTLS(files[0], files[1]))
617-
// } else {
618-
// e.logger.Fatal("invalid TLS configuration")
619-
// }
620603
}
621604

622605
func NewHTTPError(code int, msg ...string) *HTTPError {

echo_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -307,9 +307,9 @@ func TestEchoGroup(t *testing.T) {
307307
func TestEchoNotFound(t *testing.T) {
308308
e := New()
309309
req := test.NewRequest(GET, "/files", nil)
310-
res := test.NewResponseRecorder()
311-
e.ServeHTTP(req, res)
312-
assert.Equal(t, http.StatusNotFound, res.Status())
310+
rec := test.NewResponseRecorder()
311+
e.ServeHTTP(req, rec)
312+
assert.Equal(t, http.StatusNotFound, rec.Status())
313313
}
314314

315315
func TestEchoMethodNotAllowed(t *testing.T) {
@@ -318,9 +318,9 @@ func TestEchoMethodNotAllowed(t *testing.T) {
318318
return c.String(http.StatusOK, "Echo!")
319319
})
320320
req := test.NewRequest(POST, "/", nil)
321-
res := test.NewResponseRecorder()
322-
e.ServeHTTP(req, res)
323-
assert.Equal(t, http.StatusMethodNotAllowed, res.Status())
321+
rec := test.NewResponseRecorder()
322+
e.ServeHTTP(req, rec)
323+
assert.Equal(t, http.StatusMethodNotAllowed, rec.Status())
324324
}
325325

326326
func TestEchoHTTPError(t *testing.T) {
@@ -349,8 +349,8 @@ func TestEchoHook(t *testing.T) {
349349
}
350350
})
351351
req := test.NewRequest(GET, "/test/", nil)
352-
res := test.NewResponseRecorder()
353-
e.ServeHTTP(req, res)
352+
rec := test.NewResponseRecorder()
353+
e.ServeHTTP(req, rec)
354354
assert.Equal(t, req.URL().Path(), "/test")
355355
}
356356

@@ -370,7 +370,7 @@ func testMethod(t *testing.T, method, path string, e *Echo) {
370370

371371
func request(method, path string, e *Echo) (int, string) {
372372
req := test.NewRequest(method, path, nil)
373-
res := test.NewResponseRecorder()
374-
e.ServeHTTP(req, res)
375-
return res.Status(), res.Body.String()
373+
rec := test.NewResponseRecorder()
374+
e.ServeHTTP(req, rec)
375+
return rec.Status(), rec.Body.String()
376376
}

engine/engine.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package engine
22

3-
import "io"
3+
import (
4+
"io"
5+
"time"
6+
)
47

58
type (
69
Type uint8
@@ -31,6 +34,8 @@ type (
3134
Status() int
3235
Size() int64
3336
Committed() bool
37+
SetWriter(io.Writer)
38+
Writer() io.Writer
3439
}
3540

3641
Header interface {
@@ -49,7 +54,11 @@ type (
4954
}
5055

5156
Config struct {
52-
Address string
57+
Address string
58+
ReadTimeout time.Duration
59+
WriteTimeout time.Duration
60+
TLSCertfile string
61+
TLSKeyfile string
5362
}
5463
)
5564

engine/fasthttp/response.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package fasthttp
22

33
import (
4+
"io"
5+
46
"github.com/labstack/echo/engine"
57
"github.com/valyala/fasthttp"
68
)
@@ -12,6 +14,7 @@ type (
1214
status int
1315
size int64
1416
committed bool
17+
writer io.Writer
1518
}
1619
)
1720

@@ -38,3 +41,11 @@ func (r *Response) Size() int64 {
3841
func (r *Response) Committed() bool {
3942
return r.committed
4043
}
44+
45+
func (r *Response) SetWriter(w io.Writer) {
46+
r.writer = w
47+
}
48+
49+
func (r *Response) Writer() io.Writer {
50+
return r.writer
51+
}

engine/fasthttp/server.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package fasthttp
22

33
import (
4-
"log"
54
"net/http"
5+
6+
"github.com/labstack/gommon/log"
67
)
78
import (
89
"github.com/labstack/echo/engine"
@@ -27,7 +28,6 @@ func NewServer(config *engine.Config, handler engine.HandlerFunc) *Server {
2728

2829
func (s *Server) Start() {
2930
fasthttp.ListenAndServe(s.config.Address, func(ctx *fasthttp.RequestCtx) {
30-
println("FastHTTP")
3131
req := &Request{
3232
context: ctx,
3333
url: &URL{ctx.URI()},

engine/standard/header.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,26 @@ import "net/http"
44

55
type (
66
Header struct {
7-
http.Header
7+
header http.Header
88
}
99
)
1010

1111
func (h *Header) Add(key, val string) {
12-
h.Header.Add(key, val)
12+
h.header.Add(key, val)
1313
}
1414

1515
func (h *Header) Del(key string) {
16-
h.Header.Del(key)
16+
h.header.Del(key)
1717
}
1818

1919
func (h *Header) Get(key string) string {
20-
return h.Header.Get(key)
20+
return h.header.Get(key)
2121
}
2222

2323
func (h *Header) Set(key, val string) {
24-
h.Header.Set(key, val)
24+
h.header.Set(key, val)
25+
}
26+
27+
func (h *Header) reset(hdr http.Header) {
28+
h.header = hdr
2529
}

engine/standard/request.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ type (
1818
func NewRequest(r *http.Request) *Request {
1919
return &Request{
2020
request: r,
21-
url: NewURL(r.URL),
21+
url: &URL{url: r.URL},
2222
header: &Header{r.Header},
2323
}
2424
}
@@ -54,3 +54,9 @@ func (r *Request) Body() io.ReadCloser {
5454
func (r *Request) FormValue(name string) string {
5555
return r.request.FormValue(name)
5656
}
57+
58+
func (r *Request) reset(req *http.Request, h engine.Header, u engine.URL) {
59+
r.request = req
60+
r.header = h
61+
r.url = u
62+
}

engine/standard/response.go

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
package standard
22

3-
import "net/http"
4-
import "github.com/labstack/echo/engine"
3+
import (
4+
"io"
5+
"net/http"
6+
7+
"github.com/labstack/echo/engine"
8+
)
59

610
type (
711
Response struct {
@@ -10,13 +14,15 @@ type (
1014
status int
1115
size int64
1216
committed bool
17+
writer io.Writer
1318
}
1419
)
1520

1621
func NewResponse(w http.ResponseWriter) *Response {
1722
return &Response{
1823
response: w,
1924
header: &Header{w.Header()},
25+
writer: w,
2026
}
2127
}
2228

@@ -35,7 +41,7 @@ func (r *Response) WriteHeader(code int) {
3541
}
3642

3743
func (r *Response) Write(b []byte) (n int, err error) {
38-
n, err = r.response.Write(b)
44+
n, err = r.writer.Write(b)
3945
r.size += int64(n)
4046
return
4147
}
@@ -51,3 +57,20 @@ func (r *Response) Size() int64 {
5157
func (r *Response) Committed() bool {
5258
return r.committed
5359
}
60+
61+
func (r *Response) SetWriter(w io.Writer) {
62+
r.writer = w
63+
}
64+
65+
func (r *Response) Writer() io.Writer {
66+
return r.writer
67+
}
68+
69+
func (r *Response) reset(w http.ResponseWriter, h engine.Header) {
70+
r.response = w
71+
r.header = h
72+
r.status = http.StatusOK
73+
r.size = 0
74+
r.committed = false
75+
r.writer = w
76+
}

0 commit comments

Comments
 (0)