Skip to content
This repository has been archived by the owner on Feb 24, 2024. It is now read-only.

Commit

Permalink
Improve performance of WrapBuffaloHandler (#2146)
Browse files Browse the repository at this point in the history
* Add Benchmark to test performance improvements

* Implement performance improvements

* Minor improvements to default context

* Integrate wrapcontext functionality into defaultcontext
  • Loading branch information
fasmat authored Nov 20, 2021
1 parent 6781b33 commit 4644ecc
Show file tree
Hide file tree
Showing 5 changed files with 191 additions and 95 deletions.
79 changes: 42 additions & 37 deletions default_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,45 +109,47 @@ func (d *DefaultContext) Render(status int, rr render.Renderer) error {
defer func() {
d.LogField("render", time.Since(start))
}()
if rr != nil {
data := d.Data()
pp := map[string]string{}
for k, v := range d.params {
pp[k] = v[0]
}
data["params"] = pp
data["flash"] = d.Flash().data
data["session"] = d.Session()
data["request"] = d.Request()
data["status"] = status
bb := &bytes.Buffer{}

err := rr.Render(bb, data)
if err != nil {
if er, ok := errx.Unwrap(err).(render.ErrRedirect); ok {
return d.Redirect(er.Status, er.URL)
}
return HTTPError{Status: http.StatusInternalServerError, Cause: err}
}

if d.Session() != nil {
d.Flash().Clear()
d.Flash().persist(d.Session())
}

d.Response().Header().Set("Content-Type", rr.ContentType())
if p, ok := data["pagination"].(paginable); ok {
d.Response().Header().Set("X-Pagination", p.Paginate())
}
if rr == nil {
d.Response().WriteHeader(status)
_, err = io.Copy(d.Response(), bb)
if err != nil {
return HTTPError{Status: http.StatusInternalServerError, Cause: err}
return nil
}

data := d.Data()
pp := map[string]string{}
for k, v := range d.params {
pp[k] = v[0]
}
data["params"] = pp
data["flash"] = d.Flash().data
data["session"] = d.Session()
data["request"] = d.Request()
data["status"] = status
bb := &bytes.Buffer{}

err := rr.Render(bb, data)
if err != nil {
if er, ok := errx.Unwrap(err).(render.ErrRedirect); ok {
return d.Redirect(er.Status, er.URL)
}
return HTTPError{Status: http.StatusInternalServerError, Cause: err}
}

return nil
if d.Session() != nil {
d.Flash().Clear()
d.Flash().persist(d.Session())
}

d.Response().Header().Set("Content-Type", rr.ContentType())
if p, ok := data["pagination"].(paginable); ok {
d.Response().Header().Set("X-Pagination", p.Paginate())
}
d.Response().WriteHeader(status)
_, err = io.Copy(d.Response(), bb)
if err != nil {
return HTTPError{Status: http.StatusInternalServerError, Cause: err}
}

return nil
}

Expand All @@ -164,13 +166,19 @@ func (d *DefaultContext) Bind(value interface{}) error {
// as part of the request logging. This allows you to easily add things
// like metrics (think DB times) to your request.
func (d *DefaultContext) LogField(key string, value interface{}) {
if d.logger == nil {
return
}
d.logger = d.logger.WithField(key, value)
}

// LogFields adds the key/value pairs onto the Logger to be printed out
// as part of the request logging. This allows you to easily add things
// like metrics (think DB times) to your request.
func (d *DefaultContext) LogFields(values map[string]interface{}) {
if d.logger == nil {
return
}
d.logger = d.logger.WithFields(values)
}

Expand Down Expand Up @@ -253,10 +261,7 @@ func (d *DefaultContext) File(name string) (binding.File, error) {
File: f,
FileHeader: h,
}
if err != nil {
return bf, err
}
return bf, nil
return bf, err
}

// MarshalJSON implements json marshaling for the context
Expand Down
25 changes: 12 additions & 13 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,31 @@ go 1.16
require (
github.com/BurntSushi/toml v0.4.1
github.com/dustin/go-humanize v1.0.0
github.com/fatih/color v1.12.0
github.com/fatih/color v1.13.0
github.com/gobuffalo/envy v1.10.1
github.com/gobuffalo/events v1.4.1
github.com/gobuffalo/events v1.4.2
github.com/gobuffalo/flect v0.2.4
github.com/gobuffalo/github_flavored_markdown v1.1.0
github.com/gobuffalo/helpers v0.6.2
github.com/gobuffalo/httptest v1.5.0
github.com/gobuffalo/logger v1.0.4
github.com/gobuffalo/github_flavored_markdown v1.1.1
github.com/gobuffalo/helpers v0.6.3
github.com/gobuffalo/httptest v1.5.1
github.com/gobuffalo/logger v1.0.6
github.com/gobuffalo/meta v0.3.1
github.com/gobuffalo/nulls v0.4.0
github.com/gobuffalo/packd v1.0.0
github.com/gobuffalo/nulls v0.4.1
github.com/gobuffalo/packd v1.0.1
github.com/gobuffalo/packr/v2 v2.8.1
github.com/gobuffalo/plush/v4 v4.1.6
github.com/gobuffalo/plush/v4 v4.1.8
github.com/gobuffalo/pop/v5 v5.3.4
github.com/gobuffalo/tags/v3 v3.1.0
github.com/gobuffalo/tags/v3 v3.1.1
github.com/gorilla/handlers v1.5.1
github.com/gorilla/mux v1.8.0
github.com/gorilla/sessions v1.2.1
github.com/karrick/godirwalk v1.16.1
github.com/markbates/grift v1.5.0
github.com/markbates/oncer v1.0.0
github.com/markbates/refresh v1.11.1
github.com/markbates/refresh v1.12.0
github.com/markbates/safe v1.0.1
github.com/markbates/sigtx v1.0.0
github.com/microcosm-cc/bluemonday v1.0.16 // indirect
github.com/monoculum/formam v0.0.0-20210523135142-1af3317b7b9b
github.com/monoculum/formam v3.5.5+incompatible
github.com/sirupsen/logrus v1.8.1
github.com/spf13/cobra v1.2.1
github.com/stretchr/testify v1.7.0
Expand Down
Loading

0 comments on commit 4644ecc

Please sign in to comment.