Skip to content

Commit

Permalink
Network,Services,Utility: use Fsdk
Browse files Browse the repository at this point in the history
Added fsdk package. Reuse functions from Fsdk.
Set FSHarp.Core version to 6.0.1 to match the one used in Fsdk.
  • Loading branch information
webwarrior-ws committed Jan 16, 2023
1 parent c99a359 commit f9d5866
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 77 deletions.
5 changes: 5 additions & 0 deletions NOnion/NOnion.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Fsdk" Version="0.6.0--date20230115-0201.git-2550578" />
<PackageReference Include="Fsharpx.Collections" Version="3.0.1" />
<PackageReference Include="Portable.BouncyCastle" Version="1.8.10" />
<PackageReference Include="System.Memory" Version="4.5.4" />
Expand All @@ -97,6 +98,10 @@
<ProjectReference Include="..\Chaos.NaCl\Chaos.NaCl\Chaos.NaCl-Portable.csproj" PrivateAssets="all" />
</ItemGroup>

<ItemGroup>
<PackageReference Update="FSharp.Core" Version="6.0.1" />
</ItemGroup>

<Target Name="CopyProjectReferencesToPackage" DependsOnTargets="ResolveReferences">
<ItemGroup>
<BuildOutputInPackage Include="@(ReferenceCopyLocalPaths-&gt;WithMetadataValue('ReferenceSourceTarget', 'ProjectReference'))" />
Expand Down
2 changes: 2 additions & 0 deletions NOnion/Network/TorGuard.fs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ open System.Security.Authentication
open System.Security.Cryptography
open System.Threading

open Fsdk

open NOnion
open NOnion.Cells
open NOnion.Utility
Expand Down
1 change: 1 addition & 0 deletions NOnion/Services/TorServiceHost.fs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ open Org.BouncyCastle.Crypto.Parameters
open Org.BouncyCastle.Crypto.Generators
open Org.BouncyCastle.Crypto.Signers
open Org.BouncyCastle.Security
open Fsdk

open NOnion
open NOnion.Cells.Relay
Expand Down
102 changes: 25 additions & 77 deletions NOnion/Utility/FSharpUtil.fs
Original file line number Diff line number Diff line change
Expand Up @@ -4,93 +4,41 @@ open System
open System.Runtime.ExceptionServices

open FSharpx.Collections
open Fsdk

open NOnion

module FSharpUtil =
//Implementation copied from https://github.com/nblockchain/geewallet/blob/master/src/GWallet.Backend/FSharpUtil.fs
let ReRaise(ex: Exception) : Exception =
(ExceptionDispatchInfo.Capture ex).Throw()
failwith "Should be unreachable"
ex

let rec public FindException<'T when 'T :> Exception>
(ex: Exception)
: Option<'T> =
let rec findExInSeq(sq: seq<Exception>) =
match Seq.tryHeadTail sq with
| Some(head, tail) ->
match FindException head with
| Some ex -> Some ex
| None -> findExInSeq <| tail
| None -> None

if isNull ex then
None
else
match ex with
| :? 'T as specificEx -> Some specificEx
| :? AggregateException as aggEx ->
findExInSeq aggEx.InnerExceptions
| _ -> FindException<'T> ex.InnerException

type private Either<'Val, 'Err when 'Err :> Exception> =
| FailureResult of 'Err
| SuccessfulValue of 'Val

let WithTimeout (timeSpan: TimeSpan) (job: Async<'R>) : Async<'R> =
async {
let read =
async {
let! value = job
return value |> SuccessfulValue |> Some
}

let delay =
async {
let total = int timeSpan.TotalMilliseconds
do! Async.Sleep total
return FailureResult <| TimeoutException() |> Some
}

let! dummyOption = Async.Choice([ read; delay ])
let! result = FSharpUtil.WithTimeout timeSpan job

match dummyOption with
| Some theResult ->
match theResult with
| SuccessfulValue r -> return r
| FailureResult _ -> return raise <| TimeoutErrorException()
| None ->
// none of the jobs passed to Async.Choice returns None
return failwith "unreachable"
match result with
| Some value -> return value
| None -> return raise <| TimeoutErrorException()
}

let Retry<'TEx when 'TEx :> Exception>
(jobToRetry: Async<unit>)
(maxRetryCount: int)
=
let rec retryLoop(tryNumber: int) =
async {
try
do! jobToRetry
with
| :? 'TEx as ex ->
if tryNumber < maxRetryCount then
return! retryLoop(tryNumber + 1)
else
sprintf
"Maximum retry count reached, ex = %s"
(ex.ToString())
|> TorLogger.Log

return raise <| ReRaise ex
| ex ->
sprintf
"Unexpected exception happened in the retry loop, ex = %s"
(ex.ToString())
|> TorLogger.Log

return raise <| ReRaise ex
}

retryLoop 0
async {
try
do!
FSharpUtil.Retry<_, 'TEx>
(fun () -> jobToRetry)
maxRetryCount
with
| :? 'TEx as ex ->
sprintf "Maximum retry count reached, ex = %s" (ex.ToString())
|> TorLogger.Log

return raise <| FSharpUtil.ReRaise ex
| ex ->
sprintf
"Unexpected exception happened in the retry loop, ex = %s"
(ex.ToString())
|> TorLogger.Log

return raise <| FSharpUtil.ReRaise ex
}
2 changes: 2 additions & 0 deletions NOnion/Utility/MailboxUtil.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

open System.Net.Sockets

open Fsdk

open NOnion

module internal MailboxResultUtil =
Expand Down
2 changes: 2 additions & 0 deletions NOnion/Utility/ResultUtil.fs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
namespace NOnion.Utility

open Fsdk

//FIXME: for some reason FSharpUtil is in NOnion namespace instead of NOnion.Utility
open NOnion

Expand Down

0 comments on commit f9d5866

Please sign in to comment.