Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: make NewClient public #30

Merged
merged 2 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ go get github.com/tochemey/gokv
### Features

- Go-KV uses the push-pull anti-entropy method to replicate nodes' state across the cluster. This approach makes Go-KV eventually consistent.
One can set the [`stateSyncInterval`](./cluster/config.go) value to low for frequent state synchronisation at a network cost.
- Discovery API to implement custom nodes discovery provider. See: [Discovery API](./discovery/provider.go)
One can set the [`stateSyncInterval`](./cluster/config.go) value to low for frequent state synchronisation at a network cost.
- Discovery API to implement custom nodes discovery provider. See: [discovery](./discovery/provider.go)
- Comes bundled with some discovery providers that can help you hit the ground running:
- [kubernetes](https://kubernetes.io/docs/home/) [api integration](./discovery/kubernetes) is fully functional
- [nats](https://nats.io/) [integration](./discovery/nats) is fully functional
Expand Down
11 changes: 7 additions & 4 deletions cluster/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ import (
)

// Client defines the cluster client
// This client can connect to any Go-KV cluster node and retrieve data from other
// of the cluster.
type Client struct {
// http client
httpClient *nethttp.Client
Expand Down Expand Up @@ -150,8 +152,8 @@ func (client *Client) Exists(ctx context.Context, key string) (bool, error) {
return response.Msg.GetExists(), nil
}

// close closes the client connection to the cluster
func (client *Client) close() error {
// Close closes the client connection to the cluster
func (client *Client) Close() error {
// no-op when the client is not connected
if !client.connected.Load() {
return nil
Expand All @@ -161,8 +163,9 @@ func (client *Client) close() error {
return nil
}

// newClient creates an instance of the cluster Client
func newClient(host string, port int) *Client {
// NewClient creates an instance of the cluster Client
// host and port are a Go-KV cluster node host and port
func NewClient(host string, port int) *Client {
httpClient := http.NewClient()
kvService := internalpbconnect.NewKVServiceClient(
httpClient,
Expand Down
12 changes: 6 additions & 6 deletions cluster/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@
node.memberConfig.Events = &memberlist.ChannelEventDelegate{
Ch: eventsCh,
}
node.clusterClient = newClient(node.config.host, int(node.config.port))
node.clusterClient = NewClient(node.config.host, int(node.config.port))
node.started.Store(true)
node.mu.Unlock()

Expand Down Expand Up @@ -167,7 +167,7 @@

if err := errorschain.
New(errorschain.ReturnFirst()).
AddError(node.clusterClient.close()).
AddError(node.clusterClient.Close()).
AddError(node.memberlist.Leave(node.config.shutdownTimeout)).
AddError(node.config.provider.Deregister()).
AddError(node.config.provider.Close()).
Expand Down Expand Up @@ -266,8 +266,8 @@
return ch
}

// DiscoveryAddress returns the node discoveryAddress
func (node *Node) DiscoveryAddress() string {
// HostPort returns the node host:port address
func (node *Node) HostPort() string {
node.mu.Lock()
address := node.discoveryAddress
node.mu.Unlock()
Expand All @@ -285,7 +285,7 @@
if err != nil {
return nil, err
}
if member != nil && member.DiscoveryAddress() != node.DiscoveryAddress() {
if member != nil && member.DiscoveryAddress() != node.HostPort() {
members = append(members, member)
}
}
Expand Down Expand Up @@ -359,7 +359,7 @@
// skip this node
if event.Node == nil {
addr := net.JoinHostPort(event.Node.Addr.String(), strconv.Itoa(int(event.Node.Port)))
if addr == node.DiscoveryAddress() {
if addr == node.HostPort() {

Check warning on line 362 in cluster/node.go

View check run for this annotation

Codecov / codecov/patch

cluster/node.go#L362

Added line #L362 was not covered by tests
continue
}
}
Expand Down
6 changes: 3 additions & 3 deletions cluster/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,11 @@ L:
require.NotNil(t, event)
require.True(t, event.Type == NodeJoined)
actualAddr := event.Member.DiscoveryAddress()
require.Equal(t, node2.DiscoveryAddress(), actualAddr)
require.Equal(t, node2.HostPort(), actualAddr)
peers, err := node1.Peers()
require.NoError(t, err)
require.Len(t, peers, 1)
require.Equal(t, node2.DiscoveryAddress(), peers[0].DiscoveryAddress())
require.Equal(t, node2.HostPort(), peers[0].DiscoveryAddress())

// wait for some time
lib.Pause(time.Second)
Expand Down Expand Up @@ -179,7 +179,7 @@ L2:
require.NotNil(t, event)
require.True(t, event.Type == NodeLeft)
actualAddr = event.Member.DiscoveryAddress()
require.Equal(t, node2.DiscoveryAddress(), actualAddr)
require.Equal(t, node2.HostPort(), actualAddr)

t.Cleanup(func() {
assert.NoError(t, node1.Stop(ctx))
Expand Down