简体   繁体   English

多重并发时如何解决TIME_WAIT状态问题?

[英]How to solve TIME_WAIT state issue when in multiple concurrency?

If I run below example on Windows I will quickly hit TCP connection limit (which I set to 64k) and get error: dial tcp 127.0.0.1:3306: connectex: Only one usage of each socket address (protocol/network address/port) is normally permitted. 如果我在Windows上运行以下示例,我将很快达到TCP连接限制(我将其设置为64k)并得到错误: dial tcp 127.0.0.1:3306: connectex: Only one usage of each socket address (protocol/network address/port) is normally permitted.

I see all this TIME_WAIT states waiting for there lifetime to end with: netstat -ano|findstr 3306 我看到所有这些TIME_WAIT状态都在等待生命周期结束: netstat -ano|findstr 3306

Why aren't connections closed immediately? 为什么不立即关闭连接?

The code: 编码:

package main

import (
    _ "github.com/go-sql-driver/mysql"
    "github.com/jmoiron/sqlx"
    "log"
    "sync"
)

var (
    db_instance *sqlx.DB
    wg          sync.WaitGroup
)

func main() {
    db, err := sqlx.Connect("mysql", "user:pass@/table")
    if err != nil {
        log.Fatalln(err)
    }
    defer db.Close()
    db_instance = db

    for {
        for l := 0; l < 50; l++ {
            wg.Add(1)
            go DB_TEST()
        }
        wg.Wait()
    }
}

func DB_TEST() {
    defer wg.Done()

    var s string
    err := db_instance.QueryRow("SELECT NOW()").Scan(&s)
    if err != nil {
        log.Println(err)
        return
    }
    log.Println(s)
}

Drafting answer from my comments discussion with @Glavić. 我与@Glavić的评论讨论的起草答案。

Utilize the SetMaxOpenConns and SetMaxIdleConns settings to keep TIME_WAIT status and connections under control. 利用SetMaxOpenConnsSetMaxIdleConns设置来控制TIME_WAIT状态和连接。 If needed use SetConnMaxLifetime too, generally it's not needed. 如果需要,也可以使用SetConnMaxLifetime ,通常是不需要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 .NET Web服务通过TIME_WAIT打开许多MySQL连接 - .NET Web Service leaving many MySQL connections open with TIME_WAIT 读取/更新时休眠并发(?)问题 - Hibernate concurrency(?) issue when reading/updating 如何防止数据库更新的并发问题? - How to prevent concurrency issue with database update? 在横向扩展的架构中,多个实例同时读写同一行时,如何处理数据库并发? - In a horizontally scalable architecture, how to deal with database concurrency when multiple instances read/write to the same row simultaneously? 当我尝试创建此过程时,如何解决此错误“ ERROR 102 sql state 420000” - How to solve this error “ERROR 102 sql state 420000” which comes up when I try to create this procedure 如何解决mysql中的时间数据类型 - How to solve time datatype in mysql 创建models.py时发现row size太大问题,如何解决 - When creating models.py ,the row size too large issue is found ,how to solve this problem sol如何解决此数据库迁移问题? - How sol solve this database migration issue? 何时使用悲观并发? - When to use pessimistic concurrency? 时间戳混淆:并发问题的解决方案 - Confusion on timestamping: solution for concurrency issue
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM