博客
关于我
记一次go中map并发引起的事故
阅读量:456 次
发布时间:2019-03-06

本文共 1640 字,大约阅读时间需要 5 分钟。

错误使用 map 引发的血案

最近业务中,同事使用 map 来接收返回的结果,使用 WaitGroup 来并发处理执行返回的结果,结果上线之后,直接崩了。

日志分析

日志显示大量的数据库缓存池连接失败:

{"ecode":-500,"message":"timed out while checking out a connection from connection pool"}{"ecode":-500,"message":"connection(xxxxxxxxx:xxxxx) failed to write: context deadline exceeded"}

场景复原

来看伪代码:

package mainimport (    "fmt"    "sync"    "time")var count = 300func main() {    var data = make(map[int]string, count)    var wg sync.WaitGroup    for i := 0; i < count; i++ {        wg.Add(1)        go func(i int) {            defer wg.Done()            time.Sleep(time.Second * 1)            mockSqlPool()            data[i] = "test"        }(i)    }    fmt.Println("-----------WaitGroup执行结束了-----------")    wg.Wait()}// 模拟数据库的连接和释放func mockSqlPool() {    defer fmt.Println("关闭pool")    fmt.Println("我是pool")}

运行输出

运行的输出显示:

...我是pool关闭poolconcurrent map writes我是poolgoroutine 56 [running]: runtime.throw(...)...

错误原因

  • map 不是并发安全

    map 在并发环境下不是安全的,特别是在多个 goroutine 同时修改时,会导致 panic
    在示例中,多个 goroutine 并发写入 map,导致并发修改,触发 concurrent map writes 错误。

  • 避免在循环中连接数据库

    在循环中频繁连接数据库会导致连接泄漏,进而导致数据库连接池连接无法被及时释放。

  • WaitGroup 的信号量

    WaitGroup 使用信号量机制来管理 goroutine 的等待状态:

    • Wait() 会阻塞,直到所有 goroutine 完成。
    • Done() 会在最后一个 goroutine 完成时,释放信号量,允许 Wait() 继续执行。

    goroutine 状态分析

    输出显示有多个 goroutine 处于 semacquire 状态,这意味着它们正在等待信号量被唤醒。然而,由于 WaitGrouppanic 退出,无法通过 Done() 通知 goroutine 退出,导致这些 goroutine 阻塞到最后,占用数据库连接,造成连接泄漏。

    解决方案

  • 使用 sync.RwLocksync.Mutex 保护 map

    确保在并发环境下,map 的读写操作是安全的。

  • 避免在 goroutine 中直接访问数据库连接

    使用数据库连接池(如 sqlc 或自定义连接池)来管理数据库连接,确保连接能被及时释放。

  • 优化 WaitGroup 使用

    确保在 WaitGroup 中正确处理 Done() 信号,避免 panic 导致的资源泄漏。

  • 通过以上优化,可以避免并发写入导致的 panic,同时确保数据库连接能够被正确释放,避免连接泄漏问题。

    转载地址:http://xzcfz.baihongyu.com/

    你可能感兴趣的文章
    Oracle闪回技术(Flashback)
    查看>>
    oracle零碎要点---ip地址问题,服务问题,系统默认密码问题
    查看>>
    oracle零碎要点---oracle em的web访问地址忘了
    查看>>
    Oracle零碎要点---多表联合查询,收集数据库基本资料
    查看>>
    Oracle静默安装
    查看>>
    Oracle面试题:Oracle中truncate和delete的区别
    查看>>
    ThreadLocal线程内部存储类
    查看>>
    thinkphp 常用SQL执行语句总结
    查看>>
    Oracle:ORA-00911: 无效字符
    查看>>
    Text-to-Image with Diffusion models的巅峰之作:深入解读 DALL·E 2
    查看>>
    TCP基本入门-简单认识一下什么是TCP
    查看>>
    tableviewcell 中使用autolayout自适应高度
    查看>>
    Orcale表被锁
    查看>>
    org.apache.ibatis.exceptions.TooManyResultsException: Expected one result (or null) to be returned
    查看>>
    org.apache.ibatis.type.TypeException: Could not resolve type alias 'xxxx'异常
    查看>>
    org.apache.poi.hssf.util.Region
    查看>>
    org.apache.zookeeper.KeeperException$ConnectionLossException: KeeperErrorCode = ConnectionLoss for /
    查看>>
    org.hibernate.HibernateException: Unable to get the default Bean Validation factory
    查看>>
    org.hibernate.ObjectNotFoundException: No row with the given identifier exists:
    查看>>
    org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
    查看>>