forked from Badgerati/Pode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreading.ps1
More file actions
199 lines (155 loc) · 7.17 KB
/
Threading.ps1
File metadata and controls
199 lines (155 loc) · 7.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<#
.SYNOPSIS
A PowerShell script to set up a Pode server with various lock mechanisms including custom locks, mutexes, and semaphores.
.DESCRIPTION
This script sets up a Pode server that listens on a specified port and demonstrates the usage of lockables, mutexes, and semaphores for thread synchronization.
It includes routes that showcase the behavior of these synchronization mechanisms in different scopes (self, local, and global).
The server provides multiple routes to test custom locks, mutexes, and semaphores by simulating delays and concurrent access.
.EXAMPLE
To run the sample: ./Threading.ps1
Invoke-RestMethod -Uri http://localhost:8081/lock/custom/route1 -Method Get
Invoke-RestMethod -Uri http://localhost:8081/lock/custom/route2 -Method Get
Invoke-RestMethod -Uri http://localhost:8081/lock/global/route1 -Method Get
Invoke-RestMethod -Uri http://localhost:8081/lock/global/route2 -Method Get
Invoke-RestMethod -Uri http://localhost:8081/mutex/self/route1 -Method Get
Invoke-RestMethod -Uri http://localhost:8081/mutex/self/route2 -Method Get
Invoke-RestMethod -Uri http://localhost:8081/mutex/local/route1 -Method Get
Invoke-RestMethod -Uri http://localhost:8081/mutex/local/route2 -Method Get
Invoke-RestMethod -Uri http://localhost:8081/mutex/global/route1 -Method Get
Invoke-RestMethod -Uri http://localhost:8081/mutex/global/route2 -Method Get
Invoke-RestMethod -Uri http://localhost:8081/semaphore/self/route1 -Method Get
Invoke-RestMethod -Uri http://localhost:8081/semaphore/self/route2 -Method Get
Invoke-RestMethod -Uri http://localhost:8081/semaphore/local/route1 -Method Get
Invoke-RestMethod -Uri http://localhost:8081/semaphore/local/route2 -Method Get
Invoke-RestMethod -Uri http://localhost:8081/semaphore/global/route1 -Method Get
Invoke-RestMethod -Uri http://localhost:8081/semaphore/global/route2 -Method Get
.LINK
https://github.com/Badgerati/Pode/blob/develop/examples/Threading.ps1
.PARAMETER Port
The port number on which the Pode server will listen. Default is 8081.
.NOTES
Author: Pode Team
License: MIT License
#>
param(
[Parameter()]
[int]
$Port = 8081
)
try {
# Determine the script path and Pode module path
$ScriptPath = (Split-Path -Parent -Path $MyInvocation.MyCommand.Path)
$podePath = Split-Path -Parent -Path $ScriptPath
# Import the Pode module from the source path if it exists, otherwise from installed modules
if (Test-Path -Path "$($podePath)/src/Pode.psm1" -PathType Leaf) {
Import-Module "$($podePath)/src/Pode.psm1" -Force -ErrorAction Stop
}
else {
Import-Module -Name 'Pode' -MaximumVersion 2.99 -ErrorAction Stop
}
}
catch { throw }
# or just:
# Import-Module Pode
<#
# Demostrates Lockables, Mutexes, and Semaphores
#>
Start-PodeServer -Threads 2 {
Add-PodeEndpoint -Address localhost -Port $Port -Protocol Http
New-PodeLoggingMethod -Terminal | Enable-PodeErrorLogging
# custom locks
New-PodeLockable -Name 'TestLock'
Add-PodeRoute -Method Get -Path '/lock/custom/route1' -ScriptBlock {
Lock-PodeObject -Name 'TestLock' -ScriptBlock {
Start-Sleep -Seconds 10
}
Write-PodeJsonResponse -Value @{ Route = 1; Thread = $ThreadId }
}
Add-PodeRoute -Method Get -Path '/lock/custom/route2' -ScriptBlock {
Lock-PodeObject -Name 'TestLock' -ScriptBlock {}
Write-PodeJsonResponse -Value @{ Route = 2; Thread = $ThreadId }
}
# global locks
Add-PodeRoute -Method Get -Path '/lock/global/route1' -ScriptBlock {
Lock-PodeObject -ScriptBlock {
Start-Sleep -Seconds 10
}
Write-PodeJsonResponse -Value @{ Route = 1; Thread = $ThreadId }
}
Add-PodeRoute -Method Get -Path '/lock/global/route2' -ScriptBlock {
Get-PodeLockable -Name 'TestLock' | Lock-PodeObject -CheckGlobal -ScriptBlock {}
Write-PodeJsonResponse -Value @{ Route = 2; Thread = $ThreadId }
}
# self mutex
New-PodeMutex -Name 'SelfMutex'
Add-PodeRoute -Method Get -Path '/mutex/self/route1' -ScriptBlock {
Use-PodeMutex -Name 'SelfMutex' -ScriptBlock {
Start-Sleep -Seconds 10
}
Write-PodeJsonResponse -Value @{ Route = 1; Thread = $ThreadId }
}
Add-PodeRoute -Method Get -Path '/mutex/self/route2' -ScriptBlock {
Use-PodeMutex -Name 'SelfMutex' -ScriptBlock {}
Write-PodeJsonResponse -Value @{ Route = 2; Thread = $ThreadId }
}
# local mutex
New-PodeMutex -Name 'LocalMutex' -Scope Local
Add-PodeRoute -Method Get -Path '/mutex/local/route1' -ScriptBlock {
Use-PodeMutex -Name 'LocalMutex' -ScriptBlock {
Start-Sleep -Seconds 10
}
Write-PodeJsonResponse -Value @{ Route = 1; Thread = $ThreadId }
}
Add-PodeRoute -Method Get -Path '/mutex/local/route2' -ScriptBlock {
Use-PodeMutex -Name 'LocalMutex' -ScriptBlock {}
Write-PodeJsonResponse -Value @{ Route = 2; Thread = $ThreadId }
}
# global mutex
New-PodeMutex -Name 'GlobalMutex' -Scope Global
Add-PodeRoute -Method Get -Path '/mutex/global/route1' -ScriptBlock {
Use-PodeMutex -Name 'GlobalMutex' -ScriptBlock {
Start-Sleep -Seconds 10
}
Write-PodeJsonResponse -Value @{ Route = 1; Thread = $ThreadId }
}
Add-PodeRoute -Method Get -Path '/mutex/global/route2' -ScriptBlock {
Use-PodeMutex -Name 'GlobalMutex' -ScriptBlock {}
Write-PodeJsonResponse -Value @{ Route = 2; Thread = $ThreadId }
}
# self semaphore
New-PodeSemaphore -Name 'SelfSemaphore'
Add-PodeRoute -Method Get -Path '/semaphore/self/route1' -ScriptBlock {
Use-PodeSemaphore -Name 'SelfSemaphore' -ScriptBlock {
Start-Sleep -Seconds 10
}
Write-PodeJsonResponse -Value @{ Route = 1; Thread = $ThreadId }
}
Add-PodeRoute -Method Get -Path '/semaphore/self/route2' -ScriptBlock {
Use-PodeSemaphore -Name 'SelfSemaphore' -ScriptBlock {}
Write-PodeJsonResponse -Value @{ Route = 2; Thread = $ThreadId }
}
# local semaphore
New-PodeSemaphore -Name 'LocalSemaphore' -Scope Local
Add-PodeRoute -Method Get -Path '/semaphore/local/route1' -ScriptBlock {
Use-PodeSemaphore -Name 'LocalSemaphore' -ScriptBlock {
Start-Sleep -Seconds 10
}
Write-PodeJsonResponse -Value @{ Route = 1; Thread = $ThreadId }
}
Add-PodeRoute -Method Get -Path '/semaphore/local/route2' -ScriptBlock {
Use-PodeSemaphore -Name 'LocalSemaphore' -ScriptBlock {}
Write-PodeJsonResponse -Value @{ Route = 2; Thread = $ThreadId }
}
# global semaphore
New-PodeSemaphore -Name 'GlobalSemaphore' -Scope Global -Count 1
Add-PodeRoute -Method Get -Path '/semaphore/global/route1' -ScriptBlock {
Use-PodeSemaphore -Name 'GlobalSemaphore' -ScriptBlock {
Start-Sleep -Seconds 10
}
Write-PodeJsonResponse -Value @{ Route = 1; Thread = $ThreadId }
}
Add-PodeRoute -Method Get -Path '/semaphore/global/route2' -ScriptBlock {
Use-PodeSemaphore -Name 'GlobalSemaphore' -ScriptBlock {}
Write-PodeJsonResponse -Value @{ Route = 2; Thread = $ThreadId }
}
}