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

Add a Channel Handler that closes the channel #967

Merged
merged 2 commits into from
Apr 30, 2019

Conversation

Palleas
Copy link
Contributor

@Palleas Palleas commented Apr 11, 2019

Motivation:

Because of #600, many users will start leaking Channels because they
don't close on error.

Modifications:

  • Add ChannelHandlerError
  • Add ChannelHandlerErrorTest

Result:

User will have access to an handler to use to automatically close
the channel in case of unhandled errors

Closes #841.

@swift-nio-bot
Copy link

Can one of the admins verify this patch?

4 similar comments
@swift-nio-bot
Copy link

Can one of the admins verify this patch?

@swift-nio-bot
Copy link

Can one of the admins verify this patch?

@swift-nio-bot
Copy link

Can one of the admins verify this patch?

@swift-nio-bot
Copy link

Can one of the admins verify this patch?

@Palleas Palleas force-pushed the add-error-channel-handler-841 branch from 641d0d0 to 1882ae7 Compare April 11, 2019 19:31
Copy link
Contributor

@Lukasa Lukasa left a comment

Choose a reason for hiding this comment

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

Cool, generally this looks good! Do you mind addressing the notes I’ve left in the diff?

@@ -0,0 +1,11 @@
import Foundation

final public class ChannelHandlerError: ChannelInboundHandler {
Copy link
Contributor

Choose a reason for hiding this comment

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

This should be called CloseOnErrorHandler or something similar.

Copy link
Contributor

Choose a reason for hiding this comment

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

Please also update the file names with the new handler name.

Copy link
Contributor

Choose a reason for hiding this comment

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

Actually @normanmaurer is there prior art in Netty here?

Copy link
Member

Choose a reason for hiding this comment

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

We do not include such a handler in Netty, so no :)

Copy link
Member

Choose a reason for hiding this comment

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

we also need to prefix this with NIO. I.e. NIOCloseOnErrorHandler because that's the guarantee we gave in the public API declaration.

The other question is: is this swift-nio or swift-nio-extras?

Copy link
Contributor

Choose a reason for hiding this comment

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

Good question. I could go either way there.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added the NIO prefix, for the rest just let me know 😀

@@ -0,0 +1,11 @@
import Foundation
Copy link
Contributor

Choose a reason for hiding this comment

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

This import of Foundation is unnecessary and can be removed. Additionally, you’ll need to place a license header in this file, as seen in the others.

@@ -0,0 +1,46 @@
import XCTest
Copy link
Contributor

Choose a reason for hiding this comment

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

This file also needs a license header.

@Palleas
Copy link
Contributor Author

Palleas commented Apr 11, 2019

@Lukasa All done, thanks for the feedback!

//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2017-2018 Apple Inc. and the SwiftNIO project authors
Copy link
Member

Choose a reason for hiding this comment

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

2019 ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oops, I wondered when I copy/pasted 🙈 Fixed!
Do you want me to squash into one commit since only the first matches the template?

Copy link
Contributor

Choose a reason for hiding this comment

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

Squashing the commits would be great!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done!

@Palleas Palleas force-pushed the add-error-channel-handler-841 branch from 000bb52 to b441c8a Compare April 12, 2019 15:25
//
//===----------------------------------------------------------------------===//

final public class NIOCloseOnErrorHandler: ChannelInboundHandler {
Copy link
Member

Choose a reason for hiding this comment

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

  • sorry, super nit but Jazzy renders this as is, would you mind making it public final (instead of final public)
  • this needs documentation for all the public bits that don't come from at least ChannelInboundHandler, ie. the class and the init

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done! Let me know if you want me to put more details in the docs.

@Palleas Palleas force-pushed the add-error-channel-handler-841 branch from b441c8a to 6e05f6f Compare April 12, 2019 15:42
@Palleas
Copy link
Contributor Author

Palleas commented Apr 18, 2019

@normanmaurer @Lukasa Fixed! Sorry I should have pinged you!

public init() {}

public func errorCaught(context: ChannelHandlerContext, error: Error) {
context.close(promise: nil)
Copy link
Contributor

Choose a reason for hiding this comment

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

Here's a question: should this forward the error on? @weissi?

Copy link
Member

Choose a reason for hiding this comment

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

@Lukasa I think I agree, yes, don't think this could do anything harmful. In most cases absolutely nothing will happen (because this will likely be the last handler) but in certain cases it might be better to give the user visibility.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So, forward then close? I’m guessing the order matters here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@Lukasa @weissi tried something in 061f163, is that the right approach?

Copy link
Member

Choose a reason for hiding this comment

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

yes, that works!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Perfect, squashed into one commit!

@Palleas Palleas force-pushed the add-error-channel-handler-841 branch 3 times, most recently from 63b4fc2 to 34c82c7 Compare April 23, 2019 15:19
@Palleas
Copy link
Contributor Author

Palleas commented Apr 23, 2019

@Lukasa @normanmaurer all squashed and ready for another review 🦸🏻‍♂️

Copy link
Contributor

@Lukasa Lukasa left a comment

Choose a reason for hiding this comment

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

Great, LGTM.

@Lukasa Lukasa added the 🆕 semver/minor Adds new public API. label Apr 29, 2019
@Lukasa Lukasa added this to the 2.1.0 milestone Apr 29, 2019
Copy link
Member

@normanmaurer normanmaurer left a comment

Choose a reason for hiding this comment

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

LGTM... thanks

@Lukasa
Copy link
Contributor

Lukasa commented Apr 29, 2019

@swift-nio-bot test this please

@weissi
Copy link
Member

weissi commented Apr 29, 2019

@Palleas your pull request uses windows newlines (\r\n) instead of UNIX newlines (\n) and hence it doesn't pass the license header check tests. Could you run dos2unix Tests/NIOTests/NIOCloseOnErrorHandlerTest.swift Sources/NIO/NIOCloseOnErrorHandler.swift and add all the changes to your PR?

You might also want to fix your text editor to produce UNIX newlines

@Palleas
Copy link
Contributor Author

Palleas commented Apr 29, 2019

Uh, how did that happen 🤨 I’ll fix that today!

Motivation:
Because of apple#600, many users will start leaking Channels because they
don't close on error.

Modifications:

* Add ChannelHandlerError
* Add ChannelHandlerErrorTest

Result:

User will have access to an handler to use to automatically close
the channel in case of unhandled errors
@Palleas Palleas force-pushed the add-error-channel-handler-841 branch from 2b2d67c to 104d1ab Compare April 29, 2019 15:46
@Palleas
Copy link
Contributor Author

Palleas commented Apr 29, 2019

@Lukasa @weissi all done!

@Lukasa
Copy link
Contributor

Lukasa commented Apr 29, 2019

@swift-nio-bot test this please

@Lukasa
Copy link
Contributor

Lukasa commented Apr 29, 2019

@swift-nio-bot test this please

@weissi weissi merged commit d093ae0 into apple:master Apr 30, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🆕 semver/minor Adds new public API.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Provide ChannelHandler that closes Channel on unhandled error.
5 participants