| 15 | ) |
| 16 | |
| 17 | func GetDbConnection(dbType string, connectionString string) (*sqlx.DB, error) { |
| 18 | |
| 19 | if dbType == "mysql" && strings.Index(connectionString, "charset=") == -1 { |
| 20 | if strings.Index(connectionString, "?") > -1 { |
| 21 | connectionString = connectionString + "&charset=utf8mb4" |
| 22 | } else { |
| 23 | connectionString = connectionString + "?charset=utf8mb4" |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | if dbType == "mysql" && strings.Index(connectionString, "collation=") == -1 { |
| 28 | if strings.Index(connectionString, "?") > -1 { |
| 29 | connectionString = connectionString + "&collation=utf8mb4_unicode_ci" |
| 30 | } else { |
| 31 | connectionString = connectionString + "?collation=utf8mb4_unicode_ci" |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | db, e := sqlx.Open(dbType, connectionString) |
| 36 | |
| 37 | if e != nil { |
| 38 | return nil, e |
| 39 | } |
| 40 | |
| 41 | maxIdleConnections := os.Getenv("DAPTIN_MAX_IDLE_CONNECTIONS") |
| 42 | if maxIdleConnections == "" { |
| 43 | maxIdleConnections = "10" |
| 44 | } |
| 45 | maxOpenConnections := os.Getenv("DAPTIN_MAX_OPEN_CONNECTIONS") |
| 46 | if maxOpenConnections == "" { |
| 47 | maxOpenConnections = "50" |
| 48 | } |
| 49 | if strings.Index(dbType, "sqlite") > -1 { |
| 50 | maxOpenConnections = "1" |
| 51 | } |
| 52 | maxConnectionLifetimeMinString := os.Getenv("DAPTIN_MAX_CONNECTIONS_LIFETIME") |
| 53 | if maxConnectionLifetimeMinString == "" { |
| 54 | maxConnectionLifetimeMinString = "1" |
| 55 | } |
| 56 | maxConnectionIdleTimeMinString := os.Getenv("DAPTIN_MAX_IDLE_CONNECTIONS_TIME") |
| 57 | if maxConnectionIdleTimeMinString == "" { |
| 58 | maxConnectionIdleTimeMinString = "1" |
| 59 | } |
| 60 | |
| 61 | maxIdleConnectionsInt, err := strconv.ParseInt(maxIdleConnections, 10, 64) |
| 62 | if err != nil { |
| 63 | maxIdleConnectionsInt = 10 |
| 64 | } |
| 65 | |
| 66 | maxOpenConnectionsInt, err := strconv.ParseInt(maxOpenConnections, 10, 64) |
| 67 | if err != nil { |
| 68 | maxOpenConnectionsInt = 50 |
| 69 | } |
| 70 | |
| 71 | maxConnectionLifetimeMinInt, err := strconv.ParseInt(maxConnectionLifetimeMinString, 10, 64) |
| 72 | if err != nil { |
| 73 | maxConnectionLifetimeMinInt = 5 |
| 74 | } |