Skip to content

Commit 17616ef

Browse files
committed
Speed up DoltHub pending PR reads
1 parent 7d1df6d commit 17616ef

4 files changed

Lines changed: 1007 additions & 197 deletions

File tree

internal/hosted/authservice_resolver.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ func (wr *AuthServiceWorkspaceResolver) buildClient(
259259

260260
db := backend.NewRemoteDBWithClient(proxyClient, upOrg, upDB, wl.ForkOrg, wl.ForkDB, mode)
261261
provider := remote.NewDoltHubProviderWithClient(proxyClient)
262+
pendingCache := wr.getOrCreatePendingCache(session.ConnectionID, provider, upOrg, upDB)
262263

263264
branchURL := func(branch string) string {
264265
return fmt.Sprintf("https://www.dolthub.com/repositories/%s/%s/data/%s",
@@ -322,9 +323,9 @@ func (wr *AuthServiceWorkspaceResolver) buildClient(
322323
}
323324
return provider.ClosePR(upOrg, upDB, prID)
324325
},
325-
ListPendingItems: wr.getOrCreatePendingCache(session.ConnectionID, provider, upOrg, upDB).Get,
326+
ListPendingItems: pendingCache.Get,
326327
ListPendingItemsContext: func(ctx context.Context) (map[string][]sdk.PendingItem, error) {
327-
return wr.getOrCreatePendingCache(session.ConnectionID, provider, upOrg, upDB).GetContext(ctx)
328+
return pendingCache.GetContext(ctx)
328329
},
329330
BranchURL: branchURL,
330331
Signing: wl.Signing,

internal/hosted/authservice_resolver_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
11
package hosted
22

33
import (
4+
"context"
5+
"net/http"
46
"testing"
57
"time"
8+
9+
"github.com/gastownhall/wasteland/internal/dolthubauth"
610
)
711

12+
type roundTripFunc func(*http.Request) (*http.Response, error)
13+
14+
func (fn roundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) {
15+
return fn(req)
16+
}
17+
818
func TestAuthServiceWorkspaceResolver_InvalidateConnectionClearsPendingCaches(t *testing.T) {
919
resolver := NewAuthServiceWorkspaceResolver(nil, NewSessionStore())
1020
cache1 := newPendingUpstreamCache(nil, "hop", "wl-commons", time.Hour)
@@ -24,3 +34,37 @@ func TestAuthServiceWorkspaceResolver_InvalidateConnectionClearsPendingCaches(t
2434
t.Fatal("expected unrelated pending cache to remain")
2535
}
2636
}
37+
38+
func TestAuthServiceWorkspaceResolver_BuildClientWarmsPendingCache(t *testing.T) {
39+
resolver := NewAuthServiceWorkspaceResolver(nil, NewSessionStore())
40+
session := &UserSession{
41+
SubjectID: "subject-1",
42+
ConnectionID: "conn-1",
43+
}
44+
conn := &dolthubauth.ConnectionResponse{
45+
ConnectionID: "conn-1",
46+
SubjectID: "subject-1",
47+
RigHandle: "alice",
48+
}
49+
wl := dolthubauth.WastelandConfig{
50+
Upstream: "hop/wl-commons",
51+
ForkOrg: "alice",
52+
ForkDB: "wl-commons",
53+
Mode: "pr",
54+
}
55+
56+
client := &http.Client{Transport: roundTripFunc(func(*http.Request) (*http.Response, error) {
57+
return nil, context.Canceled
58+
})}
59+
60+
if _, err := resolver.buildClient(session, conn, client, wl); err != nil {
61+
t.Fatalf("buildClient() error = %v", err)
62+
}
63+
64+
key := "conn-1:hop/wl-commons"
65+
cache, ok := resolver.pendingCache[key]
66+
if !ok {
67+
t.Fatalf("expected pending cache %q to be created during build", key)
68+
}
69+
cache.Stop()
70+
}

0 commit comments

Comments
 (0)