generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #187 from aojea/race_http
Improve startup latency
- Loading branch information
Showing
4 changed files
with
168 additions
and
59 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
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 |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package controller | ||
|
||
import ( | ||
"context" | ||
"crypto/tls" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"sync" | ||
"time" | ||
|
||
"k8s.io/klog/v2" | ||
) | ||
|
||
func probeHTTP(ctx context.Context, address string) bool { | ||
klog.Infof("probe HTTP address %s", address) | ||
httpClient := &http.Client{ | ||
Timeout: 2 * time.Second, | ||
Transport: &http.Transport{ | ||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, | ||
}, | ||
} | ||
req, err := http.NewRequest("GET", address, nil) | ||
if err != nil { | ||
return false | ||
} | ||
req = req.WithContext(ctx) | ||
resp, err := httpClient.Do(req) | ||
if err != nil { | ||
klog.Infof("Failed to connect to HTTP address %s: %v", address, err) | ||
return false | ||
} | ||
defer resp.Body.Close() | ||
// drain the body | ||
io.ReadAll(resp.Body) // nolint:errcheck | ||
// we only want to verify connectivity so don't need to check the http status code | ||
// as the apiserver may not be ready | ||
return true | ||
} | ||
|
||
// firstSuccessfulProbe probes the given addresses in parallel and returns the first address to succeed, cancelling the other probes. | ||
func firstSuccessfulProbe(ctx context.Context, addresses []string) (string, error) { | ||
var wg sync.WaitGroup | ||
resultChan := make(chan string, 1) | ||
|
||
ctx, cancel := context.WithCancel(ctx) | ||
defer cancel() | ||
|
||
for _, addr := range addresses { | ||
wg.Add(1) | ||
go func(address string) { | ||
defer wg.Done() | ||
if probeHTTP(ctx, address) { | ||
select { | ||
case resultChan <- address: | ||
default: | ||
} | ||
cancel() | ||
} | ||
}(addr) | ||
} | ||
|
||
go func() { | ||
wg.Wait() | ||
close(resultChan) | ||
}() | ||
|
||
select { | ||
case result := <-resultChan: | ||
return result, nil | ||
case <-ctx.Done(): | ||
return "", fmt.Errorf("no address succeeded") | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package controller | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func Test_firstSuccessfulProbe(t *testing.T) { | ||
reqCh := make(chan struct{}) | ||
ts := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
t.Logf("received connection ") | ||
close(reqCh) | ||
})) | ||
ts.EnableHTTP2 = true | ||
ts.StartTLS() | ||
defer ts.Close() | ||
// use an address that is not likely to exist to avoid flakes | ||
addresses := []string{"https://127.0.1.201:12349", ts.URL} | ||
got, err := firstSuccessfulProbe(context.Background(), addresses) | ||
if err != nil { | ||
t.Errorf("firstSuccessfulProbe() error = %v", err) | ||
return | ||
} | ||
if got != ts.URL { | ||
t.Errorf("firstSuccessfulProbe() = %v, want %v", got, ts.URL) | ||
} | ||
|
||
select { | ||
case <-reqCh: | ||
case <-time.After(10 * time.Second): | ||
t.Fatalf("test timed out, no request received") | ||
} | ||
|
||
} |