WG Add
Adds a delta counter to the specified wait group.
How It Works
- The node retrieves the Wait Group ID from the input variable
- The Delta value is retrieved from the input (default is 1)
- The node validates that Delta is not negative
- The wait group is looked up in the global wait groups map using the ID
- A mutex lock is acquired to ensure thread-safe access
- The delta value is added to both the internal counter and the sync.WaitGroup
- The mutex is released
- 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 Code | Description | Cause |
|---|---|---|
Core.WaitGroup.Add.ErrOnCreate | Config parse error | Invalid node configuration during creation |
Core.WaitGroup.Add.ErrOnMessage | Message parse error | Invalid message format received |
Core.WaitGroup.Add.Err (Delta negative) | Delta cannot be negative | Delta value is less than 0 |
Core.WaitGroup.Add.Err (WaitGroup not found) | WaitGroup ID not found | The specified Wait Group ID doesn't exist |
Core.WaitGroup.Add.OutputMarshal | Output marshaling error | Failed 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