Skip to content

Commit

Permalink
different flushTimeout update strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
simo5 committed Dec 8, 2017
1 parent c872939 commit eda1084
Showing 1 changed file with 32 additions and 11 deletions.
43 changes: 32 additions & 11 deletions pkg/auth/oauth/registry/timeoutvalidator.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ import (

apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/util/runtime"

"github.com/openshift/origin/pkg/oauth/apis/oauth"
"github.com/openshift/origin/pkg/oauth/apis/oauth/validation"
oauthclient "github.com/openshift/origin/pkg/oauth/generated/internalclientset/typed/oauth/internalversion"
oauthclientlister "github.com/openshift/origin/pkg/oauth/generated/listers/oauth/internalversion"
"github.com/openshift/origin/pkg/user/apis/user"
)

Expand Down Expand Up @@ -41,12 +43,8 @@ func (a *tokenDataRef) Less(than btree.Item) bool {
return a.timeout.Before(than.(*tokenDataRef).timeout)
}

type OAuthClientGetter interface {
Get(name string) (*oauth.OAuthClient, error)
}

type TimeoutValidator struct {
oauthClient OAuthClientGetter
oauthClients oauthclientlister.OAuthClientLister
tokens oauthclient.OAuthAccessTokenInterface
tokenChannel chan tokenData
data map[string]tokenData
Expand All @@ -73,10 +71,10 @@ func computeTimeouts(configuredTimeout int32) (actualTimeout int32, flushTimeout
return actualTimeout, flushTimeout, safetyMargin
}

func NewTimeoutValidator(tokens oauthclient.OAuthAccessTokenInterface, oauthClient OAuthClientGetter, configuredTimeout int32) *TimeoutValidator {
func NewTimeoutValidator(tokens oauthclient.OAuthAccessTokenInterface, oauthClients oauthclientlister.OAuthClientLister, configuredTimeout int32) *TimeoutValidator {
defaultTimeout, flushTimeout, safetyMargin := computeTimeouts(configuredTimeout)
ttu := &TimeoutValidator{
oauthClient,
oauthClients,
tokens,
make(chan tokenData),
make(map[string]tokenData),
Expand Down Expand Up @@ -123,21 +121,43 @@ func (a *TimeoutValidator) updateToken(td tokenData) {
}

func (a *TimeoutValidator) clientTimeout(name string) time.Duration {
oauthClient, err := a.oauthClient.Get(name)
oauthClient, err := a.oauthClients.Get(name)
if err != nil {
glog.V(5).Infof("Failed to fetch OAuthClient %q for timeout value: %v", name, err)
return a.defaultTimeout
}
if oauthClient.AccessTokenTimeoutSeconds == nil {
return a.defaultTimeout
}
actualTimeout, flushTimeout, safetyMargin := computeTimeouts(*oauthClient.AccessTokenTimeoutSeconds)
return timeoutAsDuration(*oauthClient.AccessTokenTimeoutSeconds)
}

func (a *TimeoutValidator) updateTimeouts() {
smallerTimeout := int32(a.defaultTimeout / time.Second)
smallerClientName := "<default>"
clients, err := a.oauthClients.List(labels.Everything())
if err != nil {
glog.V(5).Infof("Failed to list OauthClients to update timeout values: %v", err)
return
}
for _, client := range clients {
if client.AccessTokenTimeoutSeconds == nil {
continue
}
if *client.AccessTokenTimeoutSeconds == 0 {
continue
}
if *client.AccessTokenTimeoutSeconds < smallerTimeout {
smallerTimeout = *client.AccessTokenTimeoutSeconds
smallerClientName = client.Name
}
}
_, flushTimeout, safetyMargin := computeTimeouts(smallerTimeout)
if flushTimeout < int32(a.flushTimeout/time.Second) {
a.flushTimeout = timeoutAsDuration(flushTimeout)
a.safetyMargin = timeoutAsDuration(safetyMargin)
glog.V(5).Infof("OauthClient %q updates timeouts to flushTimeout=%s safetyMargin=%s", oauthClient.Name, a.flushTimeout, a.safetyMargin)
glog.V(5).Infof("OauthClient %q updates timeouts to flushTimeout=%s safetyMargin=%s", smallerClientName, a.flushTimeout, a.safetyMargin)
}
return timeoutAsDuration(actualTimeout)
}

func (a *TimeoutValidator) insert(td tokenData) {
Expand Down Expand Up @@ -244,6 +264,7 @@ func (a *TimeoutValidator) Run(stopCh <-chan struct{}) {
defer closeTimer()

updateTimerAndFlush := func() {
a.updateTimeouts()
closeTimer()
nextTimer.Reset(a.flushTimeout)
nextTimeout = time.Now().Add(a.flushTimeout)
Expand Down

0 comments on commit eda1084

Please sign in to comment.