Skip to content

Commit

Permalink
Merge branch 'main' into v2/json-codegen
Browse files Browse the repository at this point in the history
  • Loading branch information
glbrntt authored Jan 20, 2025
2 parents e6b2137 + e4da2bb commit e00345b
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 54 deletions.
17 changes: 0 additions & 17 deletions Sources/GRPCCore/Call/Server/ServerContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,6 @@ public struct ServerContext: Sendable {
/// A description of the method being called.
public var descriptor: MethodDescriptor

/// A description of the remote peer.
///
/// The format of the description should follow the pattern "<transport>:<address>" where
/// "<transport>" indicates the underlying network transport (such as "ipv4", "unix", or
/// "in-process"). This is a guideline for how descriptions should be formatted; different
/// implementations may not follow this format so you shouldn't make assumptions based on it.
///
/// Some examples include:
/// - "ipv4:127.0.0.1:31415",
/// - "ipv6:[::1]:443",
/// - "in-process:27182".
@available(*, deprecated, renamed: "remotePeer")
public var peer: String {
get { remotePeer }
set { remotePeer = newValue }
}

/// A description of the remote peer.
///
/// The format of the description should follow the pattern "<transport>:<address>" where
Expand Down
4 changes: 2 additions & 2 deletions Sources/GRPCCore/Documentation.docc/Development/Design.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,12 @@ concurrency.
Most users won't use ``GRPCClient`` to execute RPCs directly, instead they will
use the generated client stubs which wrap the ``GRPCClient``. Users are
responsible for creating the client and running it (which starts and runs the
underlying transport). This is done by calling ``GRPCClient/run()``. The client
underlying transport). This is done by calling ``GRPCClient/runConnections()``. The client
can be shutdown gracefully by calling ``GRPCClient/beginGracefulShutdown()``
which will stop new RPCs from starting (by failing them with
``RPCError/Code-swift.struct/unavailable``) but allow existing ones to continue.
Existing work can be stopped more abruptly by cancelling the task where
``GRPCClient/run()`` is executing.
``GRPCClient/runConnections()`` is executing.

#### Server

Expand Down
7 changes: 1 addition & 6 deletions Sources/GRPCCore/GRPCClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -227,16 +227,11 @@ public final class GRPCClient<Transport: ClientTransport>: Sendable {
}
}

@available(*, deprecated, renamed: "runConnections", message: "It'll be removed before v2.")
public func run() async throws {
try await self.runConnections()
}

/// Close the client.
///
/// The transport will be closed: this means that it will be given enough time to wait for
/// in-flight RPCs to finish executing, but no new RPCs will be accepted. You can cancel the task
/// executing ``run()`` if you want to abruptly stop in-flight RPCs.
/// executing ``runConnections()`` if you want to abruptly stop in-flight RPCs.
public func beginGracefulShutdown() {
let wasRunning = self.stateMachine.withLock { $0.state.beginGracefulShutdown() }
if wasRunning {
Expand Down
11 changes: 0 additions & 11 deletions Sources/GRPCCore/MethodDescriptor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,6 @@ public struct MethodDescriptor: Sendable, Hashable {
self.service = ServiceDescriptor(fullyQualifiedService: fullyQualifiedService)
self.method = method
}

@available(*, deprecated, renamed: "init(fullyQualifiedService:method:)")
/// Creates a new method descriptor.
///
/// - Parameters:
/// - service: The fully qualified name of the service, including the package
/// name. For example, "helloworld.Greeter".
/// - method: The name of the method. For example, "SayHello".
public init(service: String, method: String) {
self.init(fullyQualifiedService: service, method: method)
}
}

extension MethodDescriptor: CustomStringConvertible {
Expand Down
1 change: 1 addition & 0 deletions Tests/GRPCCoreTests/Test Utilities/Coding+JSON.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import GRPCCore

import struct Foundation.Data
Expand Down
20 changes: 2 additions & 18 deletions Tests/GRPCInProcessTransportTests/InProcessTransportTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ struct InProcessTransportTests {
request: ClientRequest(message: ()),
descriptor: .peerInfo,
serializer: VoidSerializer(),
deserializer: PeerInfoDeserializer(),
deserializer: JSONDeserializer<PeerInfo>(),
options: .defaults
) {
try $0.message
Expand Down Expand Up @@ -142,7 +142,7 @@ private struct TestService: RegistrableRPCService {
router.registerHandler(
forMethod: .peerInfo,
deserializer: VoidDeserializer(),
serializer: PeerInfoSerializer(),
serializer: JSONSerializer<PeerInfo>(),
handler: {
let response = try await self.peerInfo(
request: ServerRequest<Void>(stream: $0),
Expand Down Expand Up @@ -171,22 +171,6 @@ private struct PeerInfo: Codable {
var remote: String
}

private struct PeerInfoSerializer: MessageSerializer {
func serialize<Bytes: GRPCContiguousBytes>(_ message: PeerInfo) throws -> Bytes {
Bytes("\(message.local) \(message.remote)".utf8)
}
}

private struct PeerInfoDeserializer: MessageDeserializer {
func deserialize<Bytes: GRPCContiguousBytes>(_ serializedMessageBytes: Bytes) throws -> PeerInfo {
let stringPeerInfo = serializedMessageBytes.withUnsafeBytes {
String(decoding: $0, as: UTF8.self)
}
let peerInfoComponents = stringPeerInfo.split(separator: " ")
return PeerInfo(local: String(peerInfoComponents[0]), remote: String(peerInfoComponents[1]))
}
}

private struct UTF8Serializer: MessageSerializer {
func serialize<Bytes: GRPCContiguousBytes>(_ message: String) throws -> Bytes {
Bytes(message.utf8)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2025, 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 GRPCCore

import struct Foundation.Data
import class Foundation.JSONDecoder
import class Foundation.JSONEncoder

struct JSONSerializer<Message: Codable>: MessageSerializer {
func serialize<Bytes: GRPCContiguousBytes>(_ message: Message) throws -> Bytes {
do {
let jsonEncoder = JSONEncoder()
let data = try jsonEncoder.encode(message)
return Bytes(data)
} catch {
throw RPCError(code: .internalError, message: "Can't serialize message to JSON. \(error)")
}
}
}

struct JSONDeserializer<Message: Codable>: MessageDeserializer {
func deserialize<Bytes: GRPCContiguousBytes>(_ serializedMessageBytes: Bytes) throws -> Message {
do {
let jsonDecoder = JSONDecoder()
let data = serializedMessageBytes.withUnsafeBytes { Data($0) }
return try jsonDecoder.decode(Message.self, from: data)
} catch {
throw RPCError(code: .internalError, message: "Can't deserialze message from JSON. \(error)")
}
}
}

0 comments on commit e00345b

Please sign in to comment.