diff options
author | Martin Polden <mpolden@mpolden.no> | 2023-04-27 09:23:25 +0200 |
---|---|---|
committer | Martin Polden <mpolden@mpolden.no> | 2023-04-27 10:14:34 +0200 |
commit | 2ca44073203f87b8258a632f44567710d75be4f1 (patch) | |
tree | b3ba4dc05059716432c48764f297c95a1af9e08d /client | |
parent | b572b1c2c7726d86b18554c6adfad51924e6945d (diff) |
Parse base URL once
Diffstat (limited to 'client')
-rw-r--r-- | client/go/internal/cli/cmd/feed.go | 5 | ||||
-rw-r--r-- | client/go/internal/vespa/document/http.go | 26 | ||||
-rw-r--r-- | client/go/internal/vespa/document/http_test.go | 15 |
3 files changed, 23 insertions, 23 deletions
diff --git a/client/go/internal/cli/cmd/feed.go b/client/go/internal/cli/cmd/feed.go index d50a12d8884..7cedc39bad8 100644 --- a/client/go/internal/cli/cmd/feed.go +++ b/client/go/internal/cli/cmd/feed.go @@ -139,7 +139,7 @@ func feed(files []string, options feedOptions, cli *CLI) error { if err != nil { return err } - client := document.NewClient(document.ClientOptions{ + client, err := document.NewClient(document.ClientOptions{ Compression: compression, Timeout: time.Duration(options.timeoutSecs) * time.Second, Route: options.route, @@ -147,6 +147,9 @@ func feed(files []string, options feedOptions, cli *CLI) error { BaseURL: service.BaseURL, NowFunc: cli.now, }, clients) + if err != nil { + return err + } throttler := document.NewThrottler(options.connections) circuitBreaker := document.NewCircuitBreaker(10*time.Second, time.Duration(options.doomSecs)*time.Second) dispatcher := document.NewDispatcher(client, throttler, circuitBreaker, cli.Stderr, options.verbose) diff --git a/client/go/internal/vespa/document/http.go b/client/go/internal/vespa/document/http.go index 8170dc5d33a..420d369e1b0 100644 --- a/client/go/internal/vespa/document/http.go +++ b/client/go/internal/vespa/document/http.go @@ -28,6 +28,7 @@ const ( // Client represents a HTTP client for the /document/v1/ API. type Client struct { + baseURL *url.URL options ClientOptions httpClients []countingHTTPClient now func() time.Time @@ -57,9 +58,13 @@ func (c *countingHTTPClient) Do(req *http.Request, timeout time.Duration) (*http return c.client.Do(req, timeout) } -func NewClient(options ClientOptions, httpClients []util.HTTPClient) *Client { +func NewClient(options ClientOptions, httpClients []util.HTTPClient) (*Client, error) { if len(httpClients) < 1 { - panic("need at least one HTTP client") + return nil, fmt.Errorf("need at least one HTTP client") + } + u, err := url.Parse(options.BaseURL) + if err != nil { + return nil, fmt.Errorf("invalid base url: %w", err) } countingClients := make([]countingHTTPClient, 0, len(httpClients)) for _, client := range httpClients { @@ -70,12 +75,13 @@ func NewClient(options ClientOptions, httpClients []util.HTTPClient) *Client { nowFunc = time.Now } c := &Client{ + baseURL: u, options: options, httpClients: countingClients, now: nowFunc, } c.gzippers.New = func() any { return gzip.NewWriter(io.Discard) } - return c + return c, nil } func (c *Client) queryParams() url.Values { @@ -112,11 +118,7 @@ func urlPath(id Id) string { return sb.String() } -func (c *Client) feedURL(d Document, queryParams url.Values) (string, *url.URL, error) { - u, err := url.Parse(c.options.BaseURL) - if err != nil { - return "", nil, fmt.Errorf("invalid base url: %w", err) - } +func (c *Client) feedURL(d Document, queryParams url.Values) (string, *url.URL) { httpMethod := "" switch d.Operation { case OperationPut: @@ -132,9 +134,10 @@ func (c *Client) feedURL(d Document, queryParams url.Values) (string, *url.URL, if d.Create { queryParams.Set("create", "true") } + u := *c.baseURL u.Path = urlPath(d.Id) u.RawQuery = queryParams.Encode() - return httpMethod, u, nil + return httpMethod, &u } func (c *Client) leastBusyClient() *countingHTTPClient { @@ -194,10 +197,7 @@ func (c *Client) createRequest(method, url string, body []byte) (*http.Request, func (c *Client) Send(document Document) Result { start := c.now() result := Result{Id: document.Id, Stats: Stats{Requests: 1}} - method, url, err := c.feedURL(document, c.queryParams()) - if err != nil { - return resultWithErr(result, err) - } + method, url := c.feedURL(document, c.queryParams()) req, err := c.createRequest(method, url.String(), document.Body) if err != nil { return resultWithErr(result, err) diff --git a/client/go/internal/vespa/document/http_test.go b/client/go/internal/vespa/document/http_test.go index f67368b5128..3694c7599be 100644 --- a/client/go/internal/vespa/document/http_test.go +++ b/client/go/internal/vespa/document/http_test.go @@ -38,7 +38,7 @@ func TestLeastBusyClient(t *testing.T) { for i := 0; i < 4; i++ { httpClients = append(httpClients, &mockHTTPClient{i, &httpClient}) } - client := NewClient(ClientOptions{}, httpClients) + client, _ := NewClient(ClientOptions{}, httpClients) client.httpClients[0].addInflight(1) client.httpClients[1].addInflight(1) assertLeastBusy(t, 2, client) @@ -65,7 +65,7 @@ func TestClientSend(t *testing.T) { {Create: true, Id: mustParseId("id:ns:type::doc3"), Operation: OperationUpdate, Body: []byte(`{"fields":{"baz": "789"}}`)}, } httpClient := mock.HTTPClient{} - client := NewClient(ClientOptions{ + client, _ := NewClient(ClientOptions{ BaseURL: "https://example.com:1337", Timeout: time.Duration(5 * time.Second), }, []util.HTTPClient{&httpClient}) @@ -144,7 +144,7 @@ func TestClientSend(t *testing.T) { func TestClientSendCompressed(t *testing.T) { httpClient := mock.HTTPClient{} - client := NewClient(ClientOptions{ + client, _ := NewClient(ClientOptions{ BaseURL: "https://example.com:1337", Timeout: time.Duration(5 * time.Second), }, []util.HTTPClient{&httpClient}) @@ -278,16 +278,13 @@ func TestClientFeedURL(t *testing.T) { }, } httpClient := mock.HTTPClient{} - client := NewClient(ClientOptions{ + client, _ := NewClient(ClientOptions{ BaseURL: "https://example.com", }, []util.HTTPClient{&httpClient}) for i, tt := range tests { moreParams := url.Values{} moreParams.Set("foo", "ba/r") - method, u, err := client.feedURL(tt.in, moreParams) - if err != nil { - t.Errorf("#%d: got unexpected error = %s, want none", i, err) - } + method, u := client.feedURL(tt.in, moreParams) if u.String() != tt.url || method != tt.method { t.Errorf("#%d: URL() = (%s, %s), want (%s, %s)", i, method, u.String(), tt.method, tt.url) } @@ -296,7 +293,7 @@ func TestClientFeedURL(t *testing.T) { func benchmarkClientSend(b *testing.B, compression Compression, document Document) { httpClient := mock.HTTPClient{} - client := NewClient(ClientOptions{ + client, _ := NewClient(ClientOptions{ Compression: compression, BaseURL: "https://example.com:1337", Timeout: time.Duration(5 * time.Second), |