1
0
Fork 0

Add a method option to the service Health Check

This commit is contained in:
Douglas De Toni Machado 2022-08-08 10:22:07 -03:00 committed by GitHub
parent 2a2ea759d1
commit af749f1864
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 93 additions and 8 deletions

View file

@ -8,6 +8,7 @@ import (
"net/http"
"net/url"
"strconv"
"strings"
"sync"
"time"
@ -60,6 +61,7 @@ type Options struct {
Hostname string
Scheme string
Path string
Method string
Port int
FollowRedirects bool
Transport http.RoundTripper
@ -69,7 +71,7 @@ type Options struct {
}
func (opt Options) String() string {
return fmt.Sprintf("[Hostname: %s Headers: %v Path: %s Port: %d Interval: %s Timeout: %s FollowRedirects: %v]", opt.Hostname, opt.Headers, opt.Path, opt.Port, opt.Interval, opt.Timeout, opt.FollowRedirects)
return fmt.Sprintf("[Hostname: %s Headers: %v Path: %s Method: %s Port: %d Interval: %s Timeout: %s FollowRedirects: %v]", opt.Hostname, opt.Headers, opt.Path, opt.Method, opt.Port, opt.Interval, opt.Timeout, opt.FollowRedirects)
}
type backendURL struct {
@ -101,8 +103,8 @@ func (b *BackendConfig) newRequest(serverURL *url.URL) (*http.Request, error) {
return http.NewRequest(http.MethodGet, u.String(), http.NoBody)
}
// this function adds additional http headers and hostname to http.request.
func (b *BackendConfig) addHeadersAndHost(req *http.Request) *http.Request {
// setRequestOptions sets all request options present on the BackendConfig.
func (b *BackendConfig) setRequestOptions(req *http.Request) *http.Request {
if b.Options.Hostname != "" {
req.Host = b.Options.Hostname
}
@ -110,6 +112,11 @@ func (b *BackendConfig) addHeadersAndHost(req *http.Request) *http.Request {
for k, v := range b.Options.Headers {
req.Header.Set(k, v)
}
if b.Options.Method != "" {
req.Method = strings.ToUpper(b.Options.Method)
}
return req
}
@ -246,7 +253,7 @@ func checkHealth(serverURL *url.URL, backend *BackendConfig) error {
return fmt.Errorf("failed to create HTTP request: %w", err)
}
req = backend.addHeadersAndHost(req)
req = backend.setRequestOptions(req)
client := http.Client{
Timeout: backend.Options.Timeout,