Skip to content

Commit

Permalink
refactoring and add connection retry
Browse files Browse the repository at this point in the history
  • Loading branch information
NikitaUnisikhin committed Feb 8, 2024
1 parent a7147fd commit 6a2229e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@ log_format "%p %t %l [%i %s] (%c) %m\n"

log_to_stdout no

log_file "/var/log/odyssey.log"
log_syslog no
log_syslog_ident "odyssey"
log_syslog_facility "daemon"

log_debug yes
log_config yes
log_session yes
log_query no
Expand Down
53 changes: 33 additions & 20 deletions docker/inverse_prioriteis_benchmark/pkg/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}

0 comments on commit 6a2229e

Please sign in to comment.