卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章79477本站已运行4414

Golang 函数并发编程如何优雅地关闭并发程序?

在 go 中优雅地关闭并发程序至关重要,以避免资源泄漏和数据损坏。要实现优雅关闭,可以使用:context.context:它提供了一种通过特定 context 取消操作和关闭 goroutine 的方法。sync.waitgroup:它允许您跟踪并发程序和子 goroutine 的完成情况,从而确保在主 goroutine 中等待所有子 goroutine 完成。

Golang 函数并发编程如何优雅地关闭并发程序?

Go 函数并发编程:优雅地关闭并发程序

在 Go 中进行并发编程时,优雅地关闭并发程序至关重要,以确保应用程序资源得到正确释放,避免数据丢失或损坏。本文将介绍如何使用 context.Context 和 sync.WaitGroup 来优雅地关闭 Go 函数中的并发程序。

使用 context.Context

立即学习“go语言免费学习笔记(深入)”;

context.Context 提供了一个可以通过特定 context 取消操作和关闭 Goroutine 的简便方法。要使用它,请创建一个新的 context 并将其传递给并发函数:

func main() {
    ctx, cancelFunc := context.WithCancel(context.Background())

    // 子 Goroutine 会监控 ctx.Done() 频道以获得取消信号
    go func() {
        for {
            select {
            case <-ctx.Done():
                // 处理取消信号并在必要时清理资源
                cancelFunc()
                return
            default:
                // 继续正常运行
            }
        }
    }()

    // 正常应用程序代码...

    // 当需要关闭并发程序时,调用cancelFunc()
    cancelFunc()
}

使用 sync.WaitGroup

sync.WaitGroup 允许您跟踪并发程序和子 Goroutine 的完成情况。在 main Goroutine 中使用 WaitGroup 等待子 Goroutine 完成:

func main() {
    var wg sync.WaitGroup

    for i := 0; i < 10; i++ {
        wg.Add(1)
        go func(i int) {
            defer wg.Done()
            // 子 Goroutine 的代码
        }(i)
    }

    // 等待所有子 Goroutine 完成
    wg.Wait()
}

实战案例

网络服务器

在网络服务器中,需要关闭并发程序以在服务器终止信号时优雅地释放资源。可以通过以下方式使用 context.Context:

func main() {
    listener, err := net.Listen("tcp", ":8080")
    if err != nil {
        log.Fatal(err)
    }

    // 创建用于管理 Goroutine 的 context
    ctx, cancelFunc := context.WithCancel(context.Background())

    go func() {
        for {
            conn, err := listener.Accept()
            if err != nil {
                log.Println(err)
                continue
            }

            // 创建针对新连接的新 context,它将从父 context 继承取消信号
            ctxConn, cancelConn := context.WithCancel(ctx)

            go handleConnection(ctxConn, conn)
        }
    }()

    <-ctx.Done()
    cancelFunc()
    listener.Close()
}

func handleConnection(ctx context.Context, conn net.Conn) {
    for {
        select {
        case <-ctx.Done():
            conn.Close()
            return
        default:
            // 处理传入连接
        }
    }
}

数据库连接池

在数据库连接池中,需要关闭并发程序以在应用程序关闭时释放数据库连接。可以使用 sync.WaitGroup:

func main() {
    // 创建一个WaitGroup来跟踪所有打开的数据库连接
    var wg sync.WaitGroup

    // 创建连接池
    pool := make([]*sql.DB, 0)

    for i := 0; i < 10; i++ {
        wg.Add(1)
        go func() {
            // 连接到数据库
            db, err := sql.Open("postgres", "user=postgres password=postgres database=test")
            if err != nil {
                log.Fatal(err)
            }

            // 将连接添加到连接池
            pool = append(pool, db)
            defer func() {
                wg.Done()
                db.Close()
            }()

            // 继续正常操作
        }()
    }

    // 等待所有连接打开
    wg.Wait()

    // 应用程序正常运行....

    // 关闭应用程序时,关闭所有数据库连接
    for _, db := range pool {
        db.Close()
    }
}
卓越飞翔博客
上一篇: Golang 函数链中如何进行单元测试?
下一篇: 返回列表
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏