Skip to content

Commit

Permalink
feat: optimize
Browse files Browse the repository at this point in the history
  • Loading branch information
devhaozi committed Nov 30, 2024
1 parent 9738dfc commit 23323d6
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 7 deletions.
6 changes: 3 additions & 3 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ type Context struct {
response http.ContextResponse
}

func NewContext() http.Context {
func NewContext() *Context {
return contextPool.Get().(*Context)
}

func (c *Context) Request() http.ContextRequest {
if c.request == nil {
request := NewContextRequest().(*ContextRequest)
request := NewContextRequest()
httpBody, err := getHttpBody(c)
if err != nil {
LogFacade.Error(fmt.Sprintf("%+v", errors.Unwrap(err)))

Check failure on line 41 in context.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 41 in context.go

View workflow job for this annotation

GitHub Actions / lint / nilaway

error: Potential nil panic detected. Observed nil flow from source to dereference point:
Expand All @@ -51,7 +51,7 @@ func (c *Context) Request() http.ContextRequest {

func (c *Context) Response() http.ContextResponse {
if c.response == nil {
response := NewContextResponse().(*ContextResponse)
response := NewContextResponse()
response.instance = c.instance
c.response = response
}
Expand Down
2 changes: 1 addition & 1 deletion context_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type ContextRequest struct {
validation contractsvalidate.Validation
}

func NewContextRequest() contractshttp.ContextRequest {
func NewContextRequest() *ContextRequest {
return contextRequestPool.Get().(*ContextRequest)
}

Expand Down
2 changes: 1 addition & 1 deletion context_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type ContextResponse struct {
origin contractshttp.ResponseOrigin
}

func NewContextResponse() contractshttp.ContextResponse {
func NewContextResponse() *ContextResponse {
return contextResponsePool.Get().(*ContextResponse)
}

Expand Down
4 changes: 2 additions & 2 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func middlewaresToGinHandlers(middlewares []httpcontract.Middleware) []gin.Handl

func handlerToGinHandler(handler httpcontract.HandlerFunc) gin.HandlerFunc {
return func(c *gin.Context) {
context := NewContext().(*Context)
context := NewContext()
defer func() {
contextRequestPool.Put(context.request)
contextResponsePool.Put(context.response)
Expand All @@ -45,7 +45,7 @@ func handlerToGinHandler(handler httpcontract.HandlerFunc) gin.HandlerFunc {

func middlewareToGinHandler(middleware httpcontract.Middleware) gin.HandlerFunc {
return func(c *gin.Context) {
context := NewContext().(*Context)
context := NewContext()
defer func() {
contextRequestPool.Put(context.request)
contextResponsePool.Put(context.response)
Expand Down

0 comments on commit 23323d6

Please sign in to comment.