..

ipfs-cluster中的client

1、作为 interface 的 client

type Client interface {
	// ID returns information about the cluster Peer.
	ID(context.Context) (*api.ID, error)

	// Peers requests ID information for all cluster peers.
	Peers(context.Context) ([]*api.ID, error)
	// PeerAdd adds a new peer to the cluster.
	PeerAdd(ctx context.Context, pid peer.ID) (*api.ID, error)
	// PeerRm removes a current peer from the cluster
	PeerRm(ctx context.Context, pid peer.ID) error

	// Add imports files to the cluster from the given paths.
	Add(ctx context.Context, paths []string, params *api.AddParams, out chan<- *api.AddedOutput) error
	// AddMultiFile imports new files from a MultiFileReader.
	AddMultiFile(ctx context.Context, multiFileR *files.MultiFileReader, params *api.AddParams, out chan<- *api.AddedOutput) error


	...
}

2、实现了 Client interface 的 NewLBClient, 同时进行了定制

type loadBalancingClient struct {
	strategy LBStrategy
	retries  int
}
func (lc *loadBalancingClient) Peers(ctx context.Context) ([]*api.ID, error) {
	var peers []*api.ID
	call := func(c Client) error {
		var err error
		peers, err = c.Peers(ctx)
		return err
	}

	err := lc.retry(0, call)
	return peers, err
}

3、defaultClient

// DefaultClient provides methods to interact with the ipfs-cluster API. Use
// NewDefaultClient() to create one.
type defaultClient struct {
	ctx       context.Context
	cancel    context.CancelFunc
	config    *Config
	transport *http.Transport
	net       string
	hostname  string
	client    *http.Client
	p2p       host.Host
}

4、创建client

var globalClient client.Client
globalClient, err = client.NewLBClient(&client.Failover{}, configs, retries)

func NewLBClient(strategy LBStrategy, cfgs []*Config, retries int) (Client, error) {
	fmt.Println("新建一个 load balance client.")

	var clients []Client
	for _, cfg := range cfgs {
		fmt.Println("client 的 config : ", cfg)

		defaultClient, err := NewDefaultClient(cfg)
		if err != nil {
			return nil, err
		}
		clients = append(clients, defaultClient)
	}
	strategy.SetClients(clients)
	return &loadBalancingClient{strategy: strategy, retries: retries}, nil
}

默认的config /ip4/127.0.0.1/tcp/9094


Nothing