-
Notifications
You must be signed in to change notification settings - Fork 36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Modified refresh client to calculate the shortest token expiry #1190
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -22,6 +22,7 @@ package refresh | |||||||||||||
|
||||||||||||||
import ( | ||||||||||||||
"context" | ||||||||||||||
"math" | ||||||||||||||
"time" | ||||||||||||||
|
||||||||||||||
"github.com/golang/protobuf/ptypes" | ||||||||||||||
|
@@ -108,16 +109,23 @@ func (t *refreshClient) Close(ctx context.Context, conn *networkservice.Connecti | |||||||||||||
func after(ctx context.Context, conn *networkservice.Connection) (time.Duration, error) { | ||||||||||||||
clockTime := clock.FromContext(ctx) | ||||||||||||||
|
||||||||||||||
expireTime, err := ptypes.Timestamp(conn.GetCurrentPathSegment().GetExpires()) | ||||||||||||||
if err != nil { | ||||||||||||||
return 0, errors.WithStack(err) | ||||||||||||||
} | ||||||||||||||
var minTimeout = time.Duration(math.MaxInt64) | ||||||||||||||
for _, seg := range conn.GetPath().GetPathSegments() { | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||||||||||||||
expireTime, err := ptypes.Timestamp(seg.GetExpires()) | ||||||||||||||
if err != nil { | ||||||||||||||
return 0, errors.WithStack(err) | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
timeout := clockTime.Until(expireTime) | ||||||||||||||
log.FromContext(ctx).Infof("expiration after %s at %s", timeout.String(), expireTime.UTC()) | ||||||||||||||
timeout := clockTime.Until(expireTime) | ||||||||||||||
log.FromContext(ctx).Infof("expiration after %s at %s", timeout.String(), expireTime.UTC()) | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||||||||||||||
|
||||||||||||||
if timeout <= 0 { | ||||||||||||||
return 1, nil | ||||||||||||||
if timeout <= 0 { | ||||||||||||||
return 1, nil | ||||||||||||||
} | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||||||||||||||
|
||||||||||||||
if timeout < minTimeout { | ||||||||||||||
minTimeout = timeout | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||||||||||||||
|
||||||||||||||
// A heuristic to reduce the number of redundant requests in a chain | ||||||||||||||
|
@@ -129,7 +137,7 @@ func after(ctx context.Context, conn *networkservice.Connection) (time.Duration, | |||||||||||||
if len(path.PathSegments) > 1 { | ||||||||||||||
scale = 0.2 + 0.2*float64(path.Index)/float64(len(path.PathSegments)) | ||||||||||||||
} | ||||||||||||||
duration := time.Duration(float64(timeout) * scale) | ||||||||||||||
duration := time.Duration(float64(minTimeout) * scale) | ||||||||||||||
|
||||||||||||||
return duration, nil | ||||||||||||||
} |
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -18,24 +18,25 @@ package refresh_test | |||||||||
|
||||||||||
import ( | ||||||||||
"context" | ||||||||||
"fmt" | ||||||||||
"sync/atomic" | ||||||||||
"testing" | ||||||||||
"time" | ||||||||||
|
||||||||||
"github.com/networkservicemesh/api/pkg/api/networkservice" | ||||||||||
"github.com/networkservicemesh/api/pkg/api/registry" | ||||||||||
"github.com/stretchr/testify/require" | ||||||||||
"go.uber.org/goleak" | ||||||||||
"google.golang.org/grpc/credentials" | ||||||||||
|
||||||||||
"github.com/networkservicemesh/api/pkg/api/networkservice" | ||||||||||
"github.com/networkservicemesh/api/pkg/api/registry" | ||||||||||
|
||||||||||
"github.com/networkservicemesh/sdk/pkg/networkservice/common/begin" | ||||||||||
"github.com/networkservicemesh/sdk/pkg/networkservice/common/refresh" | ||||||||||
"github.com/networkservicemesh/sdk/pkg/networkservice/common/updatepath" | ||||||||||
"github.com/networkservicemesh/sdk/pkg/networkservice/common/updatetoken" | ||||||||||
"github.com/networkservicemesh/sdk/pkg/networkservice/core/adapters" | ||||||||||
"github.com/networkservicemesh/sdk/pkg/networkservice/core/chain" | ||||||||||
"github.com/networkservicemesh/sdk/pkg/networkservice/core/next" | ||||||||||
countutil "github.com/networkservicemesh/sdk/pkg/networkservice/utils/count" | ||||||||||
"github.com/networkservicemesh/sdk/pkg/networkservice/utils/inject/injectclock" | ||||||||||
"github.com/networkservicemesh/sdk/pkg/networkservice/utils/inject/injecterror" | ||||||||||
"github.com/networkservicemesh/sdk/pkg/networkservice/utils/metadata" | ||||||||||
|
@@ -62,6 +63,28 @@ func testTokenFunc(clockTime clock.Clock) token.GeneratorFunc { | |||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
func testTokenFuncWithTimeout(clockTime clock.Clock, timeout time.Duration) token.GeneratorFunc { | ||||||||||
return func(_ credentials.AuthInfo) (token string, expireTime time.Time, err error) { | ||||||||||
return "", clockTime.Now().Add(timeout), err | ||||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
type captureTickerDuration struct { | ||||||||||
*clockmock.Mock | ||||||||||
|
||||||||||
tickerDuration time.Duration | ||||||||||
} | ||||||||||
|
||||||||||
func (m *captureTickerDuration) Ticker(d time.Duration) clock.Ticker { | ||||||||||
m.tickerDuration = d | ||||||||||
return m.Mock.Ticker(d) | ||||||||||
} | ||||||||||
|
||||||||||
func (m *captureTickerDuration) Reset(t time.Time) { | ||||||||||
m.tickerDuration = 0 | ||||||||||
m.Set(t) | ||||||||||
} | ||||||||||
|
||||||||||
func testClient( | ||||||||||
ctx context.Context, | ||||||||||
tokenGenerator token.GeneratorFunc, | ||||||||||
|
@@ -302,6 +325,82 @@ func TestRefreshClient_NoRefreshOnFailure(t *testing.T) { | |||||||||
require.Never(t, cloneClient.validator(2), testWait, testTick) | ||||||||||
} | ||||||||||
|
||||||||||
func TestRefreshClient_CalculatesShortestTokenTimeout(t *testing.T) { | ||||||||||
t.Cleanup(func() { goleak.VerifyNone(t) }) | ||||||||||
|
||||||||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) | ||||||||||
defer cancel() | ||||||||||
|
||||||||||
testData := []struct { | ||||||||||
Chain []time.Duration | ||||||||||
ExpectedRefreshTimeoutMax time.Duration | ||||||||||
ExpectedRefreshTimeoutMin time.Duration | ||||||||||
}{ | ||||||||||
{ | ||||||||||
Chain: []time.Duration{time.Hour}, | ||||||||||
ExpectedRefreshTimeoutMax: 20*time.Minute + time.Second, | ||||||||||
ExpectedRefreshTimeoutMin: 20*time.Minute - time.Second, | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use const time range.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Apply this to other places. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||||||||||
}, | ||||||||||
{ | ||||||||||
Chain: []time.Duration{time.Hour, 3 * time.Minute}, | ||||||||||
ExpectedRefreshTimeoutMax: 54*time.Second + time.Second/2., | ||||||||||
ExpectedRefreshTimeoutMin: 54*time.Second - time.Second/2., | ||||||||||
}, | ||||||||||
{ | ||||||||||
Chain: []time.Duration{time.Hour, 5 * time.Second, 3 * time.Minute}, | ||||||||||
ExpectedRefreshTimeoutMax: 5*time.Second/3. + time.Second/3., | ||||||||||
ExpectedRefreshTimeoutMin: 5*time.Second/3. - time.Second/3., | ||||||||||
}, | ||||||||||
{ | ||||||||||
Chain: []time.Duration{200 * time.Millisecond, 1 * time.Minute, 100 * time.Millisecond, time.Hour}, | ||||||||||
ExpectedRefreshTimeoutMax: 100*time.Millisecond/3. + 30*time.Millisecond, | ||||||||||
ExpectedRefreshTimeoutMin: 100*time.Millisecond/3. - 30*time.Millisecond, | ||||||||||
}, | ||||||||||
} | ||||||||||
|
||||||||||
timeNow, err := time.Parse("2006-01-02 15:04:05", "2009-11-10 23:00:00") | ||||||||||
require.NoError(t, err) | ||||||||||
|
||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||||||||||
clockMock := captureTickerDuration{ | ||||||||||
Mock: clockmock.New(ctx), | ||||||||||
} | ||||||||||
|
||||||||||
var countClient = &countutil.Client{} | ||||||||||
|
||||||||||
for _, testDataElement := range testData { | ||||||||||
clockMock.Reset(timeNow) | ||||||||||
|
||||||||||
var pathChain []networkservice.NetworkServiceClient | ||||||||||
var clientChain = []networkservice.NetworkServiceClient{ | ||||||||||
begin.NewClient(), | ||||||||||
metadata.NewClient(), | ||||||||||
injectclock.NewClient(&clockMock), | ||||||||||
refresh.NewClient(ctx), | ||||||||||
} | ||||||||||
|
||||||||||
for _, expireTimeValue := range testDataElement.Chain { | ||||||||||
pathChain = append(pathChain, | ||||||||||
updatepath.NewClient(fmt.Sprintf("test-%v", expireTimeValue)), | ||||||||||
adapters.NewServerToClient(updatetoken.NewServer(testTokenFuncWithTimeout(&clockMock, expireTimeValue))), | ||||||||||
) | ||||||||||
} | ||||||||||
|
||||||||||
clientChain = append(pathChain, clientChain...) | ||||||||||
clientChain = append(clientChain, countClient) | ||||||||||
client := chain.NewNetworkServiceClient(clientChain...) | ||||||||||
|
||||||||||
_, err = client.Request(ctx, &networkservice.NetworkServiceRequest{ | ||||||||||
Connection: new(networkservice.Connection), | ||||||||||
}) | ||||||||||
require.NoError(t, err) | ||||||||||
|
||||||||||
require.Less(t, clockMock.tickerDuration, testDataElement.ExpectedRefreshTimeoutMax) | ||||||||||
require.Greater(t, clockMock.tickerDuration, testDataElement.ExpectedRefreshTimeoutMin) | ||||||||||
} | ||||||||||
|
||||||||||
require.Equal(t, countClient.Requests(), len(testData)) | ||||||||||
} | ||||||||||
|
||||||||||
func TestRefreshClient_RefreshOnRefreshFailure(t *testing.T) { | ||||||||||
t.Cleanup(func() { goleak.VerifyNone(t) }) | ||||||||||
|
||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we don't initialize
minTimeout
, it will always be0
. It's initialized with max possible duration so that it's always greater than any value.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please fix the case when there no segments.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done