-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
137 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package connection | ||
|
||
import ( | ||
"database/sql" | ||
"database/sql/driver" | ||
"net" | ||
"time" | ||
|
||
"github.com/lib/pq" | ||
) | ||
|
||
// KeepAliveDuration is the duration between keepalives for all new Postgres | ||
// connections. | ||
var KeepAliveDuration = 5 * time.Second | ||
|
||
func init() { | ||
sql.Register("gatherer-pq", &enhancedDriver{}) | ||
} | ||
|
||
// enhancedDriver is a wrapper over lib/pq to mimic jackc/pgx's keepalive | ||
// policy. This avoids an issue where the NAT kills an "idle" connection while | ||
// it is waiting on a long-running query. | ||
type enhancedDriver struct{} | ||
|
||
// Open returns a new SQL driver connection with our custom settings. | ||
func (d *enhancedDriver) Open(name string) (driver.Conn, error) { | ||
return pq.DialOpen(&dialer{}, name) | ||
} | ||
|
||
type dialer struct{} | ||
|
||
func (d dialer) Dial(ntw, addr string) (net.Conn, error) { | ||
customDialer := net.Dialer{KeepAlive: KeepAliveDuration} | ||
return customDialer.Dial(ntw, addr) | ||
} | ||
|
||
func (d dialer) DialTimeout(ntw, addr string, timeout time.Duration) (net.Conn, error) { | ||
customDialer := net.Dialer{Timeout: timeout, KeepAlive: KeepAliveDuration} | ||
return customDialer.Dial(ntw, addr) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package connection | ||
|
||
import ( | ||
"database/sql" | ||
"sync" | ||
"testing" | ||
|
||
lua "github.com/yuin/gopher-lua" | ||
) | ||
|
||
const doAvailableConnections = ` | ||
connection:query('select 1') | ||
for _, conn in pairs(connection:available_connections()) do | ||
conn:query('select 1') | ||
end | ||
` | ||
|
||
func TestNew(t *testing.T) { | ||
wait := sync.WaitGroup{} | ||
count := 100 | ||
wait.Add(count) | ||
countOfDatabases := getCountOfDatabases(t) | ||
SetMaxOpenConns(1) | ||
for i := 0; i < count; i++ { | ||
go func() { | ||
defer wait.Done() | ||
state := lua.NewState() | ||
Preload(state) | ||
params := make(map[string]string) | ||
params[`fallback_application_name`] = `test` | ||
params[`connect_timeout`] = `5` | ||
New(state, `connection`, | ||
"/tmp", "gatherer", "gatherer", "", 5432, params) | ||
if err := state.DoString(doAvailableConnections); err != nil { | ||
t.Fatalf("do: %s\n", err.Error()) | ||
} | ||
}() | ||
} | ||
wait.Wait() | ||
if *poolDatabasesOpen != int32(countOfDatabases) { | ||
t.Fatalf("open: %d count: %d\n", *poolDatabasesOpen, countOfDatabases) | ||
} | ||
if len(connectionPool.pool) != countOfDatabases { | ||
t.Fatalf("pool: %#v\n", connectionPool.pool) | ||
} | ||
if connections := getCountOfApplicationNameTest(t); connections != countOfDatabases { | ||
t.Fatalf("databases: %d connections: %d\n", countOfDatabases, connections) | ||
} | ||
} | ||
|
||
func getCountOfDatabases(t *testing.T) int { | ||
db, err := sql.Open(`postgres`, `host=/tmp dbname=gatherer user=gatherer port=5432`) | ||
if err != nil { | ||
t.Fatalf("open: %s\n", err.Error()) | ||
} | ||
row := db.QueryRow(`select | ||
count(d.datname) | ||
from | ||
pg_catalog.pg_database d | ||
where has_database_privilege(d.datname, 'connect') and not d.datistemplate | ||
`) | ||
defer db.Close() | ||
var result int | ||
if errScan := row.Scan(&result); errScan != nil { | ||
t.Fatalf("scan: %s\n", errScan.Error()) | ||
} | ||
return result | ||
} | ||
|
||
func getCountOfApplicationNameTest(t *testing.T) int { | ||
db, err := sql.Open(`postgres`, `host=/tmp dbname=gatherer user=gatherer port=5432`) | ||
if err != nil { | ||
t.Fatalf("open: %s\n", err.Error()) | ||
} | ||
row := db.QueryRow(`select count(*) from pg_stat_activity where application_name = 'test'`) | ||
defer db.Close() | ||
var result int | ||
if errScan := row.Scan(&result); errScan != nil { | ||
t.Fatalf("scan: %s\n", errScan.Error()) | ||
} | ||
return result | ||
} |