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

pass a context to Transport.ConnContext #4536

Merged
merged 2 commits into from
Jun 5, 2024

Conversation

marten-seemann
Copy link
Member

@marten-seemann marten-seemann commented May 28, 2024

This context is cancelled when the QUIC connection is closed, or when the QUIC handshake fails. This allows the application to easily build and garbage collect a map of active connections.

TODO:

Copy link

codecov bot commented May 29, 2024

Codecov Report

Attention: Patch coverage is 53.84615% with 6 lines in your changes missing coverage. Please review.

Project coverage is 85.14%. Comparing base (07acaad) to head (4413c73).

Files Patch % Lines
server.go 40.00% 6 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4536      +/-   ##
==========================================
- Coverage   85.16%   85.14%   -0.01%     
==========================================
  Files         154      154              
  Lines       14835    14841       +6     
==========================================
+ Hits        12633    12636       +3     
- Misses       1695     1698       +3     
  Partials      507      507              

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

// The context returned from the callback is used to derive every other context used during the
// lifetime of the connection:
// * the context passed to crypto/tls (and used on the tls.ClientHelloInfo)
// * the context used in Config.Tracer
// * the context returned from Connection.Context
// * the context returned from SendStream.Context
// It is not used for dialed connections.
ConnContext func() context.Context
ConnContext func(context.Context) context.Context
Copy link
Member Author

Choose a reason for hiding this comment

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

There's another restriction here: The context returned MUST be derived from the context passed into ConnContext.

We can:

  1. Document it.
  2. Document and enforce it, by attaching an internal value to the context, and checking that it exists on the returned context (and panic if not).
  3. Remove this requirement. This means we'll have to pass a second context.CancelCauseFunc to the connection struct.

Copy link
Collaborator

@sukunrt sukunrt Jun 4, 2024

Choose a reason for hiding this comment

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

Can we derive a context from the context returned from transport.ConnContext?

I mean

ctx, cancel = context.WithCancelCause(s.connContext())
...
conn = s.newConn(ctx, cancel ...)

Copy link
Collaborator

Choose a reason for hiding this comment

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

Otherwise 3 seems the nicest.

Copy link
Member Author

Choose a reason for hiding this comment

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

Can we derive a context from the context returned from transport.ConnContext?

I mean

ctx, cancel = context.WithCancelCause(s.connContext())
...
conn = s.newConn(ctx, cancel ...)

I don't think that works, since we want the context we pass to ConnContext to be canceled when the connection is closed.

Copy link
Member Author

Choose a reason for hiding this comment

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

Actually, we can do 3 without any major refactor. A context.ContextCancelFunc is just a fancy name for a func(error), so we can just wrap the cancel func of the original and the returned context into one function.

This context is cancelled when the QUIC connection is closed, or when
the QUIC handshake fails. This allows the application to easily build
and garbage collect a map of active connections.
@marten-seemann marten-seemann merged commit 44e0147 into master Jun 5, 2024
34 checks passed
@marten-seemann marten-seemann deleted the conn-context-cancellation branch June 6, 2024 09:17
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.

pass a context to ConnContext context, cancel it when the connection is closed
2 participants