gitee.com/ks-custle/core-gm@v0.0.0-20230922171213-b83bdd97b62c/grpc/xds/server_options.go (about)

     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  
    19  package xds
    20  
    21  import (
    22  	"net"
    23  
    24  	grpc "gitee.com/ks-custle/core-gm/grpc"
    25  	"gitee.com/ks-custle/core-gm/grpc/connectivity"
    26  )
    27  
    28  type serverOptions struct {
    29  	modeCallback      ServingModeCallbackFunc
    30  	bootstrapContents []byte
    31  }
    32  
    33  type serverOption struct {
    34  	grpc.EmptyServerOption
    35  	apply func(*serverOptions)
    36  }
    37  
    38  // ServingModeCallback returns a grpc.ServerOption which allows users to
    39  // register a callback to get notified about serving mode changes.
    40  func ServingModeCallback(cb ServingModeCallbackFunc) grpc.ServerOption {
    41  	return &serverOption{apply: func(o *serverOptions) { o.modeCallback = cb }}
    42  }
    43  
    44  // ServingModeCallbackFunc is the callback that users can register to get
    45  // notified about the server's serving mode changes. The callback is invoked
    46  // with the address of the listener and its new mode.
    47  //
    48  // Users must not perform any blocking operations in this callback.
    49  type ServingModeCallbackFunc func(addr net.Addr, args ServingModeChangeArgs)
    50  
    51  // ServingModeChangeArgs wraps the arguments passed to the serving mode callback
    52  // function.
    53  type ServingModeChangeArgs struct {
    54  	// Mode is the new serving mode of the server listener.
    55  	Mode connectivity.ServingMode
    56  	// Err is set to a non-nil error if the server has transitioned into
    57  	// not-serving mode.
    58  	Err error
    59  }
    60  
    61  // BootstrapContentsForTesting returns a grpc.ServerOption which allows users
    62  // to inject a bootstrap configuration used by only this server, instead of the
    63  // global configuration from the environment variables.
    64  //
    65  // # Testing Only
    66  //
    67  // This function should ONLY be used for testing and may not work with some
    68  // other features, including the CSDS service.
    69  //
    70  // # Experimental
    71  //
    72  // Notice: This API is EXPERIMENTAL and may be changed or removed in a
    73  // later release.
    74  func BootstrapContentsForTesting(contents []byte) grpc.ServerOption {
    75  	return &serverOption{apply: func(o *serverOptions) { o.bootstrapContents = contents }}
    76  }