Skip to content

client: add connState channel #767

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

Merged
merged 1 commit into from
Mar 16, 2025
Merged

Conversation

magiconair
Copy link
Member

Add support for an optional channel to receive client state changes.

Fixes #741

@magiconair magiconair force-pushed the issue-741-state-change-channel branch 2 times, most recently from b1e0b5f to 883309c Compare February 9, 2025 09:04
@magiconair magiconair requested a review from kung-foo February 9, 2025 09:05
@magiconair magiconair added this to the v0.7.1 milestone Feb 9, 2025
@magiconair magiconair modified the milestones: v0.7.1, v0.7.2 Feb 27, 2025
Add support for an optional channel to receive client state changes.

Fixes #741
@magiconair magiconair force-pushed the issue-741-state-change-channel branch from 883309c to f44bb63 Compare March 16, 2025 06:37
@magiconair magiconair merged commit ef1c8c8 into main Mar 16, 2025
5 checks passed
@magiconair magiconair deleted the issue-741-state-change-channel branch March 16, 2025 06:47
@magiconair magiconair modified the milestones: v0.7.2, v0.7.3 Apr 7, 2025
@sruehl
Copy link
Contributor

sruehl commented May 27, 2025

I wonder what the decision was to use a channel for that if it is called in that synced fashion. So I just want to use that to hook a log statement too but this puts me in a weird spot on when to know when to end the go func to consume that channel.
So IMHO the code should be extended as follows:

	if c.stateCh != nil {
		select {
		case <-ctx.Done():
		case c.stateCh <- s:
                default:
		}
	}

with the default branch we resolve the blocking issue by not blocking state changes. Of course the same could be done with a buffered channel but then the question is what is a proper size.
Feels like a:

func StateChanged(fn func(ConnState)) Option {
	return func(cfg *Config) error {
		cfg.stateFn = fn
		return nil
	}
}

would be simpler to use as we don't have the complexity managing the channel and go func when the current implementation requires it to be non-blocking anway.

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.

Does the Client/Connection send out 'events' or notify in case the status of the connection changes?
2 participants