-
Notifications
You must be signed in to change notification settings - Fork 154
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
Support context.Context based cancellation #191
Conversation
DialWithContext(), ClientWithContext() and ServerWithContext() are added. Instead, ConnectTimeout is dropped from Config.
Codecov Report
@@ Coverage Diff @@
## master #191 +/- ##
==========================================
- Coverage 75.36% 75.23% -0.13%
==========================================
Files 63 63
Lines 3540 3550 +10
==========================================
+ Hits 2668 2671 +3
- Misses 586 591 +5
- Partials 286 288 +2
Continue to review full report at Codecov.
|
ConnectTimeout *time.Duration | ||
// function to make a context used in Dial(), Client(), Server(), and Accept(). | ||
// If nil, DefaultConnectContextMaker is used. | ||
ConnectContextMaker func() (context.Context, func()) |
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.
This function type suggest user to use cancelable contexts.
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.
I am fine removing this entirely! ping @daenney make sure I am not making a bad decision :)
Context
with a deadline if they still need deadlines.- We don't support Dialer, so not like we are breaking any APIs (Dialer also doesn't support server so will not work for us)
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.
I added this to make it possible to customize the behavior of accepting connection in dtls.listener
.
As Listen()
blocks until incoming connection and return established dtls.Conn
, deadline timer must be initialized at the time the first packet, that triggers dtls handshake, arrives.
Adding this makes it easy to implement both handshake timeout and graceful shutdown of server like:
func DTLSServer(ctx context.Context) {
l, err := dtls.Listen(..., &dtls.Config{
return context.WithTimeout(ctx, 10*time.Second)
})
for {
conn, err := l.Accept()
...
}
}
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.
Thanks for the ping. I think this makes sense, and the fact that when not explicitly provided we inject a default context with a proper timeout makes me happy.
d1fb931
to
821a2c5
Compare
821a2c5
to
4e898fb
Compare
Increased context related test coverage. |
088d2d2
to
ff4ef4b
Compare
config.go
Outdated
|
||
// MTU is the length at which handshake messages will be fragmented to | ||
// fit within the maximum transmission unit (default is 1200 bytes) | ||
MTU int | ||
} | ||
|
||
const defaultConnectTimeout = 30 * time.Second | ||
// DefaultConnectContextMaker is a default ConnectContextMaker used in Dial(), Client(), Server(), and Accept(). | ||
var DefaultConnectContextMaker = func() (context.Context, func()) { |
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.
Mind making this a function? I don't believe modification is anything we will ever need!
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.
In stdlib, default things are often defined as global variable, e.g. net.DefaultResolver
: https://golang.org/pkg/net/#pkg-variables
One of my intention of this was providing an example of ConnectContextMaker implementation on godoc.
I'll make it a function, unexpose, and explicitly add an example as a comment.
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.
Sorry I am wrong! Wasn't aware this was a Go pattern. I just always avoid globals and the possibility of people modifying things.
If you want to revert and go back to the way you had it I am happy with that :)
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.
updated
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.
But yea, it may allow something bad. If one of the library does
func init() {
net.DefaultResolver = &net.Resolver{
Dial: EvilDial,
}
}
It's weird.
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.
So I agree this is a pattern in stdlib, but I think this is a place where we should deviate from stdlib, and I'm happy to see the defaultConnectContextMaker
instead.
I understand why it's historically shown up in stdlib, net/http does something similar with DefaultHTTPClient
so you can call a naked http.$Method
without needing to create your own http.Client
first, but it's proven to be quite the footgun.
With the next context API is it possible to be notified of a Close now without doing a
|
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.
@at-wat sorry for being so slow, left some questions! I am happy with this landing in master, we can always iterate/continue to work on it :)
If nil, context.WithTimeout(context.Background(), 30*time.Second) will be used.
Add Go 1.13 error wrapping compatible error wrapper and make errConnectTimeout derived from context.DeadlineExceeded.
ff4ef4b
to
f58b55a
Compare
@Sean-Der unfortunately, #191 (comment) is not possible. I think it's better to add a state callback for this purpose. |
@at-wat I am happy with merging! We can always improve things as we go, such a shame about the leakage :( I will add the state callback soon, need it for good WebRTC behavior |
Thank you for reviewing! |
DialWithContext()
,ClientWithContext()
andServerWithContext()
are added. Instead,
ConnectTimeout
is dropped fromConfig
.Reference issue
Closes #189