-
Notifications
You must be signed in to change notification settings - Fork 7
fix: make Close() idempotent and add Close tests #161
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
Open
kacpersaw
wants to merge
7
commits into
main
Choose a base branch
from
kacpersaw/improve-testing
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d434530
fix: make Close() idempotent and add Close tests
kacpersaw 2299944
Merge branch 'main' into kacpersaw/improve-testing
kacpersaw cc9fb1a
fix: cleanup retry timers when work loop exits
kacpersaw 715ebfa
fix: wait for work goroutine to exit in Close()
kacpersaw 4857695
fix: reorder defers so cleanup runs before closing done channel
kacpersaw 29d40ae
fix: cleanup goroutine on initialization error
kacpersaw c037054
Merge branch 'main' into kacpersaw/improve-testing
kacpersaw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -86,16 +86,24 @@ func newPodEventLogger(ctx context.Context, opts podEventLoggerOptions) (*podEve | |
| }, | ||
| maxRetries: opts.maxRetries, | ||
| }, | ||
| doneChan: make(chan struct{}), | ||
| } | ||
|
|
||
| // Start the work goroutine once | ||
| go reporter.lq.work(reporter.ctx, reporter.doneChan) | ||
|
|
||
| // If no namespaces are provided, we listen for events in all namespaces. | ||
| if len(opts.namespaces) == 0 { | ||
| if err := reporter.initNamespace(""); err != nil { | ||
| reporter.cancelFunc() | ||
| <-reporter.doneChan | ||
| return nil, fmt.Errorf("init namespace: %w", err) | ||
| } | ||
| } else { | ||
| for _, namespace := range opts.namespaces { | ||
| if err := reporter.initNamespace(namespace); err != nil { | ||
| reporter.cancelFunc() | ||
| <-reporter.doneChan | ||
| return nil, err | ||
| } | ||
| } | ||
|
|
@@ -119,6 +127,11 @@ type podEventLogger struct { | |
|
|
||
| // hasSyncedFuncs tracks informer cache sync functions for testing | ||
| hasSyncedFuncs []cache.InformerSynced | ||
|
|
||
| // closeOnce ensures Close() is idempotent | ||
| closeOnce sync.Once | ||
| // doneChan is closed when the work goroutine exits | ||
| doneChan chan struct{} | ||
| } | ||
|
|
||
| // resolveEnvValue resolves the value of an environment variable, supporting both | ||
|
|
@@ -161,8 +174,6 @@ func (p *podEventLogger) initNamespace(namespace string) error { | |
| // This is to prevent us from sending duplicate events. | ||
| startTime := time.Now() | ||
|
|
||
| go p.lq.work(p.ctx) | ||
|
|
||
| podFactory := informers.NewSharedInformerFactoryWithOptions(p.client, 0, informers.WithNamespace(namespace), informers.WithTweakListOptions(func(lo *v1.ListOptions) { | ||
| lo.FieldSelector = p.fieldSelector | ||
| lo.LabelSelector = p.labelSelector | ||
|
|
@@ -411,10 +422,15 @@ func (p *podEventLogger) sendDelete(token string) { | |
| } | ||
|
|
||
| // Close stops the pod event logger and releases all resources. | ||
| // Close is idempotent and safe to call multiple times. | ||
| func (p *podEventLogger) Close() error { | ||
| p.cancelFunc() | ||
| close(p.stopChan) | ||
| close(p.errChan) | ||
| p.closeOnce.Do(func() { | ||
| p.cancelFunc() | ||
| close(p.stopChan) | ||
| close(p.errChan) | ||
| }) | ||
| // Wait for the work goroutine to exit | ||
| <-p.doneChan | ||
| return nil | ||
| } | ||
|
|
||
|
|
@@ -503,7 +519,10 @@ type logQueuer struct { | |
| maxRetries int | ||
| } | ||
|
|
||
| func (l *logQueuer) work(ctx context.Context) { | ||
| func (l *logQueuer) work(ctx context.Context, done chan struct{}) { | ||
| defer close(done) | ||
|
Member
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. probs should be the first deffered handler no? defers run in LIFO order |
||
| defer l.cleanup() | ||
|
|
||
| for ctx.Err() == nil { | ||
| select { | ||
| case log := <-l.q: | ||
|
|
@@ -521,6 +540,19 @@ func (l *logQueuer) work(ctx context.Context) { | |
| } | ||
| } | ||
|
|
||
| // cleanup stops all retry timers and cleans up resources when the work loop exits. | ||
| func (l *logQueuer) cleanup() { | ||
| l.mu.Lock() | ||
| defer l.mu.Unlock() | ||
|
|
||
| for token, rs := range l.retries { | ||
| if rs != nil && rs.timer != nil { | ||
| rs.timer.Stop() | ||
| } | ||
| delete(l.retries, token) | ||
| } | ||
| } | ||
|
|
||
| func (l *logQueuer) newLogger(ctx context.Context, log agentLog) (agentLoggerLifecycle, error) { | ||
| client := agentsdk.New(l.coderURL, agentsdk.WithFixedToken(log.agentToken)) | ||
| logger := l.logger.With(slog.F("resource_name", log.resourceName)) | ||
|
|
||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Shouldn't you make this wait for the goroutines to exit too? by having a
defer close(p.doneChan)at the top of thework()handler?