Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions internal/clerkcache/memberships.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,16 @@ type userLister interface {
List(ctx context.Context, params *user.ListParams) (*clerk.UserList, error)
}

// Users is a user lister that answers from Valkey when it can, like Memberships.
// Users is a user lister that answers from Valkey when it can, like Memberships, and tries a dropped
// connection once more too (TL-909).
type Users struct {
Next userLister
VK valkey.Client
TTL time.Duration
}

func (u Users) List(ctx context.Context, params *user.ListParams) (*clerk.UserList, error) {
return cached(ctx, u.VK, u.TTL, "clerk:users:", params, u.Next.List)
return cached(ctx, u.VK, u.TTL, "clerk:users:", params, retried(u.Next.List))
}

// cached answers fetch(params) from Valkey under prefix + a hash of params, filling it on a miss.
Expand Down
29 changes: 29 additions & 0 deletions internal/clerkcache/memberships_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/clerk/clerk-sdk-go/v2"
"github.com/clerk/clerk-sdk-go/v2/organizationmembership"
"github.com/clerk/clerk-sdk-go/v2/user"
tcvalkey "github.com/testcontainers/testcontainers-go/modules/valkey"
"github.com/valkey-io/valkey-go"
)
Expand Down Expand Up @@ -119,3 +120,31 @@ func TestMembershipsRetryDroppedConnection(t *testing.T) {
t.Fatalf("caller gone: %v after %d calls, want no retry", err, calls)
}
}

type flakyUsers struct {
calls *int
fails int
err error
}

func (f flakyUsers) List(context.Context, *user.ListParams) (*clerk.UserList, error) {
if *f.calls++; *f.calls <= f.fails {
return nil, f.err
}
return &clerk.UserList{}, nil
}

// TL-909: the user list (usernames on the assignee picker) retries a dropped connection like memberships.
func TestUsersRetryDroppedConnection(t *testing.T) {
ctx := context.Background()
reset := &url.Error{Op: "Get", URL: "https://api.clerk.com", Err: &net.OpError{Op: "read", Net: "tcp", Err: syscall.ECONNRESET}}
params := &user.ListParams{UserIDs: []string{"user_a"}}
calls := 0
if _, err := (Users{Next: flakyUsers{&calls, 1, reset}}).List(ctx, params); err != nil || calls != 2 {
t.Fatalf("reset once: %v after %d calls, want an answer after 2", err, calls)
}
calls = 0
if _, err := (Users{Next: flakyUsers{&calls, 1, errors.New("forbidden")}}).List(ctx, params); err == nil || calls != 1 {
t.Fatalf("clerk error: %v after %d calls, want no retry", err, calls)
}
}