Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add WithoutCancel context #148

Merged
merged 1 commit into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
Loading