go-zero icon indicating copy to clipboard operation
go-zero copied to clipboard

How to set timeout params in sqlx.NewMysql

Open andy-zhangtao opened this issue 1 year ago β€’ 15 comments

Hi,

I want to set some mysql params (e.g. timeout), but I found the SqlOption is private var.

func NewMysql(datasource string, opts ...SqlOption) SqlConn {
	opts = append(opts, withMysqlAcceptable())
	return NewSqlConn(mysqlDriverName, datasource, opts...)
}
SqlOption func(*commonSqlConn)

So how to fix or workaround?

andy-zhangtao avatar Jul 28 '22 10:07 andy-zhangtao

I found a workaround. Add timeout params in Mysql DSN string, but it not what my expect . So how to fix it?

andy-zhangtao avatar Jul 28 '22 10:07 andy-zhangtao

Is there any progress?

andy-zhangtao avatar Jul 30 '22 07:07 andy-zhangtao

Is there any progress?

andy-zhangtao avatar Aug 01 '22 14:08 andy-zhangtao

Has left six days, but there is no progress .... πŸ’”

andy-zhangtao avatar Aug 03 '22 05:08 andy-zhangtao

Open 10 days ago, no progress still ...

andy-zhangtao avatar Aug 07 '22 14:08 andy-zhangtao

Bot detected the issue body's language is not English, translate it automatically. πŸ‘―πŸ‘­πŸ»πŸ§‘β€πŸ€β€πŸ§‘πŸ‘«πŸ§‘πŸΏβ€πŸ€β€πŸ§‘πŸ»πŸ‘©πŸΎβ€πŸ€β€πŸ‘¨πŸΏπŸ‘¬πŸΏ


Open 10 days ago, no progress still ...

Issues-translate-bot avatar Aug 07 '22 14:08 Issues-translate-bot

Open 18 days ago, no progress still ...

andy-zhangtao avatar Aug 15 '22 00:08 andy-zhangtao

Sorry, but what timeout do you want to set? Dial or exec?

kevwan avatar Aug 18 '22 00:08 kevwan

Dial , the Ping phase, to be exact.. I expect to control the timeout when execute Ping(). If ping failed, then it will quick failure

andy-zhangtao avatar Aug 18 '22 01:08 andy-zhangtao

Did you encounter any problem so far?

kevwan avatar Aug 18 '22 01:08 kevwan

Would you give a detail example on the scenarios?

kevwan avatar Aug 18 '22 01:08 kevwan

in core/stores/sqlx/sqlmanager.go L43

func getSqlConn(driverName, server string) (*sql.DB, error) {
	pdb, err := getCachedSqlConn(driverName, server)
	if err != nil {
		return nil, err
	}

	pdb.once.Do(func() {
		err = pdb.Ping()
	})
	if err != nil {
		return nil, err
	}

	return pdb.DB, nil
}

If mysql connect failed, the Ping() will hang about 30s. In fact, we needn't this hang time. So I want to add some timeout params in NewMysql with SqlOption (e.g. 5s for ping timeout).

func NewMysql(datasource string, opts ...SqlOption) SqlConn {
	opts = append(opts, withMysqlAcceptable())
	return NewSqlConn(mysqlDriverName, datasource, opts...)
}

But SqlOption func(*commonSqlConn) is private var, I can not use it in my code. Now I use mysql dsn with timeout params.

I want to know , is there has other ways to change it?

If you wants to get more detail info, please let know.

andy-zhangtao avatar Aug 18 '22 02:08 andy-zhangtao

Open 25 days ago, no substantial progress, and I think there has a inactive community

andy-zhangtao avatar Aug 22 '22 01:08 andy-zhangtao

Didn't figure out a good solution. I read but no reply, sorry.

kevwan avatar Aug 22 '22 04:08 kevwan

I found a workaround. Add timeout params in Mysql DSN string, but it not what my expect . So how to fix it?

I think the timeout params in DSN is already a better soluion.

kevwan avatar Aug 22 '22 04:08 kevwan

This issue is stale because it has been open for 30 days with no activity.

github-actions[bot] avatar Aug 23 '23 01:08 github-actions[bot]

This issue was closed because it has been inactive for 14 days since being marked as stale.

github-actions[bot] avatar Mar 19 '24 01:03 github-actions[bot]