server_options.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. *
  3. * Copyright 2021 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. package xds
  19. import (
  20. "net"
  21. "google.golang.org/grpc"
  22. "google.golang.org/grpc/connectivity"
  23. )
  24. type serverOptions struct {
  25. modeCallback ServingModeCallbackFunc
  26. bootstrapContentsForTesting []byte
  27. }
  28. type serverOption struct {
  29. grpc.EmptyServerOption
  30. apply func(*serverOptions)
  31. }
  32. // ServingModeCallback returns a grpc.ServerOption which allows users to
  33. // register a callback to get notified about serving mode changes.
  34. func ServingModeCallback(cb ServingModeCallbackFunc) grpc.ServerOption {
  35. return &serverOption{apply: func(o *serverOptions) { o.modeCallback = cb }}
  36. }
  37. // ServingModeCallbackFunc is the callback that users can register to get
  38. // notified about the server's serving mode changes. The callback is invoked
  39. // with the address of the listener and its new mode.
  40. //
  41. // Users must not perform any blocking operations in this callback.
  42. type ServingModeCallbackFunc func(addr net.Addr, args ServingModeChangeArgs)
  43. // ServingModeChangeArgs wraps the arguments passed to the serving mode callback
  44. // function.
  45. type ServingModeChangeArgs struct {
  46. // Mode is the new serving mode of the server listener.
  47. Mode connectivity.ServingMode
  48. // Err is set to a non-nil error if the server has transitioned into
  49. // not-serving mode.
  50. Err error
  51. }
  52. // BootstrapContentsForTesting returns a grpc.ServerOption which allows users
  53. // to inject a bootstrap configuration used by only this server, instead of the
  54. // global configuration from the environment variables.
  55. //
  56. // # Testing Only
  57. //
  58. // This function should ONLY be used for testing and may not work with some
  59. // other features, including the CSDS service.
  60. //
  61. // # Experimental
  62. //
  63. // Notice: This API is EXPERIMENTAL and may be changed or removed in a
  64. // later release.
  65. func BootstrapContentsForTesting(contents []byte) grpc.ServerOption {
  66. return &serverOption{apply: func(o *serverOptions) { o.bootstrapContentsForTesting = contents }}
  67. }