Pode icon indicating copy to clipboard operation
Pode copied to clipboard

Thread-Safe State Enhancement

Open mdaneri opened this issue 5 months ago • 0 comments

Description of the Change

This update introduces the option to use PodeState with thread safety by adding a new PodeOrderedConcurrentDictionary type. This new type supports an ordered hashtable with thread-safe operations, enhancing the use of shared state in Pode.

How to Enable

To enable thread-safe state management, use the Set-PodeState -Threadsafe command.

Examples

For examples demonstrating the use of thread-safe state with the new ordered hashtable, please refer to the examples\Shared-StateThreadSafe.ps1 file.

Additional Context

Please note the following important restriction when using the PodeOrderedConcurrentDictionary type:

  • When using the ordered hashtable (PodeOrderedConcurrentDictionary), the += operator does not work correctly if the key is named keys or values. This behavior occurs because these terms are reserved internally for accessing the dictionary's keys and values collections.

  • To work around this restriction, you should use the bracket ([]) notation instead of += when working with these reserved key names.

Examples:

# This will not work due to the reserved key name 'values'
$state:hash3.values += (Get-Random -Minimum 0 -Maximum 10)

# Correct approach using the bracket notation
$state:hash3['values'] += (Get-Random -Minimum 0 -Maximum 10)

# Using a non-reserved key name works as expected
$state:hash3.myValues += (Get-Random -Minimum 0 -Maximum 10)

These adjustments ensure that the new thread-safe ordered dictionary operates smoothly within Pode while maintaining compatibility with existing scripts.

mdaneri avatar Sep 10 '24 16:09 mdaneri