Skip to content
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

unwrap client #130

Merged
merged 1 commit into from
Nov 7, 2023
Merged

unwrap client #130

merged 1 commit into from
Nov 7, 2023

Conversation

avalanche123
Copy link
Collaborator

Description of your changes

Simplify client - extend expiry on get, and don't wrap client in session.

I have:

  • Read and followed Upbound's contribution process.
  • Run make reviewable to ensure this PR is ready for review.
  • Added backport release-x.y labels to auto-backport this PR, as appropriate.

How has this code been tested

Tested locally.

Signed-off-by: Bulat Shakirzyanov <83289+avalanche123@users.noreply.github.com>
@@ -117,6 +116,7 @@ func Anonymize(cfg *rest.Config) *rest.Config {
// type the client is asked to get or list. Clients (and their caches) expire
// and are garbage collected if they are unused for five minutes.
type Cache struct {
ctx context.Context
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of creating an adhoc context when initializing a client, create a context that will be valid for the lifetime of Cache

"new-expiry", time.Now().Add(c.expiry),
)
sn.expiration.Reset(c.expiry)
return sn.client, nil
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return session client directly, client expiration is extended when a client is requested, rather than used. This simplifies code as we don't have to wrap the client anymore and still preserves cached clients since their expiration is extended from each resolver that needs to use them. extending client's expiration during operations would only be useful if the expiration duration can be lower than a duration if any one client operation, which a very unlikely situation.

@@ -295,15 +299,18 @@ func (c *Cache) Get(cr auth.Credentials, o ...GetOption) (client.Client, error)
// is possible to 'reset' (i.e. extend) a ticker.
expiration := &tickerExpiration{t: time.NewTicker(c.expiry)}
newExpiry := time.Now().Add(c.expiry)
ctx, cancel := context.WithCancel(context.Background())
sn = &session{client: wc, cancel: cancel, expiry: c.expiry, expiration: expiration, log: log}
ctx, cancel := context.WithCancel(c.ctx)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use Cache's context for constructing the client instead of creating a new context.

}
c.remove(id)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove is idempotent, so let it run no matter how we got here.

@avalanche123 avalanche123 merged commit 5ce8112 into main Nov 7, 2023
8 checks passed
@avalanche123 avalanche123 deleted the unwrap-client branch November 7, 2023 23:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants