Skip to content

Commit

Permalink
Merge pull request #148 from sunmi-OS/feature/imp
Browse files Browse the repository at this point in the history
add WithoutCancel context
  • Loading branch information
luduoxin authored Jan 19, 2024
2 parents f0411c2 + 7d309e5 commit fe617f5
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
6 changes: 3 additions & 3 deletions api/gin.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,18 +193,18 @@ func logger(ignoreRelease bool) gin.HandlerFunc {
start := time.Now()
path := c.Request.URL.Path
raw := c.Request.URL.RawQuery

// Process request
c.Next()
if raw != "" {
path = path + "?" + raw
}

// ignore logger output
if gin.Mode() == gin.ReleaseMode && ignoreRelease {
return
}

if path == "/health" || path == "/monitor/prometheus" {
return
}
// End time
end := time.Now()
fmt.Fprintf(os.Stdout, "[GIN] %s | %3d | %13v | %15s | %-7s %#v\n%s", end.Format("2006/01/02 - 15:04:05"), c.Writer.Status(), end.Sub(start), c.ClientIP(), c.Request.Method, path, c.Errors.ByType(gin.ErrorTypePrivate).String())
Expand Down
2 changes: 1 addition & 1 deletion lib/middleware/recovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func Recovery() gin.HandlerFunc {
}{
Code: -1,
Data: nil,
Msg: "gin panic",
Msg: "server panic",
})
c.Abort()
}
Expand Down
31 changes: 31 additions & 0 deletions utils/context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package utils

import (
"context"
"time"
)

// WithoutCancel returns a copy of parent that is not canceled when parent is canceled.
// The returned context returns no Deadline or Err, and its Done channel is nil.
func WithoutCancel(parent context.Context) context.Context {
if parent == nil {
parent = context.Background()
}
return withoutCancelCtx{parent}
}

type withoutCancelCtx struct {
context.Context
}

func (withoutCancelCtx) Deadline() (deadline time.Time, ok bool) {
return
}

func (withoutCancelCtx) Done() <-chan struct{} {
return nil
}

func (withoutCancelCtx) Err() error {
return nil
}

0 comments on commit fe617f5

Please sign in to comment.