-
Notifications
You must be signed in to change notification settings - Fork 419
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
Improve performance of routing inbound RPCs #924
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,6 @@ | |
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
import Foundation | ||
import SwiftProtobuf | ||
import NIO | ||
import NIOHTTP1 | ||
|
@@ -31,11 +30,11 @@ public protocol CallHandlerProvider: class { | |
/// The name of the service this object is providing methods for, including the package path. | ||
/// | ||
/// - Example: "io.grpc.Echo.EchoService" | ||
var serviceName: String { get } | ||
var serviceName: Substring { get } | ||
|
||
/// Determines, calls and returns the appropriate request handler (`GRPCCallHandler`), depending on the request's | ||
/// method. Returns nil for methods not handled by this service. | ||
func handleMethod(_ methodName: String, callHandlerContext: CallHandlerContext) -> GRPCCallHandler? | ||
func handleMethod(_ methodName: Substring, callHandlerContext: CallHandlerContext) -> GRPCCallHandler? | ||
} | ||
|
||
// This is public because it will be passed into generated code, all members are `internal` because | ||
|
@@ -57,7 +56,7 @@ public struct CallHandlerContext { | |
/// from the pipeline. | ||
public final class GRPCServerRequestRoutingHandler { | ||
private let logger: Logger | ||
private let servicesByName: [String: CallHandlerProvider] | ||
private let servicesByName: [Substring: CallHandlerProvider] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not directly related to these changes: but I wonder if just storing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, in principle we could investigate that as a further optimisation as well. |
||
private let encoding: ServerMessageEncoding | ||
private weak var errorDelegate: ServerErrorDelegate? | ||
|
||
|
@@ -69,7 +68,7 @@ public final class GRPCServerRequestRoutingHandler { | |
private var state: State = .notConfigured | ||
|
||
public init( | ||
servicesByName: [String: CallHandlerProvider], | ||
servicesByName: [Substring: CallHandlerProvider], | ||
encoding: ServerMessageEncoding, | ||
errorDelegate: ServerErrorDelegate?, | ||
logger: Logger | ||
|
@@ -219,17 +218,17 @@ extension GRPCServerRequestRoutingHandler: ChannelInboundHandler, RemovableChann | |
// `CallHandlerProvider`s should provide the service name including the package name. | ||
// - uriComponents[2]: method name. | ||
self.logger.debug("making call handler", metadata: ["path": "\(requestHead.uri)"]) | ||
let uriComponents = requestHead.uri.components(separatedBy: "/") | ||
let uriComponents = requestHead.uri.split(separator: "/") | ||
|
||
let context = CallHandlerContext( | ||
errorDelegate: self.errorDelegate, | ||
logger: self.logger, | ||
encoding: self.encoding | ||
) | ||
|
||
guard uriComponents.count >= 3 && uriComponents[0].isEmpty, | ||
let providerForServiceName = servicesByName[uriComponents[1]], | ||
let callHandler = providerForServiceName.handleMethod(uriComponents[2], callHandlerContext: context) else { | ||
guard uriComponents.count >= 2, | ||
let providerForServiceName = servicesByName[uriComponents[0]], | ||
let callHandler = providerForServiceName.handleMethod(uriComponents[1], callHandlerContext: context) else { | ||
self.logger.notice("could not create handler", metadata: ["path": "\(requestHead.uri)"]) | ||
return nil | ||
} | ||
|
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.
🚀