Skip to content

Commit 2cafc21

Browse files
committed
[core] Produce run number from Consul with kv.CAS API
1 parent 0fe7622 commit 2cafc21

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

configuration/consulsource.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ package configuration
2626

2727
import (
2828
"github.com/hashicorp/consul/api"
29+
"errors"
30+
"strconv"
2931
"strings"
3032
"gopkg.in/yaml.v2"
3133
)
@@ -49,6 +51,28 @@ func newConsulSource(uri string) (cc *ConsulSource, err error) {
4951
return
5052
}
5153

54+
func (cc *ConsulSource) GetNextUInt64(key string) (value uint64, err error) {
55+
kvp, _, err := cc.kv.Get(formatKey(key), &api.QueryOptions{RequireConsistent: true})
56+
if err != nil {
57+
return
58+
}
59+
value, err = strconv.ParseUint(string(kvp.Value[:]), 10, 64)
60+
if err != nil {
61+
return
62+
}
63+
value++
64+
kvp.Value = []byte(strconv.FormatUint(value, 10))
65+
var ok bool
66+
ok, _, err = cc.kv.CAS(kvp, nil) // Check-And-Set call, relies on ModifyIndex in KVPair
67+
if err != nil {
68+
return
69+
}
70+
if !ok {
71+
err = errors.New("cannot write back incremented CAS key")
72+
}
73+
return
74+
}
75+
5276
func (cc *ConsulSource) Get(key string) (value string, err error) {
5377
kvp, _, err := cc.kv.Get(formatKey(key), nil)
5478
if err != nil {

0 commit comments

Comments
 (0)