Skip to content

Commit

Permalink
fix: ping with context
Browse files Browse the repository at this point in the history
Signed-off-by: Young Xu <[email protected]>
  • Loading branch information
xuthus5 committed Jul 28, 2024
1 parent de8cc00 commit 9cd436a
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 13 deletions.
42 changes: 34 additions & 8 deletions opengemini/http.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package opengemini

import (
"context"
"encoding/base64"
"errors"
"io"
Expand Down Expand Up @@ -43,10 +44,14 @@ func (c *client) executeHttpGetByIdx(idx int, urlPath string, details requestDet
}

func (c *client) executeHttpRequestByIdx(idx int, method, urlPath string, details requestDetails) (*http.Response, error) {
return c.executeHttpRequestByIdxWithContext(nil, idx, method, urlPath, details)
}

func (c *client) executeHttpRequestByIdxWithContext(ctx context.Context, idx int, method, urlPath string, details requestDetails) (*http.Response, error) {
if idx >= len(c.endpoints) || idx < 0 {
return nil, errors.New("index out of range")
}
return c.executeHttpRequestInner(method, c.endpoints[idx].url, urlPath, details)
return c.executeHttpRequestInner(ctx, method, c.endpoints[idx].url, urlPath, details)
}

func (c *client) executeHttpGet(urlPath string, details requestDetails) (*http.Response, error) {
Expand All @@ -62,10 +67,22 @@ func (c *client) executeHttpRequest(method, urlPath string, details requestDetai
if err != nil {
return nil, err
}
return c.executeHttpRequestInner(method, serverUrl, urlPath, details)
return c.executeHttpRequestInner(nil, method, serverUrl, urlPath, details)
}

func (c *client) executeHttpRequestInner(method, serverUrl, urlPath string, details requestDetails) (*http.Response, error) {
// executeHttpRequestInner executes an HTTP request with the given method, server URL, URL path, and request details.
//
// Parameters:
// - ctx: The context.Context to associate with the request, if ctx is nil, request is created without a context.
// - method: The HTTP method to use for the request.
// - serverUrl: The server URL to use for the request.
// - urlPath: The URL path to use for the request.
// - details: The request details to use for the request.
//
// Returns:
// - *http.Response: The HTTP response from the request.
// - error: An error that occurred during the request.
func (c *client) executeHttpRequestInner(ctx context.Context, method, serverUrl, urlPath string, details requestDetails) (*http.Response, error) {
details.header = c.updateAuthHeader(method, urlPath, details.header)
fullUrl := serverUrl + urlPath
u, err := url.Parse(fullUrl)
Expand All @@ -77,16 +94,25 @@ func (c *client) executeHttpRequestInner(method, serverUrl, urlPath string, deta
u.RawQuery = details.queryValues.Encode()
}

req, err := http.NewRequest(method, u.String(), details.body)
if err != nil {
return nil, err
var request *http.Request

if ctx == nil {
request, err = http.NewRequest(method, u.String(), details.body)
if err != nil {
return nil, err
}
} else {
request, err = http.NewRequestWithContext(ctx, method, u.String(), details.body)
if err != nil {
return nil, err
}
}

for k, values := range details.header {
for _, v := range values {
req.Header.Add(k, v)
request.Header.Add(k, v)
}
}

return c.cli.Do(req)
return c.cli.Do(request)
}
7 changes: 6 additions & 1 deletion opengemini/ping.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
package opengemini

import (
"context"
"errors"
"io"
"net/http"
)

// Ping check that status of cluster.
func (c *client) Ping(idx int) error {
resp, err := c.executeHttpGetByIdx(idx, UrlPing, requestDetails{})
return c.ping(nil, idx)
}

func (c *client) ping(ctx context.Context, idx int) error {
resp, err := c.executeHttpRequestByIdxWithContext(ctx, idx, http.MethodGet, UrlPing, requestDetails{})
if err != nil {
return errors.New("ping request failed, error: " + err.Error())
}
Expand Down
7 changes: 3 additions & 4 deletions opengemini/servers_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@ func (c *client) endpointsCheck(ctx context.Context) {
t.Stop()
return
case <-t.C:
c.checkUpOrDown(ctx)
}

c.checkUpOrDown()
}
}

func (c *client) checkUpOrDown() {
func (c *client) checkUpOrDown(ctx context.Context) {
wg := &sync.WaitGroup{}
for i := 0; i < len(c.endpoints); i++ {
wg.Add(1)
Expand All @@ -38,7 +37,7 @@ func (c *client) checkUpOrDown() {
return
}
}()
err := c.Ping(idx)
err := c.ping(ctx, idx)
c.endpoints[idx].isDown.Store(err != nil)
}(i)
}
Expand Down

0 comments on commit 9cd436a

Please sign in to comment.