github.com/wangyougui/gf/v2@v2.6.5/net/gsel/gsel.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/wangyougui/gf.
     6  
     7  // Package gsel provides selector definition and implements.
     8  package gsel
     9  
    10  import (
    11  	"context"
    12  
    13  	"github.com/wangyougui/gf/v2/net/gsvc"
    14  )
    15  
    16  // Builder creates and returns selector in runtime.
    17  type Builder interface {
    18  	Name() string
    19  	Build() Selector
    20  }
    21  
    22  // Selector for service balancer.
    23  type Selector interface {
    24  	// Pick selects and returns service.
    25  	Pick(ctx context.Context) (node Node, done DoneFunc, err error)
    26  
    27  	// Update updates services into Selector.
    28  	Update(ctx context.Context, nodes Nodes) error
    29  }
    30  
    31  // Node is node interface.
    32  type Node interface {
    33  	Service() gsvc.Service
    34  	Address() string
    35  }
    36  
    37  // Nodes contains multiple Node.
    38  type Nodes []Node
    39  
    40  // DoneFunc is callback function when RPC invoke done.
    41  type DoneFunc func(ctx context.Context, di DoneInfo)
    42  
    43  // DoneInfo contains additional information for done.
    44  type DoneInfo struct {
    45  	// Err is the rpc error the RPC finished with. It could be nil.
    46  	Err error
    47  
    48  	// Trailer contains the metadata from the RPC's trailer, if present.
    49  	Trailer DoneInfoMD
    50  
    51  	// BytesSent indicates if any bytes have been sent to the server.
    52  	BytesSent bool
    53  
    54  	// BytesReceived indicates if any byte has been received from the server.
    55  	BytesReceived bool
    56  
    57  	// ServerLoad is the load received from server. It's usually sent as part of
    58  	// trailing metadata.
    59  	//
    60  	// The only supported type now is *orca_v1.LoadReport.
    61  	ServerLoad interface{}
    62  }
    63  
    64  // DoneInfoMD is a mapping from metadata keys to value array.
    65  // Users should use the following two convenience functions New and Pairs to generate MD.
    66  type DoneInfoMD interface {
    67  	// Len returns the number of items in md.
    68  	Len() int
    69  
    70  	// Get obtains the values for a given key.
    71  	//
    72  	// k is converted to lowercase before searching in md.
    73  	Get(k string) []string
    74  
    75  	// Set sets the value of a given key with a slice of values.
    76  	//
    77  	// k is converted to lowercase before storing in md.
    78  	Set(key string, values ...string)
    79  
    80  	// Append adds the values to key k, not overwriting what was already stored at
    81  	// that key.
    82  	//
    83  	// k is converted to lowercase before storing in md.
    84  	Append(k string, values ...string)
    85  
    86  	// Delete removes the values for a given key k which is converted to lowercase
    87  	// before removing it from md.
    88  	Delete(k string)
    89  }
    90  
    91  // String formats and returns Nodes as string.
    92  func (ns Nodes) String() string {
    93  	var s string
    94  	for _, node := range ns {
    95  		if s != "" {
    96  			s += ","
    97  		}
    98  		s += node.Address()
    99  	}
   100  	return s
   101  }