Skip to content

Commit

Permalink
Result indicates whether requests were coalesced
Browse files Browse the repository at this point in the history
  • Loading branch information
mccutchen committed Sep 4, 2024
1 parent 251c348 commit 5d71ebb
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
8 changes: 6 additions & 2 deletions urlresolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type Result struct {
ResolvedURL string
Title string
IntermediateURLs []string
Coalesced bool
}

// Resolver resolves URLs.
Expand Down Expand Up @@ -76,10 +77,13 @@ func (r *Resolver) Resolve(ctx context.Context, givenURL string) (Result, error)
givenURL = Canonicalize(u)
}

val, err, _ := r.singleflightGroup.Do(givenURL, func() (interface{}, error) {
val, err, coalesced := r.singleflightGroup.Do(givenURL, func() (interface{}, error) {
return r.doResolve(ctx, givenURL)
})
return val.(Result), err

result := val.(Result)
result.Coalesced = coalesced
return result, err
}

func (r *Resolver) doResolve(ctx context.Context, givenURL string) (Result, error) {
Expand Down
1 change: 1 addition & 0 deletions urlresolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,7 @@ func TestResolver(t *testing.T) {
wantResult := Result{
Title: "title",
ResolvedURL: srv.URL,
Coalesced: true,
}

resolver := New(http.DefaultTransport, 0)
Expand Down

0 comments on commit 5d71ebb

Please sign in to comment.