Skip to content

Commit

Permalink
sql: limit maximum open connections, document timeout options through…
Browse files Browse the repository at this point in the history
… DSN - closes #359
  • Loading branch information
Aeneas Rekkas (arekkas) committed Jan 22, 2017
1 parent d695794 commit 01bd815
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions config/backend_connections.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
r "gopkg.in/dancannon/gorethink.v2"
"gopkg.in/redis.v5"
"strings"
"runtime"
)

type MemoryConnection struct{}
Expand Down Expand Up @@ -53,9 +54,54 @@ func (c *SQLConnection) GetDatabase() *sqlx.DB {
logrus.Fatalf("Could not connect to SQL: %s", err)
}



maxConns := maxParallelism() * 2
if v := c.URL.Query().Get("max_conns"); v != "" {
s, err := strconv.ParseInt(v, 10, 64)
if err != nil {
logrus.Warnf("max_conns value %s could not be parsed to int: %s", v, err)
} else {
maxConns = int(s)
}
}

maxIdleConns := maxParallelism()
if v := c.URL.Query().Get("max_idle_conns"); v != "" {
s, err := strconv.ParseInt(v, 10, 64)
if err != nil {
logrus.Warnf("max_idle_conns value %s could not be parsed to int: %s", v, err)
} else {
maxIdleConns = int(s)
}
}

maxConnLifetime := time.Duration(0)
if v := c.URL.Query().Get("max_idle_conns"); v != "" {
s, err := time.ParseDuration(maxConnLifetime)
if err != nil {
logrus.Warnf("max_idle_conns value %s could not be parsed to int: %s", v, err)
} else {
maxConnLifetime = s
}
}

c.db.SetMaxOpenConns(maxConns)
c.db.SetMaxIdleConns(maxIdleConns)
c.db.SetConnMaxLifetime(maxConnLifetime)

return c.db
}

func maxParallelism() int {
maxProcs := runtime.GOMAXPROCS(0)
numCPU := runtime.NumCPU()
if maxProcs < numCPU {
return maxProcs
}
return numCPU
}

type RethinkDBConnection struct {
session *r.Session
URL *url.URL
Expand Down

0 comments on commit 01bd815

Please sign in to comment.