LifecycleExample.swift 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * Copyright 2025, 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 GRPCCore
  17. import GRPCInProcessTransport
  18. import GRPCServiceLifecycle
  19. import Logging
  20. import ServiceLifecycle
  21. @main
  22. struct LifecycleExample {
  23. static func main() async throws {
  24. // Create the gRPC service. It periodically changes the greeting returned to the client.
  25. // It also conforms to 'ServiceLifecycle.Service' and uses the 'run()' method to perform
  26. // the updates.
  27. //
  28. // A more realistic service may use the run method to maintain a connection to an upstream
  29. // service or database.
  30. let greetingService = GreetingService(updateInterval: .microseconds(250))
  31. // Create the client and server using the in-process transport (which is used here for
  32. // simplicity.)
  33. let inProcess = InProcessTransport()
  34. let server = GRPCServer(transport: inProcess.server, services: [greetingService])
  35. let client = GRPCClient(transport: inProcess.client)
  36. // Configure the service group with the services. They're started in the order they're listed
  37. // and shutdown in reverse order.
  38. let serviceGroup = ServiceGroup(
  39. services: [
  40. greetingService,
  41. server,
  42. client,
  43. ],
  44. logger: Logger(label: "io.grpc.examples.service-lifecycle")
  45. )
  46. try await withThrowingDiscardingTaskGroup { group in
  47. // Run the service group in a task group. This isn't typically required but is here in
  48. // order to make requests using the client while the service group is running.
  49. group.addTask {
  50. try await serviceGroup.run()
  51. }
  52. // Make some requests, pausing between each to give the server a chance to update
  53. // the greeting.
  54. let greeter = Helloworld_Greeter.Client(wrapping: client)
  55. for request in 1 ... 10 {
  56. let reply = try await greeter.sayHello(.with { $0.name = "request-\(request)" })
  57. print(reply.message)
  58. // Sleep for a moment.
  59. let waitTime = Duration.milliseconds((50 ... 400).randomElement()!)
  60. try await Task.sleep(for: waitTime)
  61. }
  62. // Finally, shutdown the service group gracefully.
  63. await serviceGroup.triggerGracefulShutdown()
  64. }
  65. }
  66. }