Skip to content
This repository was archived by the owner on May 26, 2026. It is now read-only.

Commit 7b355e9

Browse files
committed
add remaining wip
1 parent d2f23f0 commit 7b355e9

15 files changed

Lines changed: 202 additions & 429 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
default.etcd/
2+
/.vscode/
23
/config.yml
34
/ssl
45
/stream-api

client/apis.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,13 @@ type Field struct {
4040
}
4141

4242
type KVAPI interface {
43-
// Get(ctx context.Context, key string) ([]byte, error)
44-
// GetWithPrefix(ctx context.Context, prefix string) ([]Field, error)
43+
Get(ctx context.Context, key string) ([]byte, error)
44+
GetWithPrefix(ctx context.Context, prefix string) ([]Field, error)
4545
Put(ctx context.Context, key string, value []byte) error
4646
PutWithSession(ctx context.Context, key string, value []byte) error
4747
Delete(ctx context.Context, key string) error
4848
}
4949

50-
type KeepaliveAPI interface {
51-
RefreshLease(ctx context.Context, id LeaseID) error
52-
RevokeLease(ctx context.Context, id LeaseID) error
53-
}
54-
5550
type ServiceAPI interface {
5651
WatchAPI
5752
// PublishAPI

client/consul.go

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,14 @@ func (c *ConsulKV) Value() []byte {
8282

8383
// }
8484

85-
// helper method
86-
func optsWithTimeout(parentCtx context.Context, timeout time.Duration) (*api.WriteOptions, context.CancelFunc) {
85+
// helper methods
86+
func queryOptsWithTimeout(parentCtx context.Context, timeout time.Duration) (*api.QueryOptions, context.CancelFunc) {
87+
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
88+
opts := &api.QueryOptions{}
89+
return opts.WithContext(ctx), cancel
90+
}
91+
92+
func writeOptsWithTimeout(parentCtx context.Context, timeout time.Duration) (*api.WriteOptions, context.CancelFunc) {
8793
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
8894
opts := &api.WriteOptions{}
8995
return opts.WithContext(ctx), cancel
@@ -92,7 +98,7 @@ func optsWithTimeout(parentCtx context.Context, timeout time.Duration) (*api.Wri
9298
func (cc *ConsulClient) Close() {
9399
// destroy session
94100
session := cc.client.Session()
95-
opts, cancel := optsWithTimeout(context.Background(), time.Second)
101+
opts, cancel := writeOptsWithTimeout(context.Background(), time.Second)
96102
defer cancel()
97103
_, err := session.Destroy(cc.sessionId, opts)
98104
if err != nil {
@@ -190,12 +196,40 @@ func (cc *ConsulClient) makeWatchHandler(ch UpdateChan) watch.HybridHandlerFunc
190196
// kv put
191197
func (cc *ConsulClient) Put(ctx context.Context, key string, value []byte) error {
192198
p := &api.KVPair{Key: key, Value: value}
193-
opts, cancel := optsWithTimeout(ctx, time.Second)
199+
opts, cancel := writeOptsWithTimeout(ctx, time.Second)
194200
defer cancel()
195201
_, err := cc.client.KV().Put(p, opts)
196202
return err
197203
}
198204

205+
// kv put
206+
func (cc *ConsulClient) Get(ctx context.Context, key string) ([]byte, error) {
207+
opts, cancel := queryOptsWithTimeout(ctx, time.Second)
208+
defer cancel()
209+
res, _, err := cc.client.KV().Get(key, opts)
210+
if err != nil {
211+
return nil, err
212+
}
213+
return res.Value, nil
214+
}
215+
216+
func (cc *ConsulClient) GetWithPrefix(ctx context.Context, prefix string) ([]Field, error) {
217+
opts, cancel := queryOptsWithTimeout(ctx, time.Second)
218+
defer cancel()
219+
res, _, err := cc.client.KV().List(prefix, opts)
220+
if err != nil {
221+
return nil, err
222+
}
223+
var fields []Field
224+
for _, kv := range res {
225+
fields = append(fields, Field{
226+
Key: []byte(kv.Key),
227+
Value: kv.Value,
228+
})
229+
}
230+
return fields, nil
231+
}
232+
199233
// kv put with expiring session
200234
type ErrAlreadyAquired struct {
201235
Key string
@@ -207,7 +241,7 @@ func (e *ErrAlreadyAquired) Error() string {
207241

208242
func (cc *ConsulClient) PutWithSession(ctx context.Context, key string, value []byte) error {
209243
p := &api.KVPair{Key: key, Value: value, Session: cc.sessionId}
210-
opts, cancel := optsWithTimeout(ctx, time.Second)
244+
opts, cancel := writeOptsWithTimeout(ctx, time.Second)
211245
defer cancel()
212246
success, _, err := cc.client.KV().Acquire(p, opts)
213247
if !success {
@@ -223,7 +257,7 @@ func (cc *ConsulClient) PutWithSession(ctx context.Context, key string, value []
223257

224258
// kv delete with expiring session
225259
func (cc *ConsulClient) Delete(ctx context.Context, key string) error {
226-
opts, cancel := optsWithTimeout(ctx, time.Second)
260+
opts, cancel := writeOptsWithTimeout(ctx, time.Second)
227261
defer cancel()
228262
_, err := cc.client.KV().Delete(key, opts)
229263
if err != nil {

0 commit comments

Comments
 (0)