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 way to get a handler from a pipeline #974

Merged
merged 1 commit into from
Apr 30, 2019
Merged

Conversation

Palleas
Copy link
Contributor

@Palleas Palleas commented Apr 18, 2019

Motivation:

#966

Modifications:

  • Add a handler(type:) method on ChannelPipeline
  • Add a test in ChannelPipelineTest

Result:

Provides a nicer way to get a typed handler directly from the pipeline

Closes #966

@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?

@Lukasa Lukasa added the semver/minor Adds new public API. label Apr 18, 2019
@Lukasa Lukasa added this to the 2.1.0 milestone Apr 18, 2019
///
/// - parameters:
/// - handlerType: the type of `ChannelHandler` to returns
public func handler<T: ChannelHandler>(type _: T.Type) -> EventLoopFuture<T> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we have a version of this for each of our context methods? I've always thought the context methods were pretty weird, and ideally I'd get rid of them and replace them with these versions. @normanmaurer is there a good reason not to do that?

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 the context ones are there so you can search and later on remove without walking the pipeline. But I agree they're a bit weird, not sure if we should kill them though.

XCTAssertTrue(result2 === handler2)

let result3 = try channel.pipeline.handler(type: SimpleTypedHandler3.self).wait()
XCTAssertTrue(result3 === handler3)
Copy link
Member

Choose a reason for hiding this comment

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

nice! We could do some more tests here, like

  • two handlers of the same type in the pipeline, verify that we get the first of those
  • no handler of the given type there, verify that we get nil

@@ -1555,6 +1555,20 @@ extension ChannelPipeline: CustomDebugStringConvertible {
return desc.joined(separator: "\n")
}

/// Returns the `ChannelHandlerContext` that belongs to a `ChannelHandler` of the given type.
Copy link
Member

Choose a reason for hiding this comment

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

that documentation isn't quite right. It returns the first ChannelHandler of a given type.

///
/// - parameters:
/// - handlerType: the type of `ChannelHandler` to returns
public func handler<T: ChannelHandler>(type _: T.Type) -> EventLoopFuture<T> {
Copy link
Member

Choose a reason for hiding this comment

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

I think we should name the generic parameter Handler instead of just T

Copy link
Member

@weissi weissi left a comment

Choose a reason for hiding this comment

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

Thanks, that looks great already. A few suggestions

Copy link
Member

@weissi weissi left a comment

Choose a reason for hiding this comment

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

Awesome, just two nits left

Sources/NIO/ChannelPipeline.swift Outdated Show resolved Hide resolved
Sources/NIO/ChannelPipeline.swift Outdated Show resolved Hide resolved
Copy link
Member

@weissi weissi left a comment

Choose a reason for hiding this comment

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

Thank you! Looks good to me

@weissi
Copy link
Member

weissi commented Apr 18, 2019

@swift-nio-bot test this please

/// - parameters:
/// - handlerType: the type of `ChannelHandler` to return.
public func handler<Handler: ChannelHandler>(type _: Handler.Type) -> EventLoopFuture<Handler> {
return context(handlerType: Handler.self).map { context in
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: can we use the explicit self keyword 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 done!

Motivation:

apple#966

Modifications:

- Add a `handler(type:)` method on ChannelPipeline
- Add a test in ChannelPipelineTest

Result:

Provides a nicer way to get a typed handler directly from the pipeline

Closes apple#966
@Lukasa
Copy link
Contributor

Lukasa commented Apr 30, 2019

@swift-nio-bot test this please

@weissi weissi merged commit 81538ec into apple:master Apr 30, 2019
@weissi
Copy link
Member

weissi commented Apr 30, 2019

@Mordil You'll like this PR I think ;). @MrMage & @glbrntt I think you also have code like this around that should now be simpler (getting the http2 multiplexer from a client connection).

This pull request was closed.
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.

ChannelPipeline should have a way to give you a handler directly instead of via ChannelHandlerContext
4 participants