NewClusterClient creates a new Client instance. It needs one node address at least to discover the whole cluster.
(addresses []string, options ...ClusterClientOption)
| 795 | |
| 796 | // NewClusterClient creates a new Client instance. It needs one node address at least to discover the whole cluster. |
| 797 | func NewClusterClient(addresses []string, options ...ClusterClientOption) (*ClusterClient, error) { |
| 798 | if len(addresses) == 0 { |
| 799 | return nil, fmt.Errorf("addresses cannot be empty") |
| 800 | } |
| 801 | |
| 802 | var cc clusterClientConfig |
| 803 | for _, opt := range options { |
| 804 | opt(&cc) |
| 805 | } |
| 806 | |
| 807 | if cc.hasher == nil { |
| 808 | cc.hasher = hasher.NewDefaultHasher() |
| 809 | } |
| 810 | |
| 811 | if cc.logger == nil { |
| 812 | cc.logger = log.New(os.Stderr, "logger: ", log.Lshortfile) |
| 813 | } |
| 814 | |
| 815 | if cc.config == nil { |
| 816 | cc.config = config.NewClient() |
| 817 | } |
| 818 | |
| 819 | if cc.authentication != nil { |
| 820 | cc.config.Authentication = cc.authentication |
| 821 | } |
| 822 | |
| 823 | if cc.routingTableFetchInterval <= 0 { |
| 824 | cc.routingTableFetchInterval = DefaultRoutingTableFetchInterval |
| 825 | } |
| 826 | |
| 827 | if err := cc.config.Sanitize(); err != nil { |
| 828 | return nil, err |
| 829 | } |
| 830 | if err := cc.config.Validate(); err != nil { |
| 831 | return nil, err |
| 832 | } |
| 833 | |
| 834 | ctx, cancel := context.WithCancel(context.Background()) |
| 835 | cl := &ClusterClient{ |
| 836 | client: server.NewClient(cc.config), |
| 837 | config: &cc, |
| 838 | logger: cc.logger, |
| 839 | ctx: ctx, |
| 840 | cancel: cancel, |
| 841 | } |
| 842 | |
| 843 | // Initialize clients for the given cluster members. |
| 844 | for _, address := range addresses { |
| 845 | cl.client.Get(address) |
| 846 | } |
| 847 | |
| 848 | // Discover all cluster members |
| 849 | members, err := cl.Members(ctx) |
| 850 | if err != nil { |
| 851 | return nil, fmt.Errorf("error while discovering the cluster members: %w", err) |
| 852 | } |
| 853 | for _, member := range members { |
| 854 | cl.client.Get(member.Name) |