2021-10-26 03:30:57 -04:00
function Get-IcingaNextRESTApiThreadId ( )
{
2022-03-18 15:58:56 -04:00
# Improve our thread management by distributing new REST requests to a non-active thread
[ array ] $ConfiguredThreads = $Global:Icinga . Public . ThreadAliveHousekeeping . Keys ;
2024-03-29 08:22:16 -04:00
Write-IcingaDebugMessage -Message 'Distributing Icinga for Windows REST-Api calls to one of those threads' -Objects 'REST-Thread Ids' , ( $ConfiguredThreads | Out-String ) ;
2022-03-18 15:58:56 -04:00
foreach ( $thread in $ConfiguredThreads ) {
if ( $thread . ToLower ( ) -NotLike 'Start-IcingaForWindowsRESTThread::New-IcingaForWindowsRESTThread::CheckThread::*' ) {
continue ;
}
$ThreadConfig = $Global:Icinga . Public . ThreadAliveHousekeeping [ $thread ] ;
# If our thread is busy, skip this one and check for another one
if ( $ThreadConfig . Active ) {
continue ;
}
$ThreadIndex = $thread . Replace ( 'Start-IcingaForWindowsRESTThread::New-IcingaForWindowsRESTThread::CheckThread::' , '' ) ;
if ( Test-Numeric $ThreadIndex ) {
$Global:Icinga . Public . Daemons . RESTApi . LastThreadId = [ int ] $ThreadIndex ;
return ( [ int ] $ThreadIndex )
}
}
# In case we are not having any spare thread left, distribute the thread to the next thread in our list
2021-12-09 11:42:06 -05:00
[ int ] $ConcurrentThreads = $Global:Icinga . Public . Daemons . RESTApi . TotalThreads - 1 ;
[ int ] $LastThreadId = $Global:Icinga . Public . Daemons . RESTApi . LastThreadId + 1 ;
2021-10-26 03:30:57 -04:00
if ( $LastThreadId -gt $ConcurrentThreads ) {
$LastThreadId = 0 ;
}
2021-12-09 11:42:06 -05:00
$Global:Icinga . Public . Daemons . RESTApi . LastThreadId = $LastThreadId ;
2021-10-26 03:30:57 -04:00
return $LastThreadId ;
}