fix timeout issue of gitea sdk client

This commit is contained in:
crapStone 2025-01-17 20:19:41 +01:00
parent cce656ec4e
commit 2cd77be1f2
No known key found for this signature in database
GPG key ID: 22D4BF0CF7CC29C8

View file

@ -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,27 @@ 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 +165,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: