Skip to content

sql db connection not check the server whether closed the connection ,so it caused the tcp4 sockets has many can't identify protocol or TCP localhost:11335->localhost:6379 (WAIT_CLOSED) #707

Closed
@busyfree

Description

@busyfree

Issue description

sql db connection not check the server whether closed the connection ,so it caused the tcp4 sockets has many can't identify protocol or localhost:54190->localhost:mysql (WAIT_CLOSED)

db open config

max_idle_conns = 5
max_open_conns = 30
max_life_time=600
timeout=600

image

image

Example code

package dbtest

import (
	"database/sql"
	"fmt"
	"log"
	"os"
	"sync"
	"time"
	"github.com/golang/glog"
	"gopkg.in/gorp.v2"
)

type DatabaseConfig struct {
	DBName               string `toml:"dbname"`
	Host                 string `toml:"host"`
	Port                 int    `toml:"port"`
	User                 string `toml:"user"`
	Password             string `toml:"password"`
	Sslmode              string `toml:"sslmode"`
	ShowLog              bool
	DataSaveDir          string
	DataFileSaveLoopSize int
	MaxIdleConns         int `toml:"max_idle_conns"`
	MaxOpenConns         int `toml:"max_open_conns"`
	MaxLifeTime          int `toml:"max_life_time"`
	Timeout              int `toml:"timeout"`
	RTimeout             int `toml:"rtimeout"`
	WTimeout             int `toml:"wtimeout"`
}

func (c DatabaseConfig) MySQLSource() string {
	params := make(map[string]string, 0)
	params["charset"] = "utf8mb4"
	cfg := mysql.Config{}
	cfg.User = c.User
	cfg.Passwd = c.Password
	cfg.DBName = c.DBName
	cfg.ParseTime = true
	cfg.Collation = "utf8mb4_unicode_ci"
	cfg.Params = params
	cfg.Loc, _ = time.LoadLocation("Asia/Chongqing")
	cfg.Timeout = time.Duration(c.Timeout) * time.Second
	cfg.MultiStatements = true
	cfg.ReadTimeout = time.Duration(c.RTimeout) * time.Second
	cfg.WriteTimeout = time.Duration(c.WTimeout) * time.Second
	return cfg.FormatDSN()
}


var (
	dbmap                      *gorp.DbMap
	Dbm                        *gorp.DbMap
	config                     DatabaseConfig
	opened                     bool
	openMutex                  sync.RWMutex
	DB                         *sql.DB
)

//Open open the database for passport with config
func Open(cfg DatabaseConfig) {
	if !opened {
		config = cfg
		db, err := sql.Open("mysql", config.MySQLSource())
		glog.Infof("open err %v ", err)
		if err != nil {
			panic(fmt.Errorf("sql.Open failed: %v", err))
		}
		if config.MaxLifeTime > 0 {
			db.SetConnMaxLifetime(time.Duration(config.MaxLifeTime) * time.Second)
		}
		db.SetMaxIdleConns(config.MaxIdleConns)
		db.SetMaxOpenConns(config.MaxOpenConns)
		db.Ping()
		DB = db
		// construct a gorp DbMap
		dbmap = &gorp.DbMap{Db: db, Dialect: gorp.MySQLDialect{"InnoDB", "utf8mb4"}}
		Dbm = dbmap

		err = dbmap.CreateTablesIfNotExists()
		if err != nil {
			panic("create table failed " + err.Error())
		}
		openMutex.Lock()
		opened = true
		openMutex.Unlock()
		if config.ShowLog {
			dbmap.TraceOn("[gorp]", log.New(os.Stdout, schemaName+" ", log.Lmicroseconds))
		}
	}
}

//Close close the database for passport
func Close() {
	if dbmap != nil && opened {
		glog.Infof("close database %s for %s", config.DBName, schemaName)
		dbmap.Db.Close()
		openMutex.Lock()
		opened = false
		openMutex.Unlock()
	}
}

Error log

2017/11/16 11:32:13 http: Accept error: accept tcp 127.0.0.1:hide : accept4: too many open files; retrying in 1s
2017/11/16 11:32:14 http: Accept error: accept tcp 127.0.0.1:hide: accept4: too many open files; retrying in 1s
2017/11/16 11:32:15 http: Accept error: accept tcp 127.0.0.1:hide: accept4: too many open files; retrying in 1s
2017/11/16 11:32:16 http: Accept error: accept tcp 127.0.0.1:hide: accept4: too many open files; retrying in 1s
2017/11/16 11:32:17 http: Accept error: accept tcp 127.0.0.1:hide: accept4: too many open files; retrying in 1s
2017/11/16 11:32:18 http: Accept error: accept tcp 127.0.0.1:hide: accept4: too many open files; retrying in 1s
2017/11/16 11:32:19 http: Accept error: accept tcp 127.0.0.1:hide: accept4: too many open files; retrying in 1s
2017/11/16 11:32:20 http: Accept error: accept tcp 127.0.0.1:hide: accept4: too many open files; retrying in 1s
2017/11/16 11:32:21 http: Accept error: accept tcp 127.0.0.1:hide: accept4: too many open files; retrying in 1s
2017/11/16 11:32:22 http: Accept error: accept tcp 127.0.0.1:hide: accept4: too many open files; retrying in 1s

Configuration

Driver version (or git SHA):

*78d399c - (HEAD -> master, origin/master, origin/HEAD) Implement NamedValueChecker for mysqlConn (#690)

Go version: run go version in your console
go version go1.9 linux/amd64

Server version: E.g. MySQL 5.6, MariaDB 10.0.20

Server version: 5.6.15-log MySQL Community Server (GPL)

Server OS: E.g. Debian 8.1 (Jessie), Windows 10
CentOS release 6.7 (Final)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions