Skip to content

Commit 2ac2e0e

Browse files
authored
refactor: Remove pipeline abstraction (#144)
* refactor: Remove pipeline abstraction * Remove unnecessary code * Move exception to core * Formatting * Cleanup code * Update version * Bump major version
1 parent a521683 commit 2ac2e0e

File tree

13 files changed

+286
-379
lines changed

13 files changed

+286
-379
lines changed
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
namespace Oryx
55

6-
open Oryx.Pipeline
76

87
type RequestBuilder() =
98
member _.Zero() : HttpHandler<unit> = httpRequest
@@ -12,14 +11,14 @@ type RequestBuilder() =
1211
member _.Return(content: 'TResult) : HttpHandler<'TResult> = singleton content
1312
member _.ReturnFrom(req: HttpHandler<'TResult>) : HttpHandler<'TResult> = req
1413
member _.Delay(fn) = fn ()
15-
member _.Combine(source, other) = source |> Core.bind (fun _ -> other)
14+
member _.Combine(source, other) = source |> bind (fun _ -> other)
1615

1716
member _.For(source: 'TSource seq, func: 'TSource -> HttpHandler<'TResult>) : HttpHandler<'TResult list> =
1817
source |> Seq.map func |> sequential
1918

2019
/// Binds value of 'TValue for let! All handlers runs in same context within the builder.
2120
member _.Bind(source: HttpHandler<'TSource>, fn: 'TSource -> HttpHandler<'TResult>) : HttpHandler<'TResult> =
22-
source |> Core.bind fn
21+
source |> bind fn
2322

2423
[<AutoOpen>]
2524
module Builder =

src/Core.fs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2020 Cognite AS
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
namespace Oryx
5+
6+
open System.Threading.Tasks
7+
8+
type IHttpNext<'TSource> =
9+
abstract member OnSuccessAsync: ctx: HttpContext * content: 'TSource -> Task<unit>
10+
abstract member OnErrorAsync: ctx: HttpContext * error: exn -> Task<unit>
11+
abstract member OnCancelAsync: ctx: HttpContext -> Task<unit>
12+
13+
type HttpHandler<'TResult> = IHttpNext<'TResult> -> Task<unit>
14+
15+
exception HttpException of (HttpContext * exn) with
16+
override this.ToString() =
17+
match this :> exn with
18+
| HttpException(_, err) -> err.ToString()
19+
| _ -> failwith "This should not never happen."
Lines changed: 25 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
// Copyright 2020 Cognite AS
22
// SPDX-License-Identifier: Apache-2.0
3-
4-
namespace Oryx.Pipeline
3+
namespace Oryx
54

65
open System
76

87
open FSharp.Control.TaskBuilder
98
open Oryx
109

1110
module Error =
12-
/// Handler for protecting the pipeline from exceptions and protocol violations.
13-
let protect<'TContext, 'TSource> (source: Pipeline<'TContext, 'TSource>) : Pipeline<'TContext, 'TSource> =
11+
/// Handler for protecting the HttpHandler from exceptions and protocol violations.
12+
let protect<'TSource> (source: HttpHandler<'TSource>) : HttpHandler<'TSource> =
1413
fun next ->
1514
let mutable stopped = false
1615

17-
{ new IAsyncNext<'TContext, 'TSource> with
16+
{ new IHttpNext<'TSource> with
1817
member _.OnSuccessAsync(ctx, content) =
1918
task {
2019
match stopped with
@@ -47,12 +46,12 @@ module Error =
4746
|> source
4847

4948
/// Handler for catching errors and then delegating to the error handler on what to do.
50-
let catch<'TContext, 'TSource>
51-
(errorHandler: 'TContext -> exn -> Pipeline<'TContext, 'TSource>)
52-
(source: Pipeline<'TContext, 'TSource>)
53-
: Pipeline<'TContext, 'TSource> =
49+
let catch<'TSource>
50+
(errorHandler: HttpContext -> exn -> HttpHandler<'TSource>)
51+
(source: HttpHandler<'TSource>)
52+
: HttpHandler<'TSource> =
5453
fun next ->
55-
{ new IAsyncNext<'TContext, 'TSource> with
54+
{ new IHttpNext<'TSource> with
5655
member _.OnSuccessAsync(ctx, content) =
5756
task {
5857
try
@@ -66,7 +65,6 @@ module Error =
6665
match err with
6766
| PanicException error -> return! next.OnErrorAsync(ctx, error)
6867
| _ -> do! (errorHandler ctx err) next
69-
7068
}
7169

7270
member _.OnCancelAsync(ctx) = next.OnCancelAsync(ctx) }
@@ -79,22 +77,22 @@ module Error =
7977
| Error
8078
| Panic
8179

82-
/// Choose from a list of pipelines to use. The first middleware that succeeds will be used. Handlers will be
80+
/// Choose from a list of HttpHandlers to use. The first middleware that succeeds will be used. Handlers will be
8381
/// tried until one does not produce any error, or a `PanicException`.
84-
let choose<'TContext, 'TSource, 'TResult>
85-
(handlers: (Pipeline<'TContext, 'TSource> -> Pipeline<'TContext, 'TResult>) list)
86-
(source: Pipeline<'TContext, 'TSource>)
87-
: Pipeline<'TContext, 'TResult> =
82+
let choose<'TSource, 'TResult>
83+
(handlers: (HttpHandler<'TSource> -> HttpHandler<'TResult>) list)
84+
(source: HttpHandler<'TSource>)
85+
: HttpHandler<'TResult> =
8886
fun next ->
8987
let exns: ResizeArray<exn> = ResizeArray()
9088

91-
{ new IAsyncNext<'TContext, 'TSource> with
89+
{ new IHttpNext<'TSource> with
9290
member _.OnSuccessAsync(ctx, content) =
9391
let mutable state = ChooseState.Error
9492

9593
task {
9694
let obv =
97-
{ new IAsyncNext<'TContext, 'TResult> with
95+
{ new IHttpNext<'TResult> with
9896
member _.OnSuccessAsync(ctx, content) =
9997
task {
10098
exns.Clear() // Clear to avoid buildup of exceptions in streaming scenarios.
@@ -106,10 +104,10 @@ module Error =
106104
member _.OnErrorAsync(_, error) =
107105
task {
108106
match error, state with
109-
| PanicException(_), st when st <> ChooseState.Panic ->
107+
| PanicException _, st when st <> ChooseState.Panic ->
110108
state <- ChooseState.Panic
111109
return! next.OnErrorAsync(ctx, error)
112-
| SkipException(_), st when st = ChooseState.NoError ->
110+
| SkipException _, st when st = ChooseState.NoError ->
113111
// Flag error, but do not record skips.
114112
state <- ChooseState.Error
115113
| _, ChooseState.Panic ->
@@ -122,7 +120,7 @@ module Error =
122120

123121
member _.OnCancelAsync(ctx) = next.OnCancelAsync(ctx) }
124122

125-
/// Proces handlers until `NoError` or `Panic`.
123+
// Process handlers until `NoError` or `Panic`.
126124
for handler in handlers do
127125
if state = ChooseState.Error then
128126
state <- ChooseState.NoError
@@ -134,9 +132,9 @@ module Error =
134132
()
135133
| ChooseState.Error, exns when exns.Count > 1 ->
136134
return! next.OnErrorAsync(ctx, AggregateException(exns))
137-
| ChooseState.Error, exns when exns.Count = 1 -> return! next.OnErrorAsync(ctx, exns.[0])
135+
| ChooseState.Error, exns when exns.Count = 1 -> return! next.OnErrorAsync(ctx, exns[0])
138136
| ChooseState.Error, _ ->
139-
return! next.OnErrorAsync(ctx, SkipException "Choose: No hander matched")
137+
return! next.OnErrorAsync(ctx, SkipException "Choose: No handler matched")
140138
| ChooseState.NoError, _ -> ()
141139
}
142140

@@ -151,25 +149,19 @@ module Error =
151149
|> source
152150

153151
/// Error handler for forcing error. Use with e.g `req` computational expression if you need to "return" an error.
154-
let fail<'TContext, 'TSource, 'TResult>
155-
(err: Exception)
156-
(source: Pipeline<'TContext, 'TSource>)
157-
: Pipeline<'TContext, 'TResult> =
152+
let fail<'TSource, 'TResult> (err: Exception) (source: HttpHandler<'TSource>) : HttpHandler<'TResult> =
158153
fun next ->
159-
{ new IAsyncNext<'TContext, 'TSource> with
154+
{ new IHttpNext<'TSource> with
160155
member _.OnSuccessAsync(ctx, content) = next.OnErrorAsync(ctx, err)
161156
member _.OnErrorAsync(ctx, exn) = next.OnErrorAsync(ctx, exn)
162157
member _.OnCancelAsync(ctx) = next.OnCancelAsync(ctx) }
163158
|> source
164159

165160
/// Error handler for forcing a panic error. Use with e.g `req` computational expression if you need break out of
166161
/// the any error handling e.g `choose` or `catch`•.
167-
let panic<'TContext, 'TSource, 'TResult>
168-
(err: Exception)
169-
(source: Pipeline<'TContext, 'TSource>)
170-
: Pipeline<'TContext, 'TResult> =
162+
let panic<'TSource, 'TResult> (err: Exception) (source: HttpHandler<'TSource>) : HttpHandler<'TResult> =
171163
fun next ->
172-
{ new IAsyncNext<'TContext, 'TSource> with
164+
{ new IHttpNext<'TSource> with
173165
member _.OnSuccessAsync(ctx, content) =
174166
next.OnErrorAsync(ctx, PanicException(err))
175167

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ open System.Net.Http.Headers
1111
open System.Web
1212

1313
open FSharp.Control.TaskBuilder
14-
open Oryx.Pipeline
1514

1615
[<AutoOpen>]
1716
module Fetch =
@@ -101,7 +100,6 @@ module Fetch =
101100
response.Content
102101
)
103102

104-
105103
response.Dispose()
106104
return result
107105
with ex when not (ex :? HttpException) ->

0 commit comments

Comments
 (0)