Skip to main content

WG Add

Adds a delta counter to the specified wait group.

How It Works

  1. The node retrieves the Wait Group ID from the input variable
  2. The Delta value is retrieved from the input (default is 1)
  3. The node validates that Delta is not negative
  4. The wait group is looked up in the global wait groups map using the ID
  5. A mutex lock is acquired to ensure thread-safe access
  6. The delta value is added to both the internal counter and the sync.WaitGroup
  7. The mutex is released
  8. The message passes through unchanged

Requirements

  • A valid Wait Group ID must be provided (from WG Create node)
  • Delta value must be non-negative (0 or positive integers)
  • The wait group must exist (not yet deleted by WG Wait)

Error Handling

Error CodeDescriptionCause
Core.WaitGroup.Add.ErrOnCreateConfig parse errorInvalid node configuration during creation
Core.WaitGroup.Add.ErrOnMessageMessage parse errorInvalid message format received
Core.WaitGroup.Add.Err (Delta negative)Delta cannot be negativeDelta value is less than 0
Core.WaitGroup.Add.Err (WaitGroup not found)WaitGroup ID not foundThe specified Wait Group ID doesn't exist
Core.WaitGroup.Add.OutputMarshalOutput marshaling errorFailed to convert message to JSON

Common Properties

  • Name - The custom name of the node.
  • Color - The custom color of the node.
  • Delay Before (sec) - Waits in seconds before executing the node.
  • Delay After (sec) - Waits in seconds after executing node.
  • Continue On Error - Automation will continue regardless of any error. The default value is false.

Input

  • Wait Group ID - The ID of the wait group to add to.

Option

  • Delta - Delta to add to the counter of the specified wait group. Default value is 1.

Usage Examples

Example 1: Set Counter for Fixed Number of Branches

WG Create → msg.wgID
└─ WG Add (wgID, Delta: 5)
└─ Launch 5 Parallel Branches
└─ Each branch: Do Work → WG Done (wgID)
└─ WG Wait (wgID)

Add a delta of 5 to wait for 5 parallel operations.

Example 2: Dynamic Delta Based on Array Length

WG Create → msg.wgID
└─ Get Items → msg.items
└─ Function (calculate count: msg.itemCount = msg.items.length)
└─ WG Add (wgID, Delta: msg.itemCount)
└─ For Each (items)
└─ Process Item → WG Done (wgID)
└─ WG Wait (wgID)

Use a dynamic delta value based on the number of items to process.

Example 3: Incremental Addition

WG Create → msg.wgID
└─ Get Task Groups → msg.groups
└─ For Each (groups)
└─ WG Add (wgID, Delta: group.taskCount)
└─ Spawn group.taskCount parallel workers
└─ WG Wait (wgID) - Waits for sum of all taskCounts

Incrementally add to the wait group counter as you discover more parallel work.

Usage Notes

  • WG Add can be called multiple times on the same wait group - deltas accumulate
  • Always call WG Add before starting parallel operations to avoid race conditions
  • The delta value represents how many WG Done calls are needed to complete the wait group
  • A delta of 0 is valid but doesn't add any waiting requirement
  • WG Add is thread-safe and can be called from parallel branches
  • Each WG Done decrements the counter by 1

Tips

  • Call WG Add once with the total count before spawning parallel operations for clarity
  • For dynamic parallel work, calculate the total count first, then call WG Add once
  • Use message variables for delta values when the count is determined at runtime
  • Avoid calling WG Add from within parallel branches unless absolutely necessary (can lead to timing issues)
  • If you need to add more work after starting, you can call WG Add again before the new work starts
  • Remember: Total WG Done calls must equal the sum of all WG Add deltas
  • WG Create - Create a wait group before adding to it
  • WG Done - Decrement the counter after each parallel operation
  • WG Wait - Wait for the counter to reach zero