Skip to content

Commit

Permalink
explicit timeout when testing s3 endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
odrling committed Nov 10, 2024
1 parent 2b6336c commit 7fb22c3
Showing 1 changed file with 23 additions and 10 deletions.
33 changes: 23 additions & 10 deletions server/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,22 +58,35 @@ func pickBestClient(ctx context.Context) {

var best_client *TestedClient = nil

c := make(chan TestedClient, len(S3_CLIENTS))

var err error
for _, client := range S3_CLIENTS {
begin_time := time.Now()
_, err = client.ListBuckets(ctx)
if err != nil {
continue
}
tested := TestedClient{client, time.Since(begin_time).Nanoseconds()}
go func(client *minio.Client) {
begin_time := time.Now()
_, err = client.ListBuckets(ctx)
if err == nil {
tested := TestedClient{client, time.Since(begin_time).Nanoseconds()}
c <- tested
}
}(client)
}
timeout := 5 * time.Second

select {
case tested := <-c:
if best_client == nil || tested.ListLatency < best_client.ListLatency {
best_client = &tested
timeout = 500 * time.Millisecond
}
case <-time.After(timeout):
if best_client == nil {
if err == nil {
panic("timeout")
} else {
panic(err)
}
}
}

if best_client == nil {
panic(err)
}

S3_BEST_CLIENT = best_client.Client
Expand Down

0 comments on commit 7fb22c3

Please sign in to comment.