dubbo.apache.org/dubbo-go/v3@v3.1.1/xds/client/controller/version/version.go (about)

     1  /*
     2   * Licensed to the Apache Software Foundation (ASF) under one or more
     3   * contributor license agreements.  See the NOTICE file distributed with
     4   * this work for additional information regarding copyright ownership.
     5   * The ASF licenses this file to You under the Apache License, Version 2.0
     6   * (the "License"); you may not use this file except in compliance with
     7   * the License.  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   *
    20   * Copyright 2021 gRPC authors.
    21   *
    22   */
    23  
    24  // Package version defines APIs to deal with different versions of xDS.
    25  package version
    26  
    27  import (
    28  	"context"
    29  	"time"
    30  )
    31  
    32  import (
    33  	dubbogoLogger "github.com/dubbogo/gost/log/logger"
    34  
    35  	"github.com/golang/protobuf/proto"
    36  	_struct "github.com/golang/protobuf/ptypes/struct"
    37  
    38  	"google.golang.org/grpc"
    39  
    40  	"google.golang.org/protobuf/types/known/anypb"
    41  )
    42  
    43  import (
    44  	"dubbo.apache.org/dubbo-go/v3/xds/client/load"
    45  	"dubbo.apache.org/dubbo-go/v3/xds/client/resource"
    46  	"dubbo.apache.org/dubbo-go/v3/xds/client/resource/version"
    47  )
    48  
    49  var (
    50  	m = make(map[version.TransportAPI]func(opts BuildOptions) (MetadataWrappedVersionClient, error))
    51  )
    52  
    53  // RegisterAPIClientBuilder registers a client builder for xDS transport protocol
    54  // version specified by b.Version().
    55  //
    56  // NOTE: this function must only be called during initialization time (i.e. in
    57  // an init() function), and is not thread-safe. If multiple builders are
    58  // registered for the same version, the one registered last will take effect.
    59  func RegisterAPIClientBuilder(v version.TransportAPI, f func(opts BuildOptions) (MetadataWrappedVersionClient, error)) {
    60  	m[v] = f
    61  }
    62  
    63  // GetAPIClientBuilder returns the client builder registered for the provided
    64  // xDS transport API version.
    65  func GetAPIClientBuilder(version version.TransportAPI) func(opts BuildOptions) (MetadataWrappedVersionClient, error) {
    66  	if f, ok := m[version]; ok {
    67  		return f
    68  	}
    69  	return nil
    70  }
    71  
    72  // BuildOptions contains options to be passed to client builders.
    73  type BuildOptions struct {
    74  	// NodeProto contains the Node proto to be used in xDS requests. The actual
    75  	// type depends on the transport protocol version used.
    76  	NodeProto proto.Message
    77  	// // Backoff returns the amount of time to backoff before retrying broken
    78  	// // streams.
    79  	// Backoff func(int) time.Duration
    80  	// Logger provides enhanced logging capabilities.
    81  	Logger dubbogoLogger.Logger
    82  }
    83  
    84  // LoadReportingOptions contains configuration knobs for reporting load data.
    85  type LoadReportingOptions struct {
    86  	LoadStore *load.Store
    87  }
    88  
    89  // ErrResourceTypeUnsupported is an error used to indicate an unsupported xDS
    90  // resource type. The wrapped ErrStr contains the details.
    91  type ErrResourceTypeUnsupported struct {
    92  	ErrStr string
    93  }
    94  
    95  // Error helps implements the error interface.
    96  func (e ErrResourceTypeUnsupported) Error() string {
    97  	return e.ErrStr
    98  }
    99  
   100  // VersionedClient is the interface to version specific operations of the
   101  // client.
   102  //
   103  // It mainly deals with the type assertion from proto.Message to the real v2/v3
   104  // types, and grpc.Stream to the versioned stream types.
   105  type VersionedClient interface {
   106  	// NewStream returns a new xDS client stream specific to the underlying
   107  	// transport protocol version.
   108  	NewStream(ctx context.Context, cc *grpc.ClientConn) (grpc.ClientStream, error)
   109  	// SendRequest constructs and sends out a DiscoveryRequest message specific
   110  	// to the underlying transport protocol version.
   111  	SendRequest(s grpc.ClientStream, resourceNames []string, rType resource.ResourceType, version, nonce, errMsg string) error
   112  	// RecvResponse uses the provided stream to receive a response specific to
   113  	// the underlying transport protocol version.
   114  	RecvResponse(s grpc.ClientStream) (proto.Message, error)
   115  	// ParseResponse type asserts message to the versioned response, and
   116  	// retrieves the fields.
   117  	ParseResponse(r proto.Message) (resource.ResourceType, []*anypb.Any, string, string, error)
   118  
   119  	// The following are LRS methods.
   120  
   121  	// NewLoadStatsStream returns a new LRS client stream specific to the
   122  	// underlying transport protocol version.
   123  	NewLoadStatsStream(ctx context.Context, cc *grpc.ClientConn) (grpc.ClientStream, error)
   124  	// SendFirstLoadStatsRequest constructs and sends the first request on the
   125  	// LRS stream.
   126  	SendFirstLoadStatsRequest(s grpc.ClientStream) error
   127  	// HandleLoadStatsResponse receives the first response from the server which
   128  	// contains the load reporting interval and the clusters for which the
   129  	// server asks the client to report load for.
   130  	//
   131  	// If the response sets SendAllClusters to true, the returned clusters is
   132  	// nil.
   133  	HandleLoadStatsResponse(s grpc.ClientStream) (clusters []string, _ time.Duration, _ error)
   134  	// SendLoadStatsRequest will be invoked at regular intervals to send load
   135  	// report with load data reported since the last time this method was
   136  	// invoked.
   137  	SendLoadStatsRequest(s grpc.ClientStream, loads []*load.Data) error
   138  }
   139  
   140  type MetadataWrappedVersionClient interface {
   141  	VersionedClient
   142  	SetMetadata(p *_struct.Struct)
   143  }