Server Limits
Fine‑tune resource & safety thresholds using Set-KrServerLimit.
Prerequisites: basic hosting in Endpoints and route basics in Routes.
Full source
File: pwsh/tutorial/examples/13.1-Server-Limits.ps1
<#
13.1 Server Limits Example
Demonstrates setting server limits for request body size, concurrent connections, and other limits.
File: 13.1-Server-Limits.ps1
#>
param(
[int]$Port = 5000,
[IPAddress]$IPAddress = [IPAddress]::Loopback
)
# 1. (Optional) Logging pipeline so we see events
New-KrLogger |
Set-KrLoggerLevel -Value Debug |
Add-KrSinkConsole |
Register-KrLogger -Name 'console' -SetAsDefault | Out-Null
# 2. Create server host
New-KrServer -Name 'Server Limits'
# 3. Add loopback listener on port 5000 (auto unlinks existing file if present)
# This listener will be used to demonstrate server limits configuration.
Add-KrEndpoint -Port $Port -IPAddress $IPAddress
# 5. Set server limits for request body size, concurrent connections, and other limits
Set-KrServerLimit -MaxRequestBodySize 1048576 -MaxConcurrentConnections 1 -KeepAliveTimeoutSeconds 60 -MaxRequestHeadersTotalSize 16384
# 6. Finalize configuration and set server limits
Enable-KrConfiguration
# 7. Map a simple info route to demonstrate server limits in action
Add-KrMapRoute -Verbs Get -Pattern '/info' -ScriptBlock {
Start-Sleep 20
Write-KrJsonResponse @{ status = 'ok'; time = (Get-Date) }
}
# 8. Start the server (runs asynchronously; press Ctrl+C to stop)
Start-KrServer
Step-by-step
- Create server + listener.
- Add runtime.
- Apply limits (here: body size, headers size, keep-alive, and
MaxConcurrentConnections 1). - Enable configuration (commits listeners + runtime + limits).
- Map a long-running
/inforoute (includesStart-Sleep 20to simulate work). - Start server.
Common settings
| Setting | Why |
|---|---|
| MaxRequestBodySize | Reject large uploads early |
| MaxConcurrentConnections | Basic throttling |
| KeepAliveTimeoutSeconds | Trim idle sockets |
| MaxRequestHeadersTotalSize | Protect from header abuse |
| MinRequestBodyDataRate | Drop very slow clients |
Try it
Open two separate PowerShell terminals (or run jobs) and issue the request simultaneously to observe the single concurrent connection limit (second request ends early):
# Terminal 1 (holds the only allowed connection ~20s)
Invoke-WebRequest -Uri http://127.0.0.1:5000/info | Select-Object -ExpandProperty Content
# Quickly in Terminal 2 (while first still running)
Invoke-WebRequest -Uri http://127.0.0.1:5000/info
Expected:
- First call returns JSON after ~20 seconds.
- Second call fails fast with an error similar to:
Invoke-WebRequest: The response ended prematurely. (ResponseEnded)
(Exact wording may vary.) This demonstrates throttling via -MaxConcurrentConnections 1.
References
- Set-KrServerLimit
- New-KrServer
- Add-KrEndpoint
- Enable-KrConfiguration
- Add-KrMapRoute
- Write-KrJsonResponse
- Start-KrServer
Troubleshooting
| Symptom | Cause | Fix |
|---|---|---|
| Large upload rejected | Limit too small | Increase MaxRequestBodySize |
| Clients disconnect | Data rate limits strict | Relax Min*DataRate values |
| Slow responses | Too few connections | Raise MaxConcurrentConnections |
| Second request aborted | MaxConcurrentConnections set to 1 | Increase value or remove limit |
Previous / Next
Previous: Scheduling Report Next: Server Options