-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
193 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package client | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
"sync" | ||
) | ||
|
||
var ( | ||
// ErrRequestRedundant this error is returned when trying to perform the | ||
// same request (method, host, path) more than one time | ||
ErrRequestRedundant = errors.New("this request has been made already") | ||
) | ||
|
||
func decorateTransportWithRequestCacheDecorator(decorated http.RoundTripper) (*requestCacheTransportDecorator, error) { | ||
if decorated == nil { | ||
return nil, errors.New("decorated round tripper is nil") | ||
} | ||
|
||
return &requestCacheTransportDecorator{decorated: decorated}, nil | ||
} | ||
|
||
type requestCacheTransportDecorator struct { | ||
decorated http.RoundTripper | ||
requestMap sync.Map | ||
} | ||
|
||
func (u *requestCacheTransportDecorator) RoundTrip(r *http.Request) (*http.Response, error) { | ||
key := u.keyForRequest(r) | ||
|
||
_, found := u.requestMap.Load(key) | ||
if found { | ||
return nil, ErrRequestRedundant | ||
} | ||
|
||
u.requestMap.Store(key, struct{}{}) | ||
|
||
return u.decorated.RoundTrip(r) | ||
} | ||
|
||
func (u *requestCacheTransportDecorator) keyForRequest(r *http.Request) string { | ||
return fmt.Sprintf("%s~%s~%s", r.Method, r.Host, r.URL.Path) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package client | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestRequestCacheTransportDecorator(t *testing.T) { | ||
transport, err := decorateTransportWithRequestCacheDecorator(nil) | ||
assert.Nil(t, transport) | ||
assert.Error(t, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters