Skip to main content

Counter

The cloud.Counter resource represents a stateful container for one or more numbers in the cloud.

Usage

Defining a counter

bring cloud;

let counter = new cloud.Counter(
initial: 123, // optional, defaults to 0
);

Using a counter inflight

bring cloud;

let counter = new cloud.Counter();

let counterFunc = inflight () => {
let prev = counter.inc(); // increment by 1 and return previous value
counter.inc(5); // increment by 5
counter.dec(); // decrement by 1
counter.dec(2); // decrement by 2

assert(counter.peek() == 3); // check the current value

counter.set(100); // set to a specific value
};

new cloud.Function(counterFunc);

Using keys to manage multiple counter values

bring cloud;

let counter = new cloud.Counter(initial: 100);

let counterFunc = inflight () => {
let k1 = "key-1";
let k2 = "key-2";

counter.dec(1, k1); // decrement k1 by 1
counter.inc(11, k2); // increment k2 by 11

assert(counter.peek(k1) == 99); // check the current value of k1
assert(counter.peek(k2) == 111); // check the current value of k2
};

new cloud.Function(counterFunc);

Target-specific details

Simulator (sim)

Under the hood, the simulator stores the counter value in memory.

Note that counter data is not persisted between simulator runs.

AWS (tf-aws and awscdk)

The AWS implementation of cloud.Counter uses Amazon DynamoDB.

Azure (tf-azure)

🚧 Not supported yet (tracking issue: #629)

GCP (tf-gcp)

🚧 Not supported yet (tracking issue: #628)

API Reference

Counter

A distributed atomic counter.

Initializers

bring cloud;

new cloud.Counter(props?: CounterProps);
NameTypeDescription
propsCounterPropsNo description.

propsOptional

Methods

Inflight Methods
NameDescription
decDecrement the counter, returning the previous value.
incIncrements the counter atomically by a certain amount and returns the previous value.
peekGet the current value of the counter.
setSet a counter to a given value.

dec
inflight dec(amount?: num, key?: str): num

Decrement the counter, returning the previous value.

amountOptional
  • Type: num

amount to decrement (default is 1).


keyOptional
  • Type: str

specify the key to be decremented.


inc
inflight inc(amount?: num, key?: str): num

Increments the counter atomically by a certain amount and returns the previous value.

amountOptional
  • Type: num

amount to increment (default is 1).


keyOptional
  • Type: str

specify the key to be incremented.


peek
inflight peek(key?: str): num

Get the current value of the counter.

Using this API may introduce race conditions since the value can change between the time it is read and the time it is used in your code.

keyOptional
  • Type: str

specify the key to be retrieved.


set
inflight set(value: num, key?: str): void

Set a counter to a given value.

valueRequired
  • Type: num

new value.


keyOptional
  • Type: str

specify the key to be set.


Static Functions

NameDescription
onLiftTypeA hook called by the Wing compiler once for each inflight host that needs to use this type inflight.

onLiftType
bring cloud;

cloud.Counter.onLiftType(host: IInflightHost, ops: MutArray<str>);

A hook called by the Wing compiler once for each inflight host that needs to use this type inflight.

The list of requested inflight methods needed by the inflight host are given by ops.

This method is commonly used for adding permissions, environment variables, or other capabilities to the inflight host.

hostRequired

opsRequired
  • Type: MutArray<str>

Properties

NameTypeDescription
nodeconstructs.NodeThe tree node.
initialnumThe initial value of the counter.

nodeRequired
node: Node;
  • Type: constructs.Node

The tree node.


initialRequired
initial: num;
  • Type: num

The initial value of the counter.


Structs

CounterProps

Options for Counter.

Initializer

bring cloud;

let CounterProps = cloud.CounterProps{ ... };

Properties

NameTypeDescription
initialnumThe initial value of the counter.

initialOptional
initial: num;
  • Type: num
  • Default: 0

The initial value of the counter.