-
Notifications
You must be signed in to change notification settings - Fork 2
/
client.go
222 lines (193 loc) · 6.6 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
* MIT License
*
* Copyright (c) 2024 Arsene Tochemey Gandote
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package gokv
import (
"context"
nethttp "net/http"
"time"
"connectrpc.com/connect"
"go.uber.org/atomic"
"google.golang.org/protobuf/proto"
"github.com/tochemey/gokv/internal/http"
"github.com/tochemey/gokv/internal/internalpb"
"github.com/tochemey/gokv/internal/internalpb/internalpbconnect"
)
// 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
// host defines the host discoveryAddress
kvService internalpbconnect.KVServiceClient
connected *atomic.Bool
}
// Put distributes the key/value pair in the cluster
func (client *Client) Put(ctx context.Context, entry *Entry, expiration time.Duration) error {
if !client.connected.Load() {
return ErrClientNotConnected
}
_, err := client.kvService.Put(ctx, connect.NewRequest(
&internalpb.PutRequest{
Key: entry.Key,
Value: entry.Value,
Expiry: setExpiry(expiration),
}))
return err
}
// PutProto creates a key/value pair where the value is a proto message and distributes in the cluster
func (client *Client) PutProto(ctx context.Context, key string, value proto.Message, expiration time.Duration) error {
bytea, err := proto.Marshal(value)
if err != nil {
return err
}
entry := &Entry{Key: key, Value: bytea}
return client.Put(ctx, entry, expiration)
}
// PutString creates a key/value pair where the value is a string and distributes in the cluster
func (client *Client) PutString(ctx context.Context, key string, value string, expiration time.Duration) error {
entry := &Entry{Key: key, Value: []byte(value)}
return client.Put(ctx, entry, expiration)
}
// PutAny distributes the key/value pair in the cluster.
// A binary encoder is required to properly encode the value.
func (client *Client) PutAny(ctx context.Context, key string, value any, expiration time.Duration, codec Codec) error {
bytea, err := codec.Encode(value)
if err != nil {
return err
}
entry := &Entry{Key: key, Value: bytea}
return client.Put(ctx, entry, expiration)
}
// GetProto retrieves the value of the given from the cluster as protocol buffer message
// Prior to calling this method one must set a proto message as the value of the key
func (client *Client) GetProto(ctx context.Context, key string, dst proto.Message) error {
entry, err := client.Get(ctx, key)
if err != nil {
return err
}
return proto.Unmarshal(entry.Value, dst)
}
// GetString retrieves the value of the given from the cluster as a string
// Prior to calling this method one must set a string as the value of the key
func (client *Client) GetString(ctx context.Context, key string) (string, error) {
entry, err := client.Get(ctx, key)
if err != nil {
return "", err
}
return string(entry.Value), nil
}
// GetAny retrieves the value of the given from the cluster
// Prior to calling this method one must set a string as the value of the key
func (client *Client) GetAny(ctx context.Context, key string, codec Codec) (any, error) {
entry, err := client.Get(ctx, key)
if err != nil {
return nil, err
}
return codec.Decode(entry.Value)
}
// Get retrieves the value of the given key from the cluster
func (client *Client) Get(ctx context.Context, key string) (*Entry, error) {
if !client.connected.Load() {
return nil, ErrClientNotConnected
}
response, err := client.kvService.Get(ctx, connect.NewRequest(
&internalpb.GetRequest{
Key: key,
}))
if err != nil {
code := connect.CodeOf(err)
if code == connect.CodeNotFound {
return nil, ErrKeyNotFound
}
return nil, err
}
return fromNode(response.Msg.GetEntry()), nil
}
// List returns the list of entries at a point in time
func (client *Client) List(ctx context.Context) ([]*Entry, error) {
if !client.connected.Load() {
return nil, ErrClientNotConnected
}
response, err := client.kvService.List(ctx, connect.NewRequest(&internalpb.ListRequest{}))
if err != nil {
return nil, err
}
entries := make([]*Entry, 0, len(response.Msg.GetEntries()))
for _, entry := range response.Msg.GetEntries() {
entries = append(entries, fromNode(entry))
}
return entries, nil
}
// Delete deletes a given key from the cluster
// nolint
func (client *Client) Delete(ctx context.Context, key string) error {
if !client.connected.Load() {
return ErrClientNotConnected
}
_, err := client.kvService.Delete(ctx, connect.NewRequest(
&internalpb.DeleteRequest{
Key: key,
}))
return err
}
// Exists checks the existence of a given key in the cluster
func (client *Client) Exists(ctx context.Context, key string) (bool, error) {
if !client.connected.Load() {
return false, ErrClientNotConnected
}
response, err := client.kvService.KeyExists(ctx, connect.NewRequest(
&internalpb.KeyExistsRequest{
Key: key,
}))
if err != nil {
return false, err
}
return response.Msg.GetExists(), nil
}
// 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
}
client.connected.Store(false)
client.httpClient.CloseIdleConnections()
return nil
}
// 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,
http.URL(host, port),
// TODO: add observability options
)
return &Client{
httpClient: httpClient,
kvService: kvService,
connected: atomic.NewBool(true),
}
}