diff --git a/server/s3.go b/server/s3.go index ae7e1a6..23ff15b 100644 --- a/server/s3.go +++ b/server/s3.go @@ -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