Skip to content

Commit 35d2ec5

Browse files
committed
Network,Services,Utility: use Fsdk
Added fsdk package. Reuse functions from Fsdk.
1 parent c99a359 commit 35d2ec5

File tree

6 files changed

+33
-77
lines changed

6 files changed

+33
-77
lines changed

NOnion/NOnion.fsproj

+1
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
</ItemGroup>
8787

8888
<ItemGroup>
89+
<PackageReference Include="Fsdk" Version="0.6.0--date20230116-0113.git-2ef0522" />
8990
<PackageReference Include="Fsharpx.Collections" Version="3.0.1" />
9091
<PackageReference Include="Portable.BouncyCastle" Version="1.8.10" />
9192
<PackageReference Include="System.Memory" Version="4.5.4" />

NOnion/Network/TorGuard.fs

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ open System.Security.Authentication
99
open System.Security.Cryptography
1010
open System.Threading
1111

12+
open Fsdk
13+
1214
open NOnion
1315
open NOnion.Cells
1416
open NOnion.Utility

NOnion/Services/TorServiceHost.fs

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ open Org.BouncyCastle.Crypto.Parameters
1515
open Org.BouncyCastle.Crypto.Generators
1616
open Org.BouncyCastle.Crypto.Signers
1717
open Org.BouncyCastle.Security
18+
open Fsdk
1819

1920
open NOnion
2021
open NOnion.Cells.Relay

NOnion/Utility/FSharpUtil.fs

+25-77
Original file line numberDiff line numberDiff line change
@@ -4,93 +4,41 @@ open System
44
open System.Runtime.ExceptionServices
55

66
open FSharpx.Collections
7+
open Fsdk
78

89
open NOnion
910

1011
module FSharpUtil =
11-
//Implementation copied from https://github.com/nblockchain/geewallet/blob/master/src/GWallet.Backend/FSharpUtil.fs
12-
let ReRaise(ex: Exception) : Exception =
13-
(ExceptionDispatchInfo.Capture ex).Throw()
14-
failwith "Should be unreachable"
15-
ex
16-
17-
let rec public FindException<'T when 'T :> Exception>
18-
(ex: Exception)
19-
: Option<'T> =
20-
let rec findExInSeq(sq: seq<Exception>) =
21-
match Seq.tryHeadTail sq with
22-
| Some(head, tail) ->
23-
match FindException head with
24-
| Some ex -> Some ex
25-
| None -> findExInSeq <| tail
26-
| None -> None
27-
28-
if isNull ex then
29-
None
30-
else
31-
match ex with
32-
| :? 'T as specificEx -> Some specificEx
33-
| :? AggregateException as aggEx ->
34-
findExInSeq aggEx.InnerExceptions
35-
| _ -> FindException<'T> ex.InnerException
36-
37-
type private Either<'Val, 'Err when 'Err :> Exception> =
38-
| FailureResult of 'Err
39-
| SuccessfulValue of 'Val
40-
4112
let WithTimeout (timeSpan: TimeSpan) (job: Async<'R>) : Async<'R> =
4213
async {
43-
let read =
44-
async {
45-
let! value = job
46-
return value |> SuccessfulValue |> Some
47-
}
48-
49-
let delay =
50-
async {
51-
let total = int timeSpan.TotalMilliseconds
52-
do! Async.Sleep total
53-
return FailureResult <| TimeoutException() |> Some
54-
}
55-
56-
let! dummyOption = Async.Choice([ read; delay ])
14+
let! result = FSharpUtil.WithTimeout timeSpan job
5715

58-
match dummyOption with
59-
| Some theResult ->
60-
match theResult with
61-
| SuccessfulValue r -> return r
62-
| FailureResult _ -> return raise <| TimeoutErrorException()
63-
| None ->
64-
// none of the jobs passed to Async.Choice returns None
65-
return failwith "unreachable"
16+
match result with
17+
| Some value -> return value
18+
| None -> return raise <| TimeoutErrorException()
6619
}
6720

6821
let Retry<'TEx when 'TEx :> Exception>
6922
(jobToRetry: Async<unit>)
7023
(maxRetryCount: int)
7124
=
72-
let rec retryLoop(tryNumber: int) =
73-
async {
74-
try
75-
do! jobToRetry
76-
with
77-
| :? 'TEx as ex ->
78-
if tryNumber < maxRetryCount then
79-
return! retryLoop(tryNumber + 1)
80-
else
81-
sprintf
82-
"Maximum retry count reached, ex = %s"
83-
(ex.ToString())
84-
|> TorLogger.Log
85-
86-
return raise <| ReRaise ex
87-
| ex ->
88-
sprintf
89-
"Unexpected exception happened in the retry loop, ex = %s"
90-
(ex.ToString())
91-
|> TorLogger.Log
92-
93-
return raise <| ReRaise ex
94-
}
95-
96-
retryLoop 0
25+
async {
26+
try
27+
do!
28+
FSharpUtil.Retry<_, 'TEx>
29+
(fun () -> jobToRetry)
30+
maxRetryCount
31+
with
32+
| :? 'TEx as ex ->
33+
sprintf "Maximum retry count reached, ex = %s" (ex.ToString())
34+
|> TorLogger.Log
35+
36+
return raise <| FSharpUtil.ReRaise ex
37+
| ex ->
38+
sprintf
39+
"Unexpected exception happened in the retry loop, ex = %s"
40+
(ex.ToString())
41+
|> TorLogger.Log
42+
43+
return raise <| FSharpUtil.ReRaise ex
44+
}

NOnion/Utility/MailboxUtil.fs

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
open System.Net.Sockets
44

5+
open Fsdk
6+
57
open NOnion
68

79
module internal MailboxResultUtil =

NOnion/Utility/ResultUtil.fs

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
namespace NOnion.Utility
22

3+
open Fsdk
4+
35
//FIXME: for some reason FSharpUtil is in NOnion namespace instead of NOnion.Utility
46
open NOnion
57

0 commit comments

Comments
 (0)