@@ -5,7 +5,9 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
|
||||
"go.uber.org/zap"
|
||||
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -18,14 +20,16 @@ import (
|
||||
|
||||
type (
|
||||
Client struct {
|
||||
logger *zap.Logger
|
||||
url string
|
||||
httpClient *http.Client
|
||||
}
|
||||
)
|
||||
|
||||
func MakeClient(fetcherUrl string) *Client {
|
||||
func MakeClient(logger *zap.Logger, fetcherUrl string) *Client {
|
||||
return &Client{
|
||||
url: strings.TrimSuffix(fetcherUrl, "/"),
|
||||
logger: logger.Named("fetcher_client"),
|
||||
url: strings.TrimSuffix(fetcherUrl, "/"),
|
||||
httpClient: &http.Client{
|
||||
Transport: &ochttp.Transport{},
|
||||
},
|
||||
@@ -45,17 +49,17 @@ func (c *Client) getUploadUrl() string {
|
||||
}
|
||||
|
||||
func (c *Client) Specialize(ctx context.Context, req *fission.FunctionSpecializeRequest) error {
|
||||
_, err := sendRequest(ctx, c.httpClient, req, c.getSpecializeUrl())
|
||||
_, err := sendRequest(c.logger, ctx, c.httpClient, req, c.getSpecializeUrl())
|
||||
return err
|
||||
}
|
||||
|
||||
func (c *Client) Fetch(ctx context.Context, fr *fission.FunctionFetchRequest) error {
|
||||
_, err := sendRequest(ctx, c.httpClient, fr, c.getFetchUrl())
|
||||
_, err := sendRequest(c.logger, ctx, c.httpClient, fr, c.getFetchUrl())
|
||||
return err
|
||||
}
|
||||
|
||||
func (c *Client) Upload(ctx context.Context, fr *fission.ArchiveUploadRequest) (*fission.ArchiveUploadResponse, error) {
|
||||
body, err := sendRequest(ctx, c.httpClient, fr, c.getUploadUrl())
|
||||
body, err := sendRequest(c.logger, ctx, c.httpClient, fr, c.getUploadUrl())
|
||||
|
||||
uploadResp := fission.ArchiveUploadResponse{}
|
||||
err = json.Unmarshal(body, &uploadResp)
|
||||
@@ -66,7 +70,7 @@ func (c *Client) Upload(ctx context.Context, fr *fission.ArchiveUploadRequest) (
|
||||
return &uploadResp, nil
|
||||
}
|
||||
|
||||
func sendRequest(ctx context.Context, httpClient *http.Client, req interface{}, url string) ([]byte, error) {
|
||||
func sendRequest(logger *zap.Logger, ctx context.Context, httpClient *http.Client, req interface{}, url string) ([]byte, error) {
|
||||
body, err := json.Marshal(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -82,7 +86,7 @@ func sendRequest(ctx context.Context, httpClient *http.Client, req interface{},
|
||||
if resp.StatusCode == 200 {
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
log.Printf("Error reading response body: %v", err)
|
||||
logger.Error("error reading response body", zap.Error(err))
|
||||
}
|
||||
resp.Body.Close()
|
||||
return body, err
|
||||
@@ -92,7 +96,7 @@ func sendRequest(ctx context.Context, httpClient *http.Client, req interface{},
|
||||
|
||||
if i < maxRetries-1 {
|
||||
time.Sleep(50 * time.Duration(2*i) * time.Millisecond)
|
||||
log.Printf("Error specializing/fetching/uploading package (%v) with url %v, retrying", err, url)
|
||||
logger.Error("error specializing/fetching/uploading package, retrying", zap.Error(err), zap.String("url", url))
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user