-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Program.fs
227 lines (190 loc) · 7.21 KB
/
Program.fs
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
open System
open Microsoft.AspNetCore.Builder
open Microsoft.AspNetCore.Hosting
open Microsoft.Extensions.Hosting
open Microsoft.Extensions.Logging
open Microsoft.Extensions.DependencyInjection
open Giraffe
module Database =
open Dapper
open System.Data
open System.Threading
open IcedTasks
let connStr =
Npgsql.NpgsqlConnectionStringBuilder(Host = "localhost", Port=5432, Username = "postgres", Password="postgres")
let doSlowWorkAsync (conn : IDbConnection) = async {
let! ct = Async.CancellationToken
let cmdDef = CommandDefinition("select pg_sleep(10)",cancellationToken = ct)
let! _ = conn.QueryAsync(cmdDef) |> Async.AwaitTask
()
}
let doSlowWork1 (conn : IDbConnection) = task {
let! _ = conn.QueryAsync("select pg_sleep(10)")
()
}
let doSlowWork2 (ct : CancellationToken) (conn : IDbConnection) = task {
let cmdDef = CommandDefinition("select pg_sleep(10)",cancellationToken = ct)
let! _ = conn.QueryAsync(cmdDef)
()
}
// CancellableTask<'T> = CancellationToken -> Task<'T>
let doSlowWork3 (conn : IDbConnection) = cancellableTask {
let! ct = CancellableTask.getCancellationToken ()
let cmdDef = CommandDefinition("select pg_sleep(10)",cancellationToken = ct)
let! _ = conn.QueryAsync(cmdDef)
()
}
open Database
let webApp =
choose [
// This is not using CancellationTokens
route "/simpleExample1"
>=> fun next ctx -> task {
use conn = new Npgsql.NpgsqlConnection(connStr.ToString())
do! doSlowWork1 (conn)
return! next ctx
}
// This is not using CancellationTokens explicitly
route "/simpleExample2"
>=> fun next ctx -> task {
use conn = new Npgsql.NpgsqlConnection(connStr.ToString())
do! doSlowWork2 ctx.RequestAborted conn
return! next ctx
}
// This is not using CancellationTokens explicitly
route "/simpleExample3"
>=> fun next ctx -> task {
use conn = new Npgsql.NpgsqlConnection(connStr.ToString())
do! doSlowWork3 conn ctx.RequestAborted
return! next ctx
}
]
module BusinessLogic =
open System.Threading.Tasks
open Microsoft.Extensions.Caching.Distributed
let preserveEndocrins (logger : ILogger) () = task {
do! Task.Delay(100)
}
let monotonectallyImplementErrorFreeConvergence (logger : ILogger) id =
task {
let! _ = preserveEndocrins logger ()
return id
}
let fungiblyFacilitateTechnicallySoundResults id conn = task {
let! _ = Database.doSlowWork1 conn
()
}
let completelyTransitionBackendRelationships () = task {
//send an email?
()
}
let continuallyActualizeImperatives (logger : ILogger) (conn) = task {
for i=0 to 100000 do
let! r1 = monotonectallyImplementErrorFreeConvergence logger i
let! _ = fungiblyFacilitateTechnicallySoundResults r1 conn
()
}
let reticulatingSplines (logger : ILogger) (caching : IDistributedCache) (conn) = task {
logger.LogDebug("Started reticulatingSplines")
let! _ = preserveEndocrins logger ()
logger.LogInformation("preserveEndocrins might be doing something strange?")
let! _ = Database.doSlowWork1 conn
let! _ = continuallyActualizeImperatives logger conn
let! _ = completelyTransitionBackendRelationships ()
()
}
module BusinessLogic2 =
open System.Threading
open System.Threading.Tasks
open Microsoft.Extensions.Caching.Distributed
let preserveEndocrins (logger : ILogger) (ct : CancellationToken) () = task {
do! Task.Delay(100)
}
let monotonectallyImplementErrorFreeConvergence (ct : CancellationToken) (logger : ILogger) id =
task {
let! _ = preserveEndocrins logger ct ()
return id
}
let fungiblyFacilitateTechnicallySoundResults id (ct : CancellationToken) conn = task {
let! _ = Database.doSlowWork2 ct conn
()
}
let completelyTransitionBackendRelationships (ct : CancellationToken) () = task {
//send an email?
()
}
let continuallyActualizeImperatives (logger : ILogger) (ct : CancellationToken) (conn) = task {
for i=0 to 100000 do
let! r1 = monotonectallyImplementErrorFreeConvergence ct logger i
let! _ = fungiblyFacilitateTechnicallySoundResults r1 ct conn
()
}
let reticulatingSplines (logger : ILogger) (caching : IDistributedCache) (ct : CancellationToken) (conn) = task {
logger.LogDebug("Started reticulatingSplines")
let! _ = preserveEndocrins logger ct ()
logger.LogInformation("preserveEndocrins might be doing something strange?")
let! _ = Database.doSlowWork2 ct conn
let! _ = continuallyActualizeImperatives logger ct conn
let! _ = completelyTransitionBackendRelationships ct ()
()
}
module BusinessLogic3 =
open System.Threading
open System.Threading.Tasks
open Microsoft.Extensions.Caching.Distributed
open IcedTasks
let preserveEndocrins (logger : ILogger) () = cancellableTask {
// You can bind against `CancellationToken -> Task<'T>` calls no need for `CancellableTask.getCancellationToken()`.
do! fun ct -> Task.Delay(100,ct)
}
let monotonectallyImplementErrorFreeConvergence (logger : ILogger) id =
cancellableTask {
let! _ = preserveEndocrins logger ()
return id
}
let fungiblyFacilitateTechnicallySoundResults id conn = cancellableTask {
let! _ = Database.doSlowWork3 conn
()
}
let completelyTransitionBackendRelationships () = cancellableTask {
//send an email?
()
}
let continuallyActualizeImperatives (logger : ILogger) (conn) = cancellableTask {
for i=0 to 100000 do
let! r1 = monotonectallyImplementErrorFreeConvergence logger i
let! _ = fungiblyFacilitateTechnicallySoundResults r1 conn
()
}
let reticulatingSplines (logger : ILogger) (caching : IDistributedCache) (conn) = cancellableTask {
logger.LogDebug("Started reticulatingSplines")
let! _ = preserveEndocrins logger ()
logger.LogInformation("preserveEndocrins might be doing something strange?")
let! _ = Database.doSlowWork3 conn
let! _ = continuallyActualizeImperatives logger conn
let! _ = completelyTransitionBackendRelationships ()
()
}
type Startup() =
member __.ConfigureServices(services: IServiceCollection) =
// Register default Giraffe dependencies
services.AddGiraffe()
|> ignore
member __.Configure
(app: IApplicationBuilder)
(env: IHostEnvironment)
(loggerFactory: ILoggerFactory)
=
// Add Giraffe to the ASP.NET Core pipeline
app.UseGiraffe webApp
[<EntryPoint>]
let main _ =
Host
.CreateDefaultBuilder()
.ConfigureWebHostDefaults(fun webHostBuilder ->
webHostBuilder.UseStartup<Startup>()
|> ignore
)
.Build()
.Run()
0