@@ -16,6 +16,7 @@ package counter
1616
1717import (
1818 "context"
19+
1920 api "github.com/atomix/api/proto/atomix/counter"
2021 "github.com/atomix/api/proto/atomix/headers"
2122 "github.com/atomix/go-client/pkg/client/primitive"
@@ -47,6 +48,9 @@ type Counter interface {
4748
4849 // Decrement decrements the counter by the given delta
4950 Decrement (ctx context.Context , delta int64 ) (int64 , error )
51+
52+ // CAS check the counter value and then updates its current value
53+ CAS (ctx context.Context , expect int64 , update int64 ) (bool , error )
5054}
5155
5256// New creates a new counter for the given partitions
@@ -149,6 +153,26 @@ func (c *counter) Decrement(ctx context.Context, delta int64) (int64, error) {
149153 return response .(* api.DecrementResponse ).NextValue , nil
150154}
151155
156+ func (c * counter ) CAS (ctx context.Context , expect int64 , update int64 ) (bool , error ) {
157+ response , err := c .instance .DoCommand (ctx , func (ctx context.Context , conn * grpc.ClientConn , header * headers.RequestHeader ) (* headers.ResponseHeader , interface {}, error ) {
158+ client := api .NewCounterServiceClient (conn )
159+ request := & api.CheckAndSetRequest {
160+ Header : header ,
161+ Expect : expect ,
162+ Update : update ,
163+ }
164+ response , err := client .CheckAndSet (ctx , request )
165+ if err != nil {
166+ return nil , nil , err
167+ }
168+ return response .Header , response , nil
169+ })
170+ if err != nil {
171+ return false , err
172+ }
173+ return response .(* api.CheckAndSetResponse ).Succeeded , nil
174+ }
175+
152176func (c * counter ) Close (ctx context.Context ) error {
153177 return c .instance .Close (ctx )
154178}
0 commit comments