-
Notifications
You must be signed in to change notification settings - Fork 656
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix race condition in endpoint discovery (#1504)
- Loading branch information
Showing
5 changed files
with
214 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"id": "9667162d-c94c-43c7-bb0c-5b0bbcd5ef8a", | ||
"type": "bugfix", | ||
"description": "Fixed a race condition that caused concurrent calls relying on endpoint discovery to share the same `url.URL` reference in their operation's http.Request.", | ||
"modules": [ | ||
"service/internal/endpoint-discovery" | ||
] | ||
} |
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,46 @@ | ||
package endpointdiscovery | ||
|
||
import ( | ||
"net/url" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestEndpointCache_Get_prune(t *testing.T) { | ||
c := NewEndpointCache(2) | ||
c.Add(Endpoint{ | ||
Key: "foo", | ||
Addresses: []WeightedAddress{ | ||
{ | ||
URL: &url.URL{ | ||
Host: "foo.amazonaws.com", | ||
}, | ||
Expired: time.Now().Add(5 * time.Minute), | ||
}, | ||
{ | ||
URL: &url.URL{ | ||
Host: "bar.amazonaws.com", | ||
}, | ||
Expired: time.Now().Add(5 * -time.Minute), | ||
}, | ||
}, | ||
}) | ||
|
||
load, _ := c.endpoints.Load("foo") | ||
if ev := load.(Endpoint); len(ev.Addresses) != 2 { | ||
t.Errorf("expected two weighted addresses") | ||
} | ||
|
||
weightedAddress, ok := c.Get("foo") | ||
if !ok { | ||
t.Errorf("expect weighted address, got none") | ||
} | ||
if e, a := "foo.amazonaws.com", weightedAddress.URL.Host; e != a { | ||
t.Errorf("expect %v, got %v", e, a) | ||
} | ||
|
||
load, _ = c.endpoints.Load("foo") | ||
if ev := load.(Endpoint); len(ev.Addresses) != 1 { | ||
t.Errorf("expected one weighted address") | ||
} | ||
} |
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,123 @@ | ||
package endpointdiscovery | ||
|
||
import ( | ||
"net/url" | ||
"reflect" | ||
"strconv" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func Test_cloneURL(t *testing.T) { | ||
tests := []struct { | ||
value *url.URL | ||
wantClone *url.URL | ||
}{ | ||
{ | ||
value: &url.URL{ | ||
Scheme: "https", | ||
Opaque: "foo", | ||
User: nil, | ||
Host: "amazonaws.com", | ||
Path: "/", | ||
RawPath: "/", | ||
ForceQuery: true, | ||
RawQuery: "thing=value", | ||
Fragment: "1234", | ||
RawFragment: "1234", | ||
}, | ||
wantClone: &url.URL{ | ||
Scheme: "https", | ||
Opaque: "foo", | ||
User: nil, | ||
Host: "amazonaws.com", | ||
Path: "/", | ||
RawPath: "/", | ||
ForceQuery: true, | ||
RawQuery: "thing=value", | ||
Fragment: "1234", | ||
RawFragment: "1234", | ||
}, | ||
}, | ||
{ | ||
value: &url.URL{ | ||
Scheme: "https", | ||
Opaque: "foo", | ||
User: url.UserPassword("NOT", "VALID"), | ||
Host: "amazonaws.com", | ||
Path: "/", | ||
RawPath: "/", | ||
ForceQuery: true, | ||
RawQuery: "thing=value", | ||
Fragment: "1234", | ||
RawFragment: "1234", | ||
}, | ||
wantClone: &url.URL{ | ||
Scheme: "https", | ||
Opaque: "foo", | ||
User: url.UserPassword("NOT", "VALID"), | ||
Host: "amazonaws.com", | ||
Path: "/", | ||
RawPath: "/", | ||
ForceQuery: true, | ||
RawQuery: "thing=value", | ||
Fragment: "1234", | ||
RawFragment: "1234", | ||
}, | ||
}, | ||
} | ||
for i, tt := range tests { | ||
t.Run(strconv.Itoa(i), func(t *testing.T) { | ||
gotClone := cloneURL(tt.value) | ||
if gotClone == tt.value { | ||
t.Errorf("expct clone URL to not be same pointer address") | ||
} | ||
if tt.value.User != nil { | ||
if tt.value.User == gotClone.User { | ||
t.Errorf("expct cloned Userinfo to not be same pointer address") | ||
} | ||
} | ||
if !reflect.DeepEqual(gotClone, tt.wantClone) { | ||
t.Errorf("cloneURL() = %v, want %v", gotClone, tt.wantClone) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestEndpoint_Prune(t *testing.T) { | ||
endpoint := Endpoint{} | ||
|
||
endpoint.Add(WeightedAddress{ | ||
URL: &url.URL{}, | ||
Expired: time.Now().Add(5 * time.Minute), | ||
}) | ||
|
||
initial := endpoint.Addresses | ||
|
||
if e, a := false, endpoint.Prune(); e != a { | ||
t.Errorf("expect prune %v, got %v", e, a) | ||
} | ||
|
||
if e, a := &initial[0], &endpoint.Addresses[0]; e != a { | ||
t.Errorf("expect slice address to be same") | ||
} | ||
|
||
endpoint.Add(WeightedAddress{ | ||
URL: &url.URL{}, | ||
Expired: time.Now().Add(5 * -time.Minute), | ||
}) | ||
|
||
initial = endpoint.Addresses | ||
|
||
if e, a := true, endpoint.Prune(); e != a { | ||
t.Errorf("expect prune %v, got %v", e, a) | ||
} | ||
|
||
if e, a := &initial[0], &endpoint.Addresses[0]; e == a { | ||
t.Errorf("expect slice address to be different") | ||
} | ||
|
||
if e, a := 1, endpoint.Len(); e != a { | ||
t.Errorf("expect slice length %v, got %v", e, a) | ||
} | ||
} |