github.com/nyan233/littlerpc@v0.4.6-0.20230316182519-0c8d5c48abaf/core/client/loadbalance/default_resolver.go (about)

     1  package loadbalance
     2  
     3  import (
     4  	"io"
     5  	"net/http"
     6  	"os"
     7  	"strings"
     8  )
     9  
    10  func DefaultFileResolver(path string) ResolverFunc {
    11  	return func() ([]RpcNode, error) {
    12  		fileData, err := os.ReadFile(path)
    13  		if err != nil {
    14  			return nil, err
    15  		}
    16  		nodeAddrs := strings.Split(string(fileData), "\n")
    17  		nodes := make([]RpcNode, 0, len(nodeAddrs))
    18  		for _, nodeAddr := range nodeAddrs {
    19  			nodes = append(nodes, RpcNode{Address: nodeAddr})
    20  		}
    21  		return nodes, nil
    22  	}
    23  }
    24  
    25  func DefaultHttpResolver(url string) ResolverFunc {
    26  	return func() ([]RpcNode, error) {
    27  		response, err := http.Get(url)
    28  		if err != nil {
    29  			return nil, err
    30  		}
    31  		defer response.Body.Close()
    32  		bytes, err := io.ReadAll(response.Body)
    33  		if err != nil {
    34  			return nil, err
    35  		}
    36  		nodeAddrs := strings.Split(string(bytes), "\n")
    37  		nodes := make([]RpcNode, 0, len(nodeAddrs))
    38  		for _, nodeAddr := range nodeAddrs {
    39  			nodes = append(nodes, RpcNode{Address: nodeAddr})
    40  		}
    41  		return nodes, nil
    42  	}
    43  }
    44  
    45  func DefaultLiveResolver(splitAddr string) ResolverFunc {
    46  	return func() ([]RpcNode, error) {
    47  		nodeAddrs := strings.Split(splitAddr, ";")
    48  		nodes := make([]RpcNode, 0, len(nodeAddrs))
    49  		for _, nodeAddr := range nodeAddrs {
    50  			nodes = append(nodes, RpcNode{Address: nodeAddr})
    51  		}
    52  		return nodes, nil
    53  	}
    54  }