Skip to content

Commit

Permalink
Refactor some properties shared by all server-side call contexts into…
Browse files Browse the repository at this point in the history
… a common `ServerCallContext` protocol … (#389)

* Refactor some properties shared by all server-side call contexts into a common `ServerCallContext` protocol, while renaming the existing `ServerCallContext` class to `ServerCallContextBase`.

Motivation: As opposed to all three other call types, unary calls are not passed an instance of the previous `ServerCallContext` class, but rather of the `StatusOnlyCallContext` protocol. If one wanted to create a method that uses only a call's event loop and request headers but wants to process all *four* types of calls, that was not possible before. With this change, such a method could simply accept an instance of `ServerCallContext`, which the call arguments provided to all four methods now conform to.

* Minor comment typo fix.
  • Loading branch information
MrMage authored Mar 4, 2019
1 parent 8b064bd commit c082197
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
13 changes: 10 additions & 3 deletions Sources/SwiftGRPCNIO/ServerCallContexts/ServerCallContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,18 @@ import SwiftProtobuf
import NIO
import NIOHTTP1

/// Base class providing data provided to the framework user for all server calls.
open class ServerCallContext {
/// Protocol declaring a minimum set of properties exposed by *all* types of call contexts.
public protocol ServerCallContext: class {
/// The event loop this call is served on.
public let eventLoop: EventLoop
var eventLoop: EventLoop { get }

/// Generic metadata provided with this request.
var request: HTTPRequestHead { get }
}

/// Base class providing data provided to the framework user for all server calls.
open class ServerCallContextBase: ServerCallContext {
public let eventLoop: EventLoop
public let request: HTTPRequestHead

public init(eventLoop: EventLoop, request: HTTPRequestHead) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import NIOHTTP1
/// - When `statusPromise` is fulfilled, the call is closed and the provided status transmitted.
/// - If `statusPromise` is failed and the error is of type `GRPCStatus`, that error will be returned to the client.
/// - For other errors, `GRPCStatus.processingError` is returned to the client.
open class StreamingResponseCallContext<ResponseMessage: Message>: ServerCallContext {
open class StreamingResponseCallContext<ResponseMessage: Message>: ServerCallContextBase {
public typealias WrappedResponse = GRPCServerResponsePart<ResponseMessage>

public let statusPromise: EventLoopPromise<GRPCStatus>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import NIOHTTP1
///
/// For unary calls, the response is not actually provided by fulfilling `responsePromise`, but instead by completing
/// the future returned by `UnaryCallHandler.EventObserver`.
open class UnaryResponseCallContext<ResponseMessage: Message>: ServerCallContext, StatusOnlyCallContext {
open class UnaryResponseCallContext<ResponseMessage: Message>: ServerCallContextBase, StatusOnlyCallContext {
public typealias WrappedResponse = GRPCServerResponsePart<ResponseMessage>

public let responsePromise: EventLoopPromise<ResponseMessage>
Expand All @@ -31,11 +31,8 @@ open class UnaryResponseCallContext<ResponseMessage: Message>: ServerCallContext
/// be fulfilled by the user.
///
/// We can use a protocol (instead of an abstract base class) here because removing the generic `responsePromise` field
/// lets us avoid associated-type requirements on the protol.
public protocol StatusOnlyCallContext {
var eventLoop: EventLoop { get }
var request: HTTPRequestHead { get }

/// lets us avoid associated-type requirements on the protocol.
public protocol StatusOnlyCallContext: ServerCallContext {
var responseStatus: GRPCStatus { get set }
}

Expand Down

0 comments on commit c082197

Please sign in to comment.