google.golang.org/grpc@v1.74.2/xds/internal/xdsclient/clientimpl_loadreport.go (about)

     1  /*
     2   *
     3   * Copyright 2019 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 xdsclient
    19  
    20  import (
    21  	"context"
    22  	"sync"
    23  
    24  	"google.golang.org/grpc/internal/xds/bootstrap"
    25  	"google.golang.org/grpc/xds/internal/clients"
    26  	"google.golang.org/grpc/xds/internal/clients/grpctransport"
    27  	"google.golang.org/grpc/xds/internal/clients/lrsclient"
    28  )
    29  
    30  // ReportLoad starts a load reporting stream to the given server. All load
    31  // reports to the same server share the LRS stream.
    32  //
    33  // It returns a lrsclient.LoadStore for the user to report loads.
    34  func (c *clientImpl) ReportLoad(server *bootstrap.ServerConfig) (*lrsclient.LoadStore, func(context.Context)) {
    35  	if c.lrsClient == nil {
    36  		lrsC, err := lrsclient.New(lrsclient.Config{
    37  			Node:             c.xdsClientConfig.Node,
    38  			TransportBuilder: c.xdsClientConfig.TransportBuilder,
    39  		})
    40  		if err != nil {
    41  			c.logger.Warningf("Failed to create an lrs client to the management server to report load: %v", server, err)
    42  			return nil, func(context.Context) {}
    43  		}
    44  		c.lrsClient = lrsC
    45  	}
    46  
    47  	load, err := c.lrsClient.ReportLoad(clients.ServerIdentifier{
    48  		ServerURI: server.ServerURI(),
    49  		Extensions: grpctransport.ServerIdentifierExtension{
    50  			ConfigName: server.SelectedCreds().Type,
    51  		},
    52  	})
    53  	if err != nil {
    54  		c.logger.Warningf("Failed to create a load store to the management server to report load: %v", server, err)
    55  		return nil, func(context.Context) {}
    56  	}
    57  	var loadStop sync.Once
    58  	return load, func(ctx context.Context) {
    59  		loadStop.Do(func() { load.Stop(ctx) })
    60  	}
    61  }