diff options
author | Igor Drozdov <idrozdov@gitlab.com> | 2023-02-13 14:34:03 +0100 |
---|---|---|
committer | Igor Drozdov <idrozdov@gitlab.com> | 2023-03-03 07:18:39 +0100 |
commit | 83a4e8e542e9f929e1c22b235b883ee67187c4c6 (patch) | |
tree | 2f138cd934517f0280f0da148ede78720d9ab765 /internal/gitlabnet/git/client.go | |
parent | d893886d53c3038af84414589459d273609b2243 (diff) | |
download | gitlab-shell-83a4e8e542e9f929e1c22b235b883ee67187c4c6.tar.gz |
Perform HTTP request to primary on Geo push
Currently, we perform a request to Gitlab Rails that proxies
the request to primary
However, it causes timeouts on big pushes and consumes large
amount of memory. We can perform an HTTP request directly
from Gitlab Shell instead and stream the response to the user
Diffstat (limited to 'internal/gitlabnet/git/client.go')
-rw-r--r-- | internal/gitlabnet/git/client.go | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/internal/gitlabnet/git/client.go b/internal/gitlabnet/git/client.go new file mode 100644 index 0000000..db71e3f --- /dev/null +++ b/internal/gitlabnet/git/client.go @@ -0,0 +1,56 @@ +package git + +import ( + "context" + "fmt" + "io" + "net/http" + + "gitlab.com/gitlab-org/gitlab-shell/v14/client" + "gitlab.com/gitlab-org/gitlab-shell/v14/internal/config" + "gitlab.com/gitlab-org/gitlab-shell/v14/internal/gitlabnet" +) + +type Client struct { + url string + headers map[string]string + client *client.GitlabNetClient +} + +func NewClient(cfg *config.Config, url string, headers map[string]string) (*Client, error) { + client, err := gitlabnet.GetClient(cfg) + if err != nil { + return nil, fmt.Errorf("Error creating http client: %v", err) + } + + return &Client{client: client, headers: headers, url: url}, nil +} + +func (c *Client) InfoRefs(ctx context.Context, service string) (*http.Response, error) { + request, err := http.NewRequestWithContext(ctx, http.MethodGet, c.url+"/info/refs?service="+service, nil) + if err != nil { + return nil, err + } + + return c.do(request) +} + +func (c *Client) ReceivePack(ctx context.Context, body io.Reader) (*http.Response, error) { + request, err := http.NewRequestWithContext(ctx, http.MethodPost, c.url+"/git-receive-pack", body) + if err != nil { + return nil, err + } + request.Header.Add("Content-Type", "application/x-git-receive-pack-request") + request.Header.Add("Accept", "application/x-git-receive-pack-result") + + return c.do(request) +} + +func (c *Client) do(request *http.Request) (*http.Response, error) { + + for k, v := range c.headers { + request.Header.Add(k, v) + } + + return c.client.Do(request) +} |