Skip to content

Commit

Permalink
Add inrement method for monotonous counter
Browse files Browse the repository at this point in the history
  • Loading branch information
illia-malachyn committed Apr 30, 2024
1 parent 542529f commit c6b01e6
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 9 deletions.
4 changes: 1 addition & 3 deletions access/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -1119,9 +1119,7 @@ func (h *Handler) SendAndSubscribeTransactionStatuses(
return subscription.HandleSubscription(sub, func(txResults []*TransactionResult) error {
for i := range txResults {
value := messageIndex.Value()
if ok := messageIndex.Set(value + 1); !ok {
return status.Errorf(codes.Internal, "the message index has already been incremented to %d", messageIndex.Value())
}
messageIndex.Increment()

err = stream.Send(&access.SendAndSubscribeTransactionStatusesResponse{
TransactionResults: TransactionResultToMessage(txResults[i]),
Expand Down
8 changes: 2 additions & 6 deletions engine/access/state_stream/backend/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,9 +298,7 @@ func (h *Handler) handleEventsResponse(send sendSubscribeEventsResponseFunc, hea
}

index := messageIndex.Value()
if ok := messageIndex.Set(index + 1); !ok {
return status.Errorf(codes.Internal, "message index already incremented to %d", messageIndex.Value())
}
messageIndex.Increment()

err = send(&executiondata.SubscribeEventsResponse{
BlockHeight: resp.Height,
Expand Down Expand Up @@ -415,9 +413,7 @@ func (h *Handler) handleAccountStatusesResponse(
}

index := messageIndex.Value()
if ok := messageIndex.Set(index + 1); !ok {
return status.Errorf(codes.Internal, "message index already incremented to %d", messageIndex.Value())
}
messageIndex.Increment()

err = send(&executiondata.SubscribeAccountStatusesResponse{
BlockId: convert.IdentifierToMessage(resp.BlockID),
Expand Down
10 changes: 10 additions & 0 deletions module/counters/monotonous_counter.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,13 @@ func (c *StrictMonotonousCounter) Set(newValue uint64) bool {
func (c *StrictMonotonousCounter) Value() uint64 {
return atomic.LoadUint64(&c.atomicCounter)
}

func (c *StrictMonotonousCounter) Increment() uint64 {
for {
oldValue := c.Value()
newValue := oldValue + 1
if atomic.CompareAndSwapUint64(&c.atomicCounter, oldValue, newValue) {
return newValue
}
}
}
6 changes: 6 additions & 0 deletions module/counters/monotonous_counter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ func TestSet(t *testing.T) {
require.Equal(t, uint64(4), counter.Value())
}

func TestIncrement(t *testing.T) {
counter := NewMonotonousCounter(1)
require.Equal(t, uint64(2), counter.Increment())
require.Equal(t, uint64(3), counter.Increment())
}

func TestFuzzy(t *testing.T) {
counter := NewMonotonousCounter(3)
require.True(t, counter.Set(4))
Expand Down

0 comments on commit c6b01e6

Please sign in to comment.