@@ -3,10 +3,8 @@ package postgres
33import (
44 "database/sql"
55 "fmt"
6- "net"
76 "os"
87 "strconv"
9- "sync"
108 "time"
119
1210 "github.com/cenkalti/backoff/v4"
@@ -18,10 +16,6 @@ import (
1816 "github.com/coder/coder/cryptorand"
1917)
2018
21- // Required to prevent port collision during container creation.
22- // Super unlikely, but it happened. See: https://github.com/coder/coder/runs/5375197003
23- var openPortMutex sync.Mutex
24-
2519// Open creates a new PostgreSQL database instance. With DB_FROM environment variable set, it clones a database
2620// from the provided template. With the environment variable unset, it creates a new Docker container running postgres.
2721func Open () (string , func (), error ) {
@@ -68,17 +62,6 @@ func OpenContainerized(port int) (string, func(), error) {
6862 return "" , nil , xerrors .Errorf ("create tempdir: %w" , err )
6963 }
7064
71- openPortMutex .Lock ()
72- if port == 0 {
73- // Pick an explicit port on the host to connect to 5432.
74- // This is necessary so we can configure the port to only use ipv4.
75- port , err = getFreePort ()
76- if err != nil {
77- openPortMutex .Unlock ()
78- return "" , nil , xerrors .Errorf ("get free port: %w" , err )
79- }
80- }
81-
8265 resource , err := pool .RunWithOptions (& dockertest.RunOptions {
8366 Repository : "postgres" ,
8467 Tag : "13" ,
@@ -114,10 +97,8 @@ func OpenContainerized(port int) (string, func(), error) {
11497 config .RestartPolicy = docker.RestartPolicy {Name : "no" }
11598 })
11699 if err != nil {
117- openPortMutex .Unlock ()
118100 return "" , nil , xerrors .Errorf ("could not start resource: %w" , err )
119101 }
120- openPortMutex .Unlock ()
121102
122103 hostAndPort := resource .GetHostPort ("5432/tcp" )
123104 dbURL := fmt .Sprintf ("postgres://postgres:postgres@%s/postgres?sslmode=disable" , hostAndPort )
@@ -166,18 +147,3 @@ func OpenContainerized(port int) (string, func(), error) {
166147 _ = os .RemoveAll (tempDir )
167148 }, nil
168149}
169-
170- // getFreePort asks the kernel for a free open port that is ready to use.
171- func getFreePort () (port int , err error ) {
172- // Binding to port 0 tells the OS to grab a port for us:
173- // https://stackoverflow.com/questions/1365265/on-localhost-how-do-i-pick-a-free-port-number
174- listener , err := net .Listen ("tcp" , "localhost:0" )
175- if err != nil {
176- return 0 , err
177- }
178-
179- defer listener .Close ()
180- // This is always a *net.TCPAddr.
181- // nolint:forcetypeassert
182- return listener .Addr ().(* net.TCPAddr ).Port , nil
183- }
0 commit comments