-
Notifications
You must be signed in to change notification settings - Fork 420
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First pass implementation of NIO client (#357)
* First pass implementation of NIO client. * Renaming and removal of force unwrap/try * Improve error handling in NIO server. - Adds a user-configurable error handler to the server - Updates NIO server codegen to provide an optional error handler - Errors are handled by GRPCChannelHandler or BaseCallHandler, depending on the pipeline state - Adds some error handling tests - Tidies some logic in HTTP1ToRawGRPCServerCodec - Extends message handling logic in HTTP1ToRawGRPCServerCodec to handle messages split across multiple ByteBuffers (i.e. when a message exceeds a the size of a frame) * Update error delegate * Renaming, tidy up HTTP1ToRawGRPCClientCodec, CallOptions * Client code-gen * Strongly hold errorDelegate in the server until shutdown * Timeoutes, tidying up, documentation * GRPCTimeout documentation * Add timeout to request headers * Add client cancelling and timeout tests. * Fix typos, missing doc * Add allCases to CompressionMehcnaism for swift < 4.2 * Update LinuxMain * More errors to a dedicated enum, fix typos, etc. * PR feedback; docs, tidying * Renaming, typo fixes * Split out GRPCChannelHandlerTests and HTTPToRawGRPCServerCodecTests * Update LinuxMain * Add missing commas to LinuxMain * Fix grpc-web testUnaryLotsOfRequests on Linux * Disable broken Linux test * Split errors into server and client enums. * Add more client-specific errors * Fixup comments, documentation * Fix typos and clarify documentation * Enqueue messages to be sent * Workaround compile error for swift<4.2 * Fix documentation, add TODOs * Increase timeout for bidi tests
- Loading branch information
Showing
52 changed files
with
2,701 additions
and
594 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
// | ||
// DO NOT EDIT. | ||
// | ||
// Generated by the protocol buffer compiler. | ||
// Source: echo.proto | ||
// | ||
|
||
// | ||
// Copyright 2018, gRPC Authors All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
import Foundation | ||
import NIO | ||
import NIOHTTP1 | ||
import SwiftGRPCNIO | ||
import SwiftProtobuf | ||
|
||
|
||
/// Usage: instantiate Echo_EchoService_NIOClient, then call methods of this protocol to make API calls. | ||
internal protocol Echo_EchoService_NIO { | ||
func get(_ request: Echo_EchoRequest, callOptions: CallOptions?) -> UnaryClientCall<Echo_EchoRequest, Echo_EchoResponse> | ||
func expand(_ request: Echo_EchoRequest, callOptions: CallOptions?, handler: @escaping (Echo_EchoResponse) -> Void) -> ServerStreamingClientCall<Echo_EchoRequest, Echo_EchoResponse> | ||
func collect(callOptions: CallOptions?) -> ClientStreamingClientCall<Echo_EchoRequest, Echo_EchoResponse> | ||
func update(callOptions: CallOptions?, handler: @escaping (Echo_EchoResponse) -> Void) -> BidirectionalStreamingClientCall<Echo_EchoRequest, Echo_EchoResponse> | ||
} | ||
|
||
internal final class Echo_EchoService_NIOClient: GRPCServiceClient, Echo_EchoService_NIO { | ||
internal let client: GRPCClient | ||
internal let service = "echo.Echo" | ||
internal var defaultCallOptions: CallOptions | ||
|
||
/// Creates a client for the echo.Echo service. | ||
/// | ||
/// - Parameters: | ||
/// - client: `GRPCClient` with a connection to the service host. | ||
/// - defaultCallOptions: Options to use for each service call if the user doesn't provide them. Defaults to `client.defaultCallOptions`. | ||
internal init(client: GRPCClient, defaultCallOptions: CallOptions? = nil) { | ||
self.client = client | ||
self.defaultCallOptions = defaultCallOptions ?? client.defaultCallOptions | ||
} | ||
|
||
/// Asynchronous unary call to Get. | ||
/// | ||
/// - Parameters: | ||
/// - request: Request to send to Get. | ||
/// - callOptions: Call options; `self.defaultCallOptions` is used if `nil`. | ||
/// - Returns: A `UnaryClientCall` with futures for the metadata, status and response. | ||
internal func get(_ request: Echo_EchoRequest, callOptions: CallOptions? = nil) -> UnaryClientCall<Echo_EchoRequest, Echo_EchoResponse> { | ||
return UnaryClientCall(client: client, path: path(forMethod: "Get"), request: request, callOptions: callOptions ?? self.defaultCallOptions) | ||
} | ||
|
||
/// Asynchronous server-streaming call to Expand. | ||
/// | ||
/// - Parameters: | ||
/// - request: Request to send to Expand. | ||
/// - callOptions: Call options; `self.defaultCallOptions` is used if `nil`. | ||
/// - handler: A closure called when each response is received from the server. | ||
/// - Returns: A `ServerStreamingClientCall` with futures for the metadata and status. | ||
internal func expand(_ request: Echo_EchoRequest, callOptions: CallOptions? = nil, handler: @escaping (Echo_EchoResponse) -> Void) -> ServerStreamingClientCall<Echo_EchoRequest, Echo_EchoResponse> { | ||
return ServerStreamingClientCall(client: client, path: path(forMethod: "Expand"), request: request, callOptions: callOptions ?? self.defaultCallOptions, handler: handler) | ||
} | ||
|
||
/// Asynchronous client-streaming call to Collect. | ||
/// | ||
/// Callers should use the `send` method on the returned object to send messages | ||
/// to the server. The caller should send an `.end` after the final message has been sent. | ||
/// | ||
/// - Parameters: | ||
/// - callOptions: Call options; `self.defaultCallOptions` is used if `nil`. | ||
/// - Returns: A `ClientStreamingClientCall` with futures for the metadata, status and response. | ||
internal func collect(callOptions: CallOptions? = nil) -> ClientStreamingClientCall<Echo_EchoRequest, Echo_EchoResponse> { | ||
return ClientStreamingClientCall(client: client, path: path(forMethod: "Collect"), callOptions: callOptions ?? self.defaultCallOptions) | ||
} | ||
|
||
/// Asynchronous bidirectional-streaming call to Update. | ||
/// | ||
/// Callers should use the `send` method on the returned object to send messages | ||
/// to the server. The caller should send an `.end` after the final message has been sent. | ||
/// | ||
/// - Parameters: | ||
/// - callOptions: Call options; `self.defaultCallOptions` is used if `nil`. | ||
/// - handler: A closure called when each response is received from the server. | ||
/// - Returns: A `ClientStreamingClientCall` with futures for the metadata and status. | ||
internal func update(callOptions: CallOptions? = nil, handler: @escaping (Echo_EchoResponse) -> Void) -> BidirectionalStreamingClientCall<Echo_EchoRequest, Echo_EchoResponse> { | ||
return BidirectionalStreamingClientCall(client: client, path: path(forMethod: "Update"), callOptions: callOptions ?? self.defaultCallOptions, handler: handler) | ||
} | ||
|
||
} | ||
|
||
/// To build a server, implement a class that conforms to this protocol. | ||
internal protocol Echo_EchoProvider_NIO: CallHandlerProvider { | ||
func get(request: Echo_EchoRequest, context: StatusOnlyCallContext) -> EventLoopFuture<Echo_EchoResponse> | ||
func expand(request: Echo_EchoRequest, context: StreamingResponseCallContext<Echo_EchoResponse>) -> EventLoopFuture<GRPCStatus> | ||
func collect(context: UnaryResponseCallContext<Echo_EchoResponse>) -> EventLoopFuture<(StreamEvent<Echo_EchoRequest>) -> Void> | ||
func update(context: StreamingResponseCallContext<Echo_EchoResponse>) -> EventLoopFuture<(StreamEvent<Echo_EchoRequest>) -> Void> | ||
} | ||
|
||
extension Echo_EchoProvider_NIO { | ||
internal var serviceName: String { return "echo.Echo" } | ||
|
||
/// Determines, calls and returns the appropriate request handler, depending on the request's method. | ||
/// Returns nil for methods not handled by this service. | ||
internal func handleMethod(_ methodName: String, request: HTTPRequestHead, serverHandler: GRPCChannelHandler, channel: Channel, errorDelegate: ServerErrorDelegate?) -> GRPCCallHandler? { | ||
switch methodName { | ||
case "Get": | ||
return UnaryCallHandler(channel: channel, request: request, errorDelegate: errorDelegate) { context in | ||
return { request in | ||
self.get(request: request, context: context) | ||
} | ||
} | ||
|
||
case "Expand": | ||
return ServerStreamingCallHandler(channel: channel, request: request, errorDelegate: errorDelegate) { context in | ||
return { request in | ||
self.expand(request: request, context: context) | ||
} | ||
} | ||
|
||
case "Collect": | ||
return ClientStreamingCallHandler(channel: channel, request: request, errorDelegate: errorDelegate) { context in | ||
return self.collect(context: context) | ||
} | ||
|
||
case "Update": | ||
return BidirectionalStreamingCallHandler(channel: channel, request: request, errorDelegate: errorDelegate) { context in | ||
return self.update(context: context) | ||
} | ||
|
||
default: return nil | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
../../../../Tests/SwiftGRPCNIOTests/echo.pb.swift | ||
../../Echo/Generated/echo.pb.swift |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.