Description

file1: results of enforcement

package main

import (
    sc "context"
    "fmt"
    "github.com/gin-gonic/gin"
    "time"
)

func main() {
    g := gin.Default()
    g.GET("index", func(context *gin.Context) {
        d := time.Now().Add(2 * time.Second)
        ctx, cancel := sc.WithDeadline(sc.Background(), d)
        go func(c sc.Context) {
            fmt.Println(1, time.Now().Format("2006-01-02 15:04:05"))
            time.Sleep(3 * time.Second)
            fmt.Println(2, time.Now().Format("2006-01-02 15:04:05"))
        }(ctx)
        defer cancel()
        fmt.Println(3, time.Now().Format("2006-01-02 15:04:05"))
        select {
        case <-time.After(1 * time.Second):
            fmt.Println(4, time.Now().Format("2006-01-02 15:04:05"))
            fmt.Println("overslept")
            return
        case <-ctx.Done():
            fmt.Println(5, time.Now().Format("2006-01-02 15:04:05"))
            fmt.Println(ctx.Err())
            return
        }
    })

    g.Run(":8999")

}
// 3 2023-08-17 23:36:53
// 1 2023-08-17 23:36:53
// 4 2023-08-17 23:36:54
// overslept
// 2 2023-08-17 23:36:56

file2: results of enforcement

package main

import (
    sc "context"
    "fmt"
    "time"
)

func main() {
    d := time.Now().Add(2 * time.Second)
    ctx, cancel := sc.WithDeadline(sc.Background(), d)
    go func(c sc.Context) {
        fmt.Println(1, time.Now().Format("2006-01-02 15:04:05"))
        time.Sleep(3 * time.Second)
        fmt.Println(2, time.Now().Format("2006-01-02 15:04:05"))
    }(ctx)
    defer cancel()
    fmt.Println(3, time.Now().Format("2006-01-02 15:04:05"))
    select {
    case <-time.After(1 * time.Second):
        fmt.Println(4, time.Now().Format("2006-01-02 15:04:05"))
        fmt.Println("overslept")
        return
    case <-ctx.Done():
        fmt.Println(5, time.Now().Format("2006-01-02 15:04:05"))
        fmt.Println(ctx.Err())
        return
    }

}
// 3 2023-08-17 23:43:52
// 1 2023-08-17 23:43:52
// 4 2023-08-17 23:43:53
// overslept

file1 has "2 2023-08-17 23:36:56", but file no this? why ?

Environment

  • go version: 1.21.0
  • gin version (or commit ref): v1.9.1
  • operating system: MAC M1

Comment From: kaylee595

@1207702931 The reason "2 2023-08-17 23:36:56" does not appear is that the program exited. This is a very simple example, just prevent the "file2" program from exiting immediately and "2 2023-08-17 23:36:56" will appear.

func main() {
    d := time.Now().Add(2 * time.Second)
    ctx, cancel := sc.WithDeadline(sc.Background(), d)
    go func(c sc.Context) {
        fmt.Println(1, time.Now().Format("2006-01-02 15:04:05"))
        time.Sleep(3 * time.Second)
        fmt.Println(2, time.Now().Format("2006-01-02 15:04:05"))
    }(ctx)
    defer cancel()
    fmt.Println(3, time.Now().Format("2006-01-02 15:04:05"))
    select {
    case <-time.After(1 * time.Second):
        fmt.Println(4, time.Now().Format("2006-01-02 15:04:05"))
        fmt.Println("overslept")
    case <-ctx.Done():
        fmt.Println(5, time.Now().Format("2006-01-02 15:04:05"))
        fmt.Println(ctx.Err())
    }
    select {}
}

Comment From: 1207702931

Thank you, my basic knowledge is not very good