AsyncPingPongRequestMaker.swift 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * Copyright 2020, 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 Foundation
  17. import GRPC
  18. import Logging
  19. import NIO
  20. /// Makes streaming requests and listens to responses ping-pong style.
  21. /// Iterations can be limited by config.
  22. final class AsyncPingPongRequestMaker: RequestMaker {
  23. private let client: Grpc_Testing_BenchmarkServiceClient
  24. private let requestMessage: Grpc_Testing_SimpleRequest
  25. private let logger: Logger
  26. private let stats: StatsWithLock
  27. /// If greater than zero gives a limit to how many messages are exchanged before termination.
  28. private let messagesPerStream: Int
  29. /// Stops more requests being made after stop is requested.
  30. private var stopRequested = false
  31. /// Initialiser to gather requirements.
  32. /// - Parameters:
  33. /// - config: config from the driver describing what to do.
  34. /// - client: client interface to the server.
  35. /// - requestMessage: Pre-made request message to use possibly repeatedly.
  36. /// - logger: Where to log useful diagnostics.
  37. /// - stats: Where to record statistics on latency.
  38. init(config: Grpc_Testing_ClientConfig,
  39. client: Grpc_Testing_BenchmarkServiceClient,
  40. requestMessage: Grpc_Testing_SimpleRequest,
  41. logger: Logger,
  42. stats: StatsWithLock) {
  43. self.client = client
  44. self.requestMessage = requestMessage
  45. self.logger = logger
  46. self.stats = stats
  47. self.messagesPerStream = Int(config.messagesPerStream)
  48. }
  49. /// Initiate a request sequence to the server - in this case the sequence is streaming requests to the server and waiting
  50. /// to see responses before repeating ping-pong style. The number of iterations can be limited by config.
  51. /// - returns: A future which completes when the request-response sequence is complete.
  52. func makeRequest() -> EventLoopFuture<GRPCStatus> {
  53. var startTime = grpcTimeNow()
  54. var messagesSent = 1
  55. var streamingCall: BidirectionalStreamingCall<
  56. Grpc_Testing_SimpleRequest,
  57. Grpc_Testing_SimpleResponse
  58. >?
  59. /// Handle a response from the server - potentially triggers making another request.
  60. /// Will execute on the event loop which deals with thread safety concerns.
  61. func handleResponse(response: Grpc_Testing_SimpleResponse) {
  62. streamingCall!.eventLoop.preconditionInEventLoop()
  63. let endTime = grpcTimeNow()
  64. self.stats.add(latency: endTime - startTime)
  65. if !self.stopRequested,
  66. self.messagesPerStream == 0 || messagesSent < self.messagesPerStream {
  67. messagesSent += 1
  68. startTime = endTime // Use end of previous request as the start of the next.
  69. streamingCall!.sendMessage(self.requestMessage, promise: nil)
  70. } else {
  71. streamingCall!.sendEnd(promise: nil)
  72. }
  73. }
  74. // Setup the call.
  75. streamingCall = self.client.streamingCall(handler: handleResponse)
  76. // Kick start with initial request
  77. streamingCall!.sendMessage(self.requestMessage, promise: nil)
  78. return streamingCall!.status
  79. }
  80. /// Request termination of the request-response sequence.
  81. func requestStop() {
  82. // Flag stop as requested - this will prevent any more requests being made.
  83. self.stopRequested = true
  84. }
  85. }