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 {
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
|
||||
"gitea.d-ma.be/mathias/gitea-mcp/internal/gitea"
|
||||
@@ -45,3 +46,23 @@ func TestListRepos(t *testing.T) {
|
||||
assert.Equal(t, "mathias/infra", repos[0].FullName)
|
||||
assert.Equal(t, "main", repos[0].DefaultBranch)
|
||||
}
|
||||
|
||||
func TestDefaultBranchCachesAcrossCalls(t *testing.T) {
|
||||
var hits int32
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||
atomic.AddInt32(&hits, 1)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write([]byte(`{"name":"infra","full_name":"o/infra","default_branch":"trunk"}`))
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
c := gitea.NewClient(srv.URL, "tok")
|
||||
|
||||
for i := 0; i < 5; i++ {
|
||||
b, err := c.DefaultBranch(context.Background(), "o", "infra")
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "trunk", b)
|
||||
}
|
||||
|
||||
assert.Equal(t, int32(1), atomic.LoadInt32(&hits), "5 calls should cause exactly 1 server hit due to cache")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user