From a77e2d944065bfdb25bff9353bdb029db37e81c8 Mon Sep 17 00:00:00 2001 From: crapStone Date: Fri, 17 Jan 2025 19:49:16 +0000 Subject: [PATCH] fix: timeout issue of gitea sdk client (#422) closes #420 Reviewed-on: https://codeberg.org/Codeberg/pages-server/pulls/422 Co-authored-by: crapStone Co-committed-by: crapStone --- server/gitea/client.go | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/server/gitea/client.go b/server/gitea/client.go index 0cf6e6c..5633bf2 100644 --- a/server/gitea/client.go +++ b/server/gitea/client.go @@ -49,6 +49,7 @@ const ( type Client struct { sdkClient *gitea.Client + sdkFileClient *gitea.Client responseCache cache.ICache giteaRoot string @@ -68,8 +69,6 @@ func NewClient(cfg config.ForgeConfig, respCache cache.ICache) (*Client, error) } giteaRoot := strings.TrimSuffix(rootURL.String(), "/") - stdClient := http.Client{Timeout: 10 * time.Second} - forbiddenMimeTypes := make(map[string]bool, len(cfg.ForbiddenMimeTypes)) for _, mimeType := range cfg.ForbiddenMimeTypes { forbiddenMimeTypes[mimeType] = true @@ -80,15 +79,26 @@ func NewClient(cfg config.ForgeConfig, respCache cache.ICache) (*Client, error) defaultMimeType = "application/octet-stream" } - sdk, err := gitea.NewClient( + sdkClient, err := gitea.NewClient( giteaRoot, - gitea.SetHTTPClient(&stdClient), + gitea.SetHTTPClient(&http.Client{Timeout: 10 * time.Second}), + gitea.SetToken(cfg.Token), + gitea.SetUserAgent("pages-server/"+version.Version), + ) + if err != nil { + return nil, err + } + + sdkFileClient, err := gitea.NewClient( + giteaRoot, + gitea.SetHTTPClient(&http.Client{Timeout: 1 * time.Hour}), gitea.SetToken(cfg.Token), gitea.SetUserAgent("pages-server/"+version.Version), ) return &Client{ - sdkClient: sdk, + sdkClient: sdkClient, + sdkFileClient: sdkFileClient, responseCache: respCache, giteaRoot: giteaRoot, @@ -154,7 +164,7 @@ func (client *Client) ServeRawContent(ctx *context.Context, targetOwner, targetR } log.Trace().Msg("file not in cache") // not in cache, open reader via gitea api - reader, resp, err := client.sdkClient.GetFileReader(targetOwner, targetRepo, ref, resource, client.supportLFS) + reader, resp, err := client.sdkFileClient.GetFileReader(targetOwner, targetRepo, ref, resource, client.supportLFS) if resp != nil { switch resp.StatusCode { case http.StatusOK: