Skip to content

Conversation

@haxtibal
Copy link
Contributor

The former implementation had 5 threads permanently spinning fast (10ms sleep) while waiting for a REST connection to process. This causes higher load in general and it breaks systems where Turn on PowerShell Script Block Logging policy is enabled, because then each PS statement including Start-Sleep is logged - resulting in 500 event log entries per second. It's a suggested setting in some hardening guidelines.

We can easily replace the Queue with a BlockingCollection backed by a ConcurrentQueue, which has the built-in feature to sleep until there are new items. Now the REST API threads consumes zero CPU time while waiting.

@cla-bot cla-bot bot added the cla/signed label Mar 18, 2022
@haxtibal haxtibal force-pushed the feature/avoid_busy_loop branch from 69c05c9 to 380c840 Compare March 18, 2022 17:02
@haxtibal haxtibal force-pushed the feature/avoid_busy_loop branch from 380c840 to e5f8589 Compare March 29, 2022 21:13
@haxtibal haxtibal changed the title WIP: Use a BlockingCollection to avoid busy loop in REST API threads Use a BlockingCollection to avoid busy loop in REST API threads Mar 29, 2022
@LordHepipud LordHepipud added this to the v1.9.0 milestone Mar 30, 2022
@LordHepipud
Copy link
Collaborator

Thank you for the PR! It looks great. Could you please update the PR with a changelog as bugfix for v1.9.0:

* [#497](https://github.com/Icinga/icinga-powershell-framework/pull/497) Fixes loop sleep for idle REST-Api threads by replacing them with [BlockingCollection](https://docs.microsoft.com/en-us/dotnet/api/system.collections.concurrent.blockingcollection-1?view=net-6.0) [ConcurrentQueue](https://docs.microsoft.com/en-us/dotnet/api/system.collections.concurrent.concurrentqueue-1?view=net-6.0)

I will then merge it immediately and publish it with v1.9.0

@LordHepipud
Copy link
Collaborator

One more addition. Could you please replace

$RESTThreadQueue = [System.Collections.Concurrent.BlockingCollection[PSObject]]::new(
            [System.Collections.Concurrent.ConcurrentQueue[PSObject]]::new()
        );

with

$RESTThreadQueue = New-Object System.Collections.Concurrent.BlockingCollection[PSObject] `
    -ArgumentList (New-Object System.Collections.Concurrent.ConcurrentQueue[PSObject]);

Otherwise the REST-Api will no longer work in Windows 2012 R2 or older.

The former implementation had 5 threads permanently spinning fast
(10ms sleep) while waiting for a REST connection to process.
This causes higher load in general and it breaks systems where
"Turn on PowerShell Script Block Logging policy" is enabled,
because then each PS statement including Start-Sleep is logged -
resulting in 500 event log entries per second. It's a suggested
setting in some hardening guidelines.

We can easily replace the Queue with a BlockingCollection backed
by a ConcurrentQueue, which has the built-in feature to sleep until
there are new items. Now the REST API threads consumes zero CPU time
while waiting.
@haxtibal haxtibal force-pushed the feature/avoid_busy_loop branch from e5f8589 to d215cfd Compare March 30, 2022 09:15
@haxtibal
Copy link
Contributor Author

Thanks, just applied your suggestions.

@LordHepipud
Copy link
Collaborator

Thanks!

@LordHepipud LordHepipud merged commit 6838a4f into Icinga:master Mar 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants