feat(gitea): default-branch lru cache
Shared LRU avoids repeated Gitea calls for default-branch resolution; the simple stdlib map alternative would race on concurrent access without a mutex per entry, which is more code than the LRU.
This commit is contained in:
@@ -6,22 +6,40 @@ import (
|
||||
"io"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/hashicorp/golang-lru/v2/expirable"
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
baseURL string
|
||||
token string
|
||||
hc *http.Client
|
||||
baseURL string
|
||||
token string
|
||||
hc *http.Client
|
||||
branchCache *expirable.LRU[string, string]
|
||||
}
|
||||
|
||||
func NewClient(baseURL, token string) *Client {
|
||||
return &Client{
|
||||
baseURL: baseURL,
|
||||
token: token,
|
||||
hc: &http.Client{Timeout: 30 * time.Second},
|
||||
baseURL: baseURL,
|
||||
token: token,
|
||||
hc: &http.Client{Timeout: 30 * time.Second},
|
||||
branchCache: expirable.NewLRU[string, string](64, nil, 60*time.Second),
|
||||
}
|
||||
}
|
||||
|
||||
// DefaultBranch returns the default branch for a repo. Cached for 60s.
|
||||
func (c *Client) DefaultBranch(ctx context.Context, owner, name string) (string, error) {
|
||||
key := owner + "/" + name
|
||||
if v, ok := c.branchCache.Get(key); ok {
|
||||
return v, nil
|
||||
}
|
||||
repo, err := c.GetRepo(ctx, owner, name)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
c.branchCache.Add(key, repo.DefaultBranch)
|
||||
return repo.DefaultBranch, nil
|
||||
}
|
||||
|
||||
func (c *Client) doOnce(ctx context.Context, method, path string, body []byte) ([]byte, int, error) {
|
||||
var reader io.Reader
|
||||
if body != nil {
|
||||
|
||||
Reference in New Issue
Block a user