Skip to content

Commit

Permalink
feat: optimize performance (#106)
Browse files Browse the repository at this point in the history
  • Loading branch information
devhaozi authored Nov 30, 2024
1 parent 26325a9 commit 0c5e251
Show file tree
Hide file tree
Showing 7 changed files with 179 additions and 114 deletions.
25 changes: 19 additions & 6 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package gin
import (
"context"
"net/http/httptest"
"sync"
"time"

"github.com/gin-gonic/gin"

"github.com/goravel/framework/contracts/http"
)

Expand All @@ -17,30 +17,43 @@ func Background() http.Context {
return NewContext(ctx)
}

var contextPool = sync.Pool{New: func() any {
return &Context{}
}}

type Context struct {
instance *gin.Context
request http.ContextRequest
response http.ContextResponse
}

func NewContext(ctx *gin.Context) http.Context {
return &Context{instance: ctx}
func NewContext(c *gin.Context) *Context {
ctx := contextPool.Get().(*Context)
ctx.instance = c
return ctx
}

func (c *Context) Request() http.ContextRequest {
if c.request == nil {
c.request = NewContextRequest(c, LogFacade, ValidationFacade)
request := NewContextRequest(c, LogFacade, ValidationFacade)
c.request = request
}

return c.request
}

func (c *Context) Response() http.ContextResponse {
if c.response == nil {
response := NewContextResponse(c.instance, &BodyWriter{ResponseWriter: c.instance.Writer})
c.response = response
}

responseOrigin := c.Value("responseOrigin")
if responseOrigin != nil {
return NewContextResponse(c.instance, responseOrigin.(http.ResponseOrigin))
c.response.(*ContextResponse).origin = responseOrigin.(http.ResponseOrigin)
}

return NewContextResponse(c.instance, &BodyWriter{ResponseWriter: c.instance.Writer})
return c.response
}

func (c *Context) WithValue(key any, value any) {
Expand Down
18 changes: 15 additions & 3 deletions context_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"reflect"
"strconv"
"strings"
"sync"

"github.com/gin-gonic/gin"
"github.com/gookit/validate"
Expand All @@ -23,6 +24,13 @@ import (
"github.com/spf13/cast"
)

var contextRequestPool = sync.Pool{New: func() any {
return &ContextRequest{
log: LogFacade,
validation: ValidationFacade,
}
}}

type ContextRequest struct {
ctx *Context
instance *gin.Context
Expand All @@ -32,12 +40,16 @@ type ContextRequest struct {
}

func NewContextRequest(ctx *Context, log log.Log, validation contractsvalidate.Validation) contractshttp.ContextRequest {
request := contextRequestPool.Get().(*ContextRequest)
httpBody, err := getHttpBody(ctx)
if err != nil {
LogFacade.Error(fmt.Sprintf("%+v", errors.Unwrap(err)))
log.Error(fmt.Sprintf("%+v", errors.Unwrap(err)))

Check failure on line 46 in context_request.go

View workflow job for this annotation

GitHub Actions / lint / nilaway

error: Potential nil panic detected. Observed nil flow from source to dereference point:

Check failure on line 46 in context_request.go

View workflow job for this annotation

GitHub Actions / lint / nilaway

error: Potential nil panic detected. Observed nil flow from source to dereference point:
}

return &ContextRequest{ctx: ctx, instance: ctx.instance, httpBody: httpBody, log: log, validation: validation}
request.ctx = ctx
request.instance = ctx.instance
request.httpBody = httpBody
request.validation = validation
return request
}

func (r *ContextRequest) AbortWithStatus(code int) {
Expand Down
10 changes: 9 additions & 1 deletion context_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,27 @@ package gin
import (
"bytes"
"net/http"
"sync"

"github.com/gin-gonic/gin"
contractshttp "github.com/goravel/framework/contracts/http"
"github.com/goravel/framework/support/carbon"
)

var contextResponsePool = sync.Pool{New: func() any {
return &ContextResponse{}
}}

type ContextResponse struct {
instance *gin.Context
origin contractshttp.ResponseOrigin
}

func NewContextResponse(instance *gin.Context, origin contractshttp.ResponseOrigin) *ContextResponse {
return &ContextResponse{instance, origin}
response := contextResponsePool.Get().(*ContextResponse)
response.instance = instance
response.origin = origin
return response
}

func (r *ContextResponse) Cookie(cookie contractshttp.Cookie) contractshttp.ContextResponse {
Expand Down
50 changes: 25 additions & 25 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
module github.com/goravel/gin

go 1.22.0
go 1.22.7

toolchain go1.23.3

require (
github.com/gin-gonic/gin v1.10.0
github.com/gookit/validate v1.5.2
github.com/goravel/framework v1.14.1-0.20241031053134-1404bff1b56d
github.com/goravel/framework v1.14.1-0.20241129131350-61d2c42ca9bc
github.com/rs/cors v1.11.1
github.com/savioxavier/termlink v1.4.1
github.com/spf13/cast v1.7.0
Expand All @@ -29,30 +29,30 @@ require (
github.com/atotto/clipboard v0.1.4 // indirect
github.com/aws/aws-sdk-go v1.49.6 // indirect
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/brianvoe/gofakeit/v6 v6.28.0 // indirect
github.com/bytedance/sonic v1.11.6 // indirect
github.com/bytedance/sonic/loader v0.1.1 // indirect
github.com/catppuccin/go v0.2.0 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/charmbracelet/bubbles v0.20.0 // indirect
github.com/charmbracelet/bubbletea v1.1.1 // indirect
github.com/charmbracelet/bubbletea v1.2.3 // indirect
github.com/charmbracelet/huh v0.6.0 // indirect
github.com/charmbracelet/huh/spinner v0.0.0-20241028115900-20a4d21717a8 // indirect
github.com/charmbracelet/lipgloss v0.13.1 // indirect
github.com/charmbracelet/x/ansi v0.3.2 // indirect
github.com/charmbracelet/huh/spinner v0.0.0-20241127125741-aad810dfbce6 // indirect
github.com/charmbracelet/lipgloss v1.0.0 // indirect
github.com/charmbracelet/x/ansi v0.4.5 // indirect
github.com/charmbracelet/x/exp/strings v0.0.0-20240722160745-212f7b056ed0 // indirect
github.com/charmbracelet/x/term v0.2.0 // indirect
github.com/charmbracelet/x/term v0.2.1 // indirect
github.com/cloudwego/base64x v0.1.4 // indirect
github.com/cloudwego/iasm v0.2.0 // indirect
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.5 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/dromara/carbon/v2 v2.5.0 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.6 // indirect
github.com/gabriel-vasile/mimetype v1.4.7 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/glebarez/go-sqlite v1.22.0 // indirect
github.com/glebarez/sqlite v1.11.0 // indirect
Expand All @@ -66,7 +66,6 @@ require (
github.com/go-stack/stack v1.8.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/golang-jwt/jwt/v5 v5.2.1 // indirect
github.com/golang-module/carbon/v2 v2.4.1 // indirect
github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9 // indirect
github.com/golang-sql/sqlexp v0.1.0 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
Expand Down Expand Up @@ -102,7 +101,7 @@ require (
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-localereader v0.0.1 // indirect
github.com/mattn/go-runewidth v0.0.16 // indirect
github.com/microsoft/go-mssqldb v1.6.0 // indirect
github.com/microsoft/go-mssqldb v1.7.2 // indirect
github.com/mitchellh/hashstructure/v2 v2.0.2 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
Expand All @@ -114,7 +113,7 @@ require (
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/pterm/pterm v0.12.79 // indirect
github.com/pterm/pterm v0.12.80 // indirect
github.com/rabbitmq/amqp091-go v1.9.0 // indirect
github.com/redis/go-redis/v9 v9.7.0 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
Expand All @@ -126,6 +125,7 @@ require (
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/samber/lo v1.47.0 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.11.0 // indirect
Expand Down Expand Up @@ -153,27 +153,27 @@ require (
go.uber.org/atomic v1.11.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/arch v0.8.0 // indirect
golang.org/x/crypto v0.28.0 // indirect
golang.org/x/exp v0.0.0-20241009180824-f66d83c29e7c // indirect
golang.org/x/net v0.30.0 // indirect
golang.org/x/oauth2 v0.22.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.26.0 // indirect
golang.org/x/term v0.25.0 // indirect
golang.org/x/text v0.19.0 // indirect
golang.org/x/crypto v0.29.0 // indirect
golang.org/x/exp v0.0.0-20241108190413-2d47ceb2692f // indirect
golang.org/x/net v0.31.0 // indirect
golang.org/x/oauth2 v0.23.0 // indirect
golang.org/x/sync v0.9.0 // indirect
golang.org/x/sys v0.27.0 // indirect
golang.org/x/term v0.26.0 // indirect
golang.org/x/text v0.20.0 // indirect
golang.org/x/time v0.5.0 // indirect
google.golang.org/api v0.171.0 // indirect
google.golang.org/genproto v0.0.0-20240227224415-6ceb2ff114de // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240814211410-ddb44dafa142 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240814211410-ddb44dafa142 // indirect
google.golang.org/grpc v1.67.1 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240903143218-8af14fe29dc1 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240903143218-8af14fe29dc1 // indirect
google.golang.org/grpc v1.68.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
gorm.io/driver/mysql v1.5.7 // indirect
gorm.io/driver/postgres v1.5.9 // indirect
gorm.io/driver/sqlserver v1.5.3 // indirect
gorm.io/driver/postgres v1.5.10 // indirect
gorm.io/driver/sqlserver v1.5.4 // indirect
gorm.io/gorm v1.25.12 // indirect
gorm.io/plugin/dbresolver v1.5.3 // indirect
modernc.org/libc v1.37.6 // indirect
Expand Down
Loading

0 comments on commit 0c5e251

Please sign in to comment.