fix: timeout issue of gitea sdk client (#422)

closes #420

Reviewed-on: https://codeberg.org/Codeberg/pages-server/pulls/422
Co-authored-by: crapStone <me@crapstone.dev>
Co-committed-by: crapStone <me@crapstone.dev>
This commit is contained in:
crapStone 2025-01-17 19:49:16 +00:00 committed by crapStone
parent cce656ec4e
commit a77e2d9440

View file

@ -49,6 +49,7 @@ const (
type Client struct { type Client struct {
sdkClient *gitea.Client sdkClient *gitea.Client
sdkFileClient *gitea.Client
responseCache cache.ICache responseCache cache.ICache
giteaRoot string giteaRoot string
@ -68,8 +69,6 @@ func NewClient(cfg config.ForgeConfig, respCache cache.ICache) (*Client, error)
} }
giteaRoot := strings.TrimSuffix(rootURL.String(), "/") giteaRoot := strings.TrimSuffix(rootURL.String(), "/")
stdClient := http.Client{Timeout: 10 * time.Second}
forbiddenMimeTypes := make(map[string]bool, len(cfg.ForbiddenMimeTypes)) forbiddenMimeTypes := make(map[string]bool, len(cfg.ForbiddenMimeTypes))
for _, mimeType := range cfg.ForbiddenMimeTypes { for _, mimeType := range cfg.ForbiddenMimeTypes {
forbiddenMimeTypes[mimeType] = true forbiddenMimeTypes[mimeType] = true
@ -80,15 +79,26 @@ func NewClient(cfg config.ForgeConfig, respCache cache.ICache) (*Client, error)
defaultMimeType = "application/octet-stream" defaultMimeType = "application/octet-stream"
} }
sdk, err := gitea.NewClient( sdkClient, err := gitea.NewClient(
giteaRoot, 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.SetToken(cfg.Token),
gitea.SetUserAgent("pages-server/"+version.Version), gitea.SetUserAgent("pages-server/"+version.Version),
) )
return &Client{ return &Client{
sdkClient: sdk, sdkClient: sdkClient,
sdkFileClient: sdkFileClient,
responseCache: respCache, responseCache: respCache,
giteaRoot: giteaRoot, giteaRoot: giteaRoot,
@ -154,7 +164,7 @@ func (client *Client) ServeRawContent(ctx *context.Context, targetOwner, targetR
} }
log.Trace().Msg("file not in cache") log.Trace().Msg("file not in cache")
// not in cache, open reader via gitea api // 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 { if resp != nil {
switch resp.StatusCode { switch resp.StatusCode {
case http.StatusOK: case http.StatusOK: