tidb.go 968 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. package tidb
  2. import (
  3. "go-common/library/log"
  4. "go-common/library/net/netutil/breaker"
  5. "go-common/library/stat"
  6. "go-common/library/time"
  7. // database driver
  8. _ "github.com/go-sql-driver/mysql"
  9. )
  10. var stats = stat.DB
  11. // Config mysql config.
  12. type Config struct {
  13. DSN string // dsn
  14. Active int // pool
  15. Idle int // pool
  16. IdleTimeout time.Duration // connect max life time.
  17. QueryTimeout time.Duration // query sql timeout
  18. ExecTimeout time.Duration // execute sql timeout
  19. TranTimeout time.Duration // transaction sql timeout
  20. Breaker *breaker.Config // breaker
  21. }
  22. // NewTiDB new db and retry connection when has error.
  23. func NewTiDB(c *Config) (db *DB) {
  24. if c.QueryTimeout == 0 || c.ExecTimeout == 0 || c.TranTimeout == 0 {
  25. panic("tidb must be set query/execute/transction timeout")
  26. }
  27. db, err := Open(c)
  28. if err != nil {
  29. log.Error("open tidb error(%v)", err)
  30. panic(err)
  31. }
  32. return
  33. }