ChannelArgument.swift 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * Copyright 2018, 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. #if SWIFT_PACKAGE
  17. import CgRPC
  18. #endif
  19. import Foundation // for String.Encoding
  20. public extension Channel {
  21. enum Argument {
  22. /// Default authority to pass if none specified on call construction.
  23. case defaultAuthority(String)
  24. /// Primary user agent. Goes at the start of the user-agent metadata sent
  25. /// on each request.
  26. case primaryUserAgent(String)
  27. /// Secondary user agent. Goes at the end of the user-agent metadata sent
  28. /// on each request.
  29. case secondaryUserAgent(String)
  30. /// After a duration of this time, the client/server pings its peer to see
  31. /// if the transport is still alive.
  32. case keepAliveTime(TimeInterval)
  33. /// After waiting for a duration of this time, if the keepalive ping sender does
  34. /// not receive the ping ack, it will close the transport.
  35. case keepAliveTimeout(TimeInterval)
  36. /// Is it permissible to send keepalive pings without any outstanding streams?
  37. case keepAlivePermitWithoutCalls(Bool)
  38. /// The time between the first and second connection attempts.
  39. case reconnectBackoffInitial(TimeInterval)
  40. /// The minimum time between subsequent connection attempts.
  41. case reconnectBackoffMin(TimeInterval)
  42. /// The maximum time between subsequent connection attempts.
  43. case reconnectBackoffMax(TimeInterval)
  44. /// Should we allow receipt of true-binary data on http2 connections?
  45. /// Defaults to on (true)
  46. case http2EnableTrueBinary(Bool)
  47. /// Minimum time between sending successive ping frames without receiving
  48. /// any data frame.
  49. case http2MinSentPingInterval(TimeInterval)
  50. /// Number of pings before needing to send a data frame or header frame.
  51. /// `0` indicates that an infinite number of pings can be sent without
  52. /// sending a data frame or header frame.
  53. case http2MaxPingsWithoutData(UInt)
  54. /// This *should* be used for testing only.
  55. /// Override the target name used for SSL host name checking using this
  56. /// channel argument. If this argument is not specified, the name used
  57. /// for SSL host name checking will be the target parameter (assuming that the
  58. /// secure channel is an SSL channel). If this parameter is specified and the
  59. /// underlying is not an SSL channel, it will just be ignored.
  60. case sslTargetNameOverride(String)
  61. }
  62. }
  63. extension Channel.Argument {
  64. class Wrapper {
  65. // Creating a `grpc_arg` allocates memory. This wrapper ensures that the memory is freed after use.
  66. let wrapped: grpc_arg
  67. init(_ wrapped: grpc_arg) {
  68. self.wrapped = wrapped
  69. }
  70. deinit {
  71. gpr_free(wrapped.key)
  72. if wrapped.type == GRPC_ARG_STRING {
  73. gpr_free(wrapped.value.string)
  74. }
  75. }
  76. }
  77. func toCArg() -> Wrapper {
  78. switch self {
  79. case let .defaultAuthority(value):
  80. return makeArgument("grpc.default_authority", value: value)
  81. case let .primaryUserAgent(value):
  82. return makeArgument("grpc.primary_user_agent", value: value)
  83. case let .secondaryUserAgent(value):
  84. return makeArgument("grpc.secondary_user_agent", value: value)
  85. case let .keepAliveTime(value):
  86. return makeArgument("grpc.keepalive_time_ms", value: value * 1_000)
  87. case let .keepAliveTimeout(value):
  88. return makeArgument("grpc.keepalive_timeout_ms", value: value * 1_000)
  89. case let .keepAlivePermitWithoutCalls(value):
  90. return makeArgument("grpc.keepalive_permit_without_calls", value: value)
  91. case let .reconnectBackoffMin(value):
  92. return makeArgument("grpc.min_reconnect_backoff_ms", value: value * 1_000)
  93. case let .reconnectBackoffMax(value):
  94. return makeArgument("grpc.max_reconnect_backoff_ms", value: value * 1_000)
  95. case let .reconnectBackoffInitial(value):
  96. return makeArgument("grpc.initial_reconnect_backoff_ms", value: value * 1_000)
  97. case let .http2EnableTrueBinary(value):
  98. return makeArgument("grpc.http2.true_binary", value: value)
  99. case let .http2MinSentPingInterval(value):
  100. return makeArgument("grpc.http2.min_time_between_pings_ms", value: value * 1_000)
  101. case let .http2MaxPingsWithoutData(value):
  102. return makeArgument("grpc.http2.max_pings_without_data", value: value)
  103. case let .sslTargetNameOverride(value):
  104. return makeArgument("grpc.ssl_target_name_override", value: value)
  105. }
  106. }
  107. }
  108. private func makeArgument(_ key: String, value: String) -> Channel.Argument.Wrapper {
  109. var arg = grpc_arg()
  110. arg.key = gpr_strdup(key)
  111. arg.type = GRPC_ARG_STRING
  112. arg.value.string = gpr_strdup(value)
  113. return Channel.Argument.Wrapper(arg)
  114. }
  115. private func makeArgument(_ key: String, value: Bool) -> Channel.Argument.Wrapper {
  116. return makeArgument(key, value: Int32(value ? 1 : 0))
  117. }
  118. private func makeArgument(_ key: String, value: Double) -> Channel.Argument.Wrapper {
  119. return makeArgument(key, value: Int32(value))
  120. }
  121. private func makeArgument(_ key: String, value: UInt) -> Channel.Argument.Wrapper {
  122. return makeArgument(key, value: Int32(value))
  123. }
  124. private func makeArgument(_ key: String, value: Int) -> Channel.Argument.Wrapper {
  125. return makeArgument(key, value: Int32(value))
  126. }
  127. private func makeArgument(_ key: String, value: Int32) -> Channel.Argument.Wrapper {
  128. var arg = grpc_arg()
  129. arg.key = gpr_strdup(key)
  130. arg.type = GRPC_ARG_INTEGER
  131. arg.value.integer = value
  132. return Channel.Argument.Wrapper(arg)
  133. }