Package org.apache.synapse.commons.throttle.core

# Class SharedParamManager

java.lang.Object
org.apache.synapse.commons.throttle.core.SharedParamManager

public class SharedParamManager extends Object

- ## Method Summary

  Modifier and Type
  Method
  Description
  `static long`
  `addAndGetDistributedCounter(String id, long value)`
  Add given value to the distribute counter of caller context of given id.
  `static long`
  `asyncGetAndAddDistributedCounter(String id, long value)`
  Asynchronously add given value to the distribute counter of caller context of given id.
  `static long`
  `asyncGetAndAlterDistributedCounter(String id, long value)`
  Asynchronously add given value to the distribute counter of caller context of given id.
  `static long`
  `getDistributedCounter(String id)`
  Return distributed shared counter for this caller context with given id.
  `static long`
  `getSharedTimestamp(String id)`
  Return hazelcast shared timestamp for this caller context with given id.
  `static long`
  `getTtl(String key)`
  Get the time-to-live value for the given key
  `static long[]`
  `getWindowState(String id)`
  Atomically reads the window timestamp and counter for the given caller id.
  `static long`
  `incrWindowCounter(String id, long delta, long expiryTime)`
  Atomically increments the window counter by `delta`.
  `static boolean`
  `lockSharedKeys(String callerContextId, String lockValue)`
  Acquire lock for the given callerContext (with the given value), so that another process cannot acquire the same lock
  `static void`
  `releaseSharedKeys(String callerContextId)`
  Release the lock of the given callerContext
  `static void`
  `releaseWindowLock(String id)`
  Releases the per-caller window lock acquired by `tryWindowLock(java.lang.String, long)`.
  `static void`
  `removeCounter(String id)`
  Destroy hazelcast global counter, if it's local then remove the map entry
  `static void`
  `removeTimestamp(String id)`
  Destroy hazelcast shared timggestamp counter, if it's local then remove the map entry
  `static void`
  `setDistributedCounter(String id, long value)`
  Set distribute counter of caller context of given id to the provided value.
  `static void`
  `setDistributedCounterWithExpiry(String id, long value, long expiryTime)`
  Set the distributed counter with the given id key with an expiry time
  `static void`
  `setExpiryTime(String id, long expiryTimeStamp)`
  `static void`
  `setSharedTimestamp(String id, long timestamp)`
  Set distribute timestamp of caller context of given id to the provided value.
  `static void`
  `setSharedTimestampWithExpiry(String id, long timestamp, long expiryTime)`
  Set the shared timestamp with the given id key with an expiry time
  `static void`
  `setWindow(String id, long count, long ts, long expiryTime)`
  Unconditionally sets a throttle window by overwriting both timestamp and counter, then refreshing the TTL.
  `static boolean`
  `tryWindowLock(String id, long expiryTime)`
  Tries to acquire the per-caller window lock without blocking.

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Method Details

  - ### getDistributedCounter

    public static long getDistributedCounter(String id)
    Return distributed shared counter for this caller context with given id. If it's not distributed will get from the local counter
    Parameters:
      `id` - of the shared counter
    Returns:
      shared hazelcast current shared counter

  - ### setDistributedCounter

    public static void setDistributedCounter(String id, long value)
    Set distribute counter of caller context of given id to the provided value. If it's not distributed do the same for local counter
    Parameters:
      `id` - of the caller context
      `value` - to set to the global counter

  - ### setDistributedCounterWithExpiry

    public static void setDistributedCounterWithExpiry(String id, long value, long expiryTime)
    Set the distributed counter with the given id key with an expiry time
    Parameters:
      `id` - key id
      `value` - value to set
      `expiryTime` - expiry time in milliseconds

  - ### setSharedTimestampWithExpiry

    public static void setSharedTimestampWithExpiry(String id, long timestamp, long expiryTime)
    Set the shared timestamp with the given id key with an expiry time
    Parameters:
      `id` - key id
      `timestamp` - timestamp value to set
      `expiryTime` - expiry time in milliseconds

  - ### addAndGetDistributedCounter

    public static long addAndGetDistributedCounter(String id, long value)
    Add given value to the distribute counter of caller context of given id. If it's not distributed return local counter
    Parameters:
      `id` - of the caller context
      `value` - to set to the global counter

  - ### asyncGetAndAddDistributedCounter

    public static long asyncGetAndAddDistributedCounter(String id, long value)
    Asynchronously add given value to the distribute counter of caller context of given id. If it's not distributed return local counter. This will return global value before add the provided counter
    Parameters:
      `id` - of the caller context
      `value` - to set to the global counter

  - ### asyncGetAndAlterDistributedCounter

    public static long asyncGetAndAlterDistributedCounter(String id, long value)
    Asynchronously add given value to the distribute counter of caller context of given id. If it's not distributed return local counter. This will return global value before add the provided counter
    Parameters:
      `id` - of the caller context
      `value` - to set to the global counter

  - ### removeCounter

    public static void removeCounter(String id)
    Destroy hazelcast global counter, if it's local then remove the map entry
    Parameters:
      `id` - of the caller context

  - ### getSharedTimestamp

    public static long getSharedTimestamp(String id)
    Return hazelcast shared timestamp for this caller context with given id. If it's not distributed will get from the local counter
    Parameters:
      `id` - of the shared counter
    Returns:
      shared hazelcast current shared counter

  - ### setSharedTimestamp

    public static void setSharedTimestamp(String id, long timestamp)
    Set distribute timestamp of caller context of given id to the provided value. If it's not distributed do the same for local counter
    Parameters:
      `id` - of the caller context
      `timestamp` - to set to the global counter

  - ### removeTimestamp

    public static void removeTimestamp(String id)
    Destroy hazelcast shared timggestamp counter, if it's local then remove the map entry
    Parameters:
      `id` - of the caller context

  - ### setExpiryTime

    public static void setExpiryTime(String id, long expiryTimeStamp)

  - ### getTtl

    public static long getTtl(String key)
    Get the time-to-live value for the given key
    Parameters:
      `key` - name key of the key
    Returns:
      time-to-live value

  - ### lockSharedKeys

    public static boolean lockSharedKeys(String callerContextId, String lockValue)
    Acquire lock for the given callerContext (with the given value), so that another process cannot acquire the same lock
    Returns:
      true if lock acquired, false if lock is not acquired within the configured timeout period

  - ### releaseSharedKeys

    public static void releaseSharedKeys(String callerContextId)
    Release the lock of the given callerContext

  - ### getWindowState

    public static long[] getWindowState(String id)
    Atomically reads the window timestamp and counter for the given caller id.
    Parameters:
      `id` - caller context id
    Returns:
      long[2]: {timestamp, counter}

  - ### setWindow

    public static void setWindow(String id, long count, long ts, long expiryTime)
    Unconditionally sets a throttle window by overwriting both timestamp and counter, then refreshing the TTL. Must be called while holding the per-caller window lock.
    Parameters:
      `id` - caller context id
      `count` - initial counter value
      `ts` - window first-access time in milliseconds
      `expiryTime` - absolute expiry timestamp in milliseconds (ts + unitTime)

  - ### incrWindowCounter

    public static long incrWindowCounter(String id, long delta, long expiryTime)
    Atomically increments the window counter by `delta`.
    Parameters:
      `id` - caller context id
      `delta` - value to add to the counter
    Returns:
      new global counter value after the increment

  - ### tryWindowLock

    public static boolean tryWindowLock(String id, long expiryTime)
    Tries to acquire the per-caller window lock without blocking. Returns `true` if the lock was acquired, `false` if another node holds it. The lock TTL is set to `expiryTime` (absolute window boundary) so it auto-expires when the window ends, even if the holder crashes mid-tick.
    Parameters:
      `id` - caller context id
      `expiryTime` - absolute window-end timestamp in ms (localFAT + unitTime)
    Returns:
      true if this node acquired the lock

  - ### releaseWindowLock

    public static void releaseWindowLock(String id)
    Releases the per-caller window lock acquired by `tryWindowLock(java.lang.String, long)`. Should be called in a `finally` block after lock-protected Redis work completes.
    Parameters:
      `id` - caller context id