[DEPRECATED DOCUMENTATION] I will update the docs in the next few days. Until you can check the usage in incrementer_test.go
Distributed counter based on Couchbase.
Recommendation: Always try to use the AddSafe
, it's ensure the 100% the key will gets incremented.
Add
: Simple add plus one to the specified key, if the key has been locked it will return angocb.ErrTmpFail
AddSafe
: Solve the issue occured in theAdd
which cause thegocb.ErrTmpFail
Parameter list of non-persistent functions
conn string
: connection string to couchbase's clusterauth gocb.PasswordAuthenticator
: password auth for cocuhbase's clusterbucketName string
: name of the bucketbucketPassword string
: password of the bucketkey string
: key to incrementrollover uint64
: rollover of the keyinitial int64
: initial value of the keyinc uin64
: amount of increment
err := incrmntr.Add(
"couchbase://localhost",
gocb.PasswordAuthenticator{
Username: "Administrator",
Password: "password",
},
"increment",
"",
"test2",
999,
1,
1,
)
// handle error
#### Persistent connection
New
New(bucket *gocb.Bucket, rollover uint64, initial int64, inc uint64) (Incrmntr, error)
cluster
waits a previously setted up couchbase cluster connection.bucketName
waits the name of the bucketbucketPassword
waits the password of the bucketrollover
is the rollover limitinitial
is the initial value if the rollover happens put it back to that numberinc
amount of increment
Methods
Interface returned by the incrmntr.New
type Incrmntr interface {
Get(key string) (int64, error)
Add(key string) error
AddSafe(key string) error
AddWithRollover(key string, rollover uint64) error
AddSafeWithRollover(key string, rollover uint64) error
Close() error
}
Get
: Returns the current value of the keyAdd
: Simple add plus one to the specified key, if the key has been locked it will return angocb.ErrTmpFail
AddSafe
: Solve the issue occured in theAdd
which cause thegocb.ErrTmpFail
AddWithRollover
: Same as theAdd
with custom rolloverAddSafeWithRollover
: Same as theAddSafe
with custom rolloverClose
: Close the bucket
cluster, err := gocb.Connect("couchbase://localhost")
// handle error
cluster.Authenticate(gocb.PasswordAuthenticator{
Username: "Administrator",
Password: "password",
})
bucket, err := cluster.OpenBucket("increment", "")
if err != nil {
t.Error(err)
}
inc, err := incrmntr.New(bucket, 999, 1, 1)
// handle error
err := inc.Add("test")
// handle error
err := inc.AddSafe("test")
// handle error
There is a docker/docker-compose-single.yml
which represents a single couchbase server
The docker-compose.yml
has a cluster what the developer have to set up manually. The api server waits a req like that to trigger the test case:
curl -X GET \
'http://localhost:8890/trigger?amount=100&bucket=increment&conn=couchbase://cb2' \
-H 'content-type: application/x-www-form-urlencoded'