Skip to content

Conversation

@prashanthjos
Copy link
Contributor

@prashanthjos prashanthjos commented Jan 14, 2026

This enables consumers to react to connection state changes without polling. Callbacks are invoked when:

  • OnConnect: connection is established (including reconnections)
  • OnDisconnect: connection is lost (with error reason)

Changes

  • Add OnConnect callback field to ManagedConnection - invoked when connection is established
  • Add OnDisconnect callback field to ManagedConnection - invoked when connection is lost with error reason
  • Add tests for callback functionality

/kind enhancement

Fixes #

This change supports observability improvements in knative/serving#16318, where the activator needs to track autoscaler connection status. With callbacks, consumers can update metrics and logs in real-time without polling Status().

Release Note

Add OnConnect and OnDisconnect callbacks to websocket ManagedConnection for event-driven connection state monitoring

This enables consumers to react to connection state changes without polling. Callbacks are invoked when:
- OnConnect: connection is established (including reconnections)
- OnDisconnect: connection is lost (with error reason)
@knative-prow
Copy link

knative-prow bot commented Jan 14, 2026

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: prashanthjos
Once this PR has been reviewed and has the lgtm label, please assign aliok for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@knative-prow knative-prow bot requested review from aslom and skonto January 14, 2026 01:34
@knative-prow
Copy link

knative-prow bot commented Jan 14, 2026

Hi @prashanthjos. Thanks for your PR.

I'm waiting for a knative member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@knative-prow knative-prow bot added needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Jan 14, 2026
This enables consumers to react to connection state changes without polling. Callbacks are invoked when:
- OnConnect: connection is established (including reconnections)
- OnDisconnect: connection is lost (with error reason)
@codecov
Copy link

codecov bot commented Jan 14, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 74.66%. Comparing base (93d6642) to head (31d06d1).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3309      +/-   ##
==========================================
+ Coverage   74.63%   74.66%   +0.03%     
==========================================
  Files         188      188              
  Lines        8195     8207      +12     
==========================================
+ Hits         6116     6128      +12     
  Misses       1840     1840              
  Partials      239      239              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@prashanthjos
Copy link
Contributor Author

@dprotaso can you please have a look at this PR, this is a pre-required PR for

knative/serving#16318 (comment)

@prashanthjos
Copy link
Contributor Author

prashanthjos commented Jan 14, 2026

The callbacks need to be set before the connection goroutine starts to avoid a data race. When NewDurableSendingConnection returns, the background goroutine is already running and may read OnConnect/OnDisconnect at any time.

If we allowed setting callbacks after construction like:

conn := NewDurableSendingConnection(target, logger)
conn.OnConnect = func() { ... }  // RACE: goroutine may already be reading this

There's a race between the main goroutine writing the callback and the connection goroutine reading it.

The functional options pattern ensures callbacks are applied atomically before the goroutine starts:

conn := NewDurableSendingConnection(target, logger,
    WithOnConnect(func() { ... }),
    WithOnDisconnect(func(err error) { ... }),
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/enhancement needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant