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:
Mathias Bergqvist
2026-05-04 23:06:06 +02:00
parent fb473262ba
commit 4274b48ea5
7 changed files with 54 additions and 12 deletions

View File

@@ -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 {

View File

@@ -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")
}

View File

@@ -57,11 +57,11 @@ func (t *FileRead) Call(ctx context.Context, raw json.RawMessage) (json.RawMessa
ref := args.Ref
if ref == "" {
repo, err := t.c.GetRepo(ctx, args.Owner, args.Name)
var err error
ref, err = t.c.DefaultBranch(ctx, args.Owner, args.Name)
if err != nil {
return nil, err
}
ref = repo.DefaultBranch
}
fc, err := t.c.GetFileContents(ctx, args.Owner, args.Name, args.Path, ref)

View File

@@ -75,11 +75,11 @@ func (t *FileWriteBranch) Call(ctx context.Context, raw json.RawMessage) (json.R
if !exists {
base := args.Base
if base == "" {
repo, err := t.c.GetRepo(ctx, args.Owner, args.Name)
var err error
base, err = t.c.DefaultBranch(ctx, args.Owner, args.Name)
if err != nil {
return nil, err
}
base = repo.DefaultBranch
}
if err := t.c.CreateBranch(ctx, args.Owner, args.Name, args.Branch, base); err != nil {
return nil, err

View File

@@ -61,11 +61,11 @@ func (t *WorkflowRunTrigger) Call(ctx context.Context, raw json.RawMessage) (jso
ref := args.Ref
if ref == "" {
repo, err := t.c.GetRepo(ctx, args.Owner, args.Name)
var err error
ref, err = t.c.DefaultBranch(ctx, args.Owner, args.Name)
if err != nil {
return nil, err
}
ref = repo.DefaultBranch
}
result, err := t.c.DispatchWorkflow(ctx, args.Owner, args.Name, args.Workflow, gitea.DispatchWorkflowArgs{