AsyncPingPongRequestMaker.swift 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * Copyright 2022, gRPC Authors All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. import Atomics
  17. import Foundation
  18. import GRPC
  19. import Logging
  20. import NIOCore
  21. /// Makes streaming requests and listens to responses ping-pong style.
  22. /// Iterations can be limited by config.
  23. /// Class is marked as `@unchecked Sendable` because `ManagedAtomic<Bool>` doesn't conform
  24. /// to `Sendable`, but we know it's safe.
  25. final class AsyncPingPongRequestMaker: AsyncRequestMaker, @unchecked Sendable {
  26. private let client: Grpc_Testing_BenchmarkServiceAsyncClient
  27. private let requestMessage: Grpc_Testing_SimpleRequest
  28. private let logger: Logger
  29. private let stats: StatsWithLock
  30. /// If greater than zero gives a limit to how many messages are exchanged before termination.
  31. private let messagesPerStream: Int
  32. /// Stops more requests being made after stop is requested.
  33. private let stopRequested = ManagedAtomic<Bool>(false)
  34. /// Initialiser to gather requirements.
  35. /// - Parameters:
  36. /// - config: config from the driver describing what to do.
  37. /// - client: client interface to the server.
  38. /// - requestMessage: Pre-made request message to use possibly repeatedly.
  39. /// - logger: Where to log useful diagnostics.
  40. /// - stats: Where to record statistics on latency.
  41. init(
  42. config: Grpc_Testing_ClientConfig,
  43. client: Grpc_Testing_BenchmarkServiceAsyncClient,
  44. requestMessage: Grpc_Testing_SimpleRequest,
  45. logger: Logger,
  46. stats: StatsWithLock
  47. ) {
  48. self.client = client
  49. self.requestMessage = requestMessage
  50. self.logger = logger
  51. self.stats = stats
  52. self.messagesPerStream = Int(config.messagesPerStream)
  53. }
  54. /// Initiate a request sequence to the server - in this case the sequence is streaming requests to the server and waiting
  55. /// to see responses before repeating ping-pong style. The number of iterations can be limited by config.
  56. func makeRequest() async throws {
  57. var startTime = grpcTimeNow()
  58. var messagesSent = 0
  59. let streamingCall = self.client.makeStreamingCallCall()
  60. var responseStream = streamingCall.responseStream.makeAsyncIterator()
  61. while !self.stopRequested.load(ordering: .relaxed),
  62. self.messagesPerStream == 0 || messagesSent < self.messagesPerStream {
  63. try await streamingCall.requestStream.send(self.requestMessage)
  64. _ = try await responseStream.next()
  65. let endTime = grpcTimeNow()
  66. self.stats.add(latency: endTime - startTime)
  67. messagesSent += 1
  68. startTime = endTime
  69. }
  70. }
  71. /// Request termination of the request-response sequence.
  72. func requestStop() {
  73. self.logger.info("AsyncPingPongRequestMaker stop requested")
  74. // Flag stop as requested - this will prevent any more requests being made.
  75. self.stopRequested.store(true, ordering: .relaxed)
  76. }
  77. }