-
Notifications
You must be signed in to change notification settings - Fork 167
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactoring and add connection retry
- Loading branch information
1 parent
a7147fd
commit 6a2229e
Showing
2 changed files
with
33 additions
and
22 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
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 |
---|---|---|
|
@@ -11,22 +11,26 @@ import ( | |
_ "github.com/lib/pq" | ||
) | ||
|
||
const clientsCount = 1000 | ||
const connectTimeoutSec = 2 | ||
|
||
func connect(successConnect *int, i int, wg *sync.WaitGroup) { | ||
defer wg.Done() | ||
const ( | ||
hostname = "127.0.0.1" | ||
odyPort = 6432 | ||
databaseName = "ipb_db" | ||
clientsCount = 1000 | ||
connectTimeoutSec = 2 | ||
connectRetryCount = 3 | ||
) | ||
|
||
userName := fmt.Sprintf("inverse_priorities_benchmark_%d", i) | ||
pgConString := fmt.Sprintf("postgres://%[email protected]:6432/ipb_db?sslmode=require&connect_timeout=%d", | ||
userName, connectTimeoutSec) | ||
func connectRetry(clientNumber int) int { | ||
userName := fmt.Sprintf("inverse_priorities_benchmark_%d", clientNumber) | ||
pgConString := fmt.Sprintf("postgres://%s@%s:%d/%s?sslmode=require&connect_timeout=%d", | ||
userName, hostname, odyPort, databaseName, connectTimeoutSec) | ||
config, err := pgx.ParseConfig(pgConString) | ||
if err != nil { | ||
fmt.Println("DCN error:", err.Error()) | ||
return | ||
return 1 | ||
} | ||
|
||
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) | ||
ctx, cancel := context.WithTimeout(context.Background(), connectTimeoutSec*time.Second) | ||
defer cancel() | ||
|
||
conn, err := pgx.ConnectConfig(ctx, config) | ||
|
@@ -36,25 +40,34 @@ func connect(successConnect *int, i int, wg *sync.WaitGroup) { | |
} else { | ||
fmt.Println("Connect error:", err.Error()) | ||
} | ||
return | ||
return 1 | ||
} | ||
|
||
defer conn.Close(ctx) | ||
*successConnect++ | ||
return 0 | ||
} | ||
|
||
func connect(successConnect *int, clientNumber int, wg *sync.WaitGroup) { | ||
defer wg.Done() | ||
for i := 0; i < connectRetryCount; i++ { | ||
statusCode := connectRetry(clientNumber) | ||
if statusCode == 0 { | ||
*successConnect++ | ||
break | ||
} | ||
} | ||
} | ||
|
||
func main() { | ||
fmt.Printf("inverse_prioriteis_benchmark_start\n") | ||
successConnect := 0 | ||
connectCount := clientsCount | ||
var wg sync.WaitGroup | ||
wg.Add(connectCount) | ||
for i := 0; i < connectCount; i++ { | ||
client_number := i % clientsCount | ||
fmt.Printf("inverse_prioriteis_benchmark_connect_%d\n", client_number) | ||
go connect(&successConnect, client_number, &wg) | ||
wg.Add(clientsCount) | ||
for i := 0; i < clientsCount; i++ { | ||
fmt.Printf("inverse_prioriteis_benchmark_connect_%d\n", i) | ||
go connect(&successConnect, i, &wg) | ||
} | ||
wg.Wait() | ||
resultPercent := (float64(successConnect) / float64(connectCount)) * 100 | ||
fmt.Printf("inverse_prioriteis_benchmark_result: %d success connections out of %d (%.2f %%)\n", successConnect, connectCount, resultPercent) | ||
resultPercent := (float64(successConnect) / float64(clientsCount)) * 100 | ||
fmt.Printf("inverse_prioriteis_benchmark_result: %d success connections out of %d (%.2f %%)\n", successConnect, clientsCount, resultPercent) | ||
} |