google.golang.org/grpc@v1.72.2/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  	"google.golang.org/grpc"
    25  	"google.golang.org/grpc/connectivity"
    26  	"google.golang.org/grpc/internal/xds/bootstrap"
    27  	"google.golang.org/grpc/xds/internal/xdsclient"
    28  )
    29  
    30  type serverOptions struct {
    31  	modeCallback         ServingModeCallbackFunc
    32  	clientPoolForTesting *xdsclient.Pool
    33  }
    34  
    35  type serverOption struct {
    36  	grpc.EmptyServerOption
    37  	apply func(*serverOptions)
    38  }
    39  
    40  // ServingModeCallback returns a grpc.ServerOption which allows users to
    41  // register a callback to get notified about serving mode changes.
    42  func ServingModeCallback(cb ServingModeCallbackFunc) grpc.ServerOption {
    43  	return &serverOption{apply: func(o *serverOptions) { o.modeCallback = cb }}
    44  }
    45  
    46  // ServingModeCallbackFunc is the callback that users can register to get
    47  // notified about the server's serving mode changes. The callback is invoked
    48  // with the address of the listener and its new mode.
    49  //
    50  // Users must not perform any blocking operations in this callback.
    51  type ServingModeCallbackFunc func(addr net.Addr, args ServingModeChangeArgs)
    52  
    53  // ServingModeChangeArgs wraps the arguments passed to the serving mode callback
    54  // function.
    55  type ServingModeChangeArgs struct {
    56  	// Mode is the new serving mode of the server listener.
    57  	Mode connectivity.ServingMode
    58  	// Err is set to a non-nil error if the server has transitioned into
    59  	// not-serving mode.
    60  	Err error
    61  }
    62  
    63  // BootstrapContentsForTesting returns a grpc.ServerOption which allows users
    64  // to inject a bootstrap configuration used by only this server, instead of the
    65  // global configuration from the environment variables.
    66  //
    67  // # Testing Only
    68  //
    69  // This function should ONLY be used for testing and may not work with some
    70  // other features, including the CSDS service.
    71  //
    72  // # Experimental
    73  //
    74  // Notice: This API is EXPERIMENTAL and may be changed or removed in a
    75  // later release.
    76  func BootstrapContentsForTesting(bootstrapContents []byte) grpc.ServerOption {
    77  	config, err := bootstrap.NewConfigFromContents(bootstrapContents)
    78  	if err != nil {
    79  		logger.Warningf("Failed to parse bootstrap contents %s for server options: %v", string(bootstrapContents), err)
    80  		return &serverOption{apply: func(o *serverOptions) { o.clientPoolForTesting = nil }}
    81  	}
    82  	return ClientPoolForTesting(xdsclient.NewPool(config))
    83  }
    84  
    85  // ClientPoolForTesting returns a grpc.ServerOption with the pool for xds
    86  // clients. It allows users to set a pool for xDS clients sharing the bootstrap
    87  // contents for this server.
    88  //
    89  // # Testing Only
    90  //
    91  // This function should ONLY be used for testing and may not work with some
    92  // other features, including the CSDS service.
    93  //
    94  // # Experimental
    95  //
    96  // Notice: This API is EXPERIMENTAL and may be changed or removed in a
    97  // later release.
    98  func ClientPoolForTesting(pool *xdsclient.Pool) grpc.ServerOption {
    99  	return &serverOption{apply: func(o *serverOptions) { o.clientPoolForTesting = pool }}
   100  }