Improve cancellation support and code style

This commit is contained in:
Zahary Karadjov 2021-11-22 17:24:07 +02:00
parent 7ab1b31614
commit e4dba96f5c
No known key found for this signature in database
GPG Key ID: C8936F8A3073D609
2 changed files with 26 additions and 24 deletions

View File

@ -24,17 +24,18 @@ const
MaxHttpRequestSize = 128 * 1024 * 1024 # maximum size of HTTP body in octets
proc new(T: type RpcHttpClient, maxBodySize = MaxHttpRequestSize, secure = false): T =
if secure:
T(
maxBodySize: maxBodySize,
httpSession: HttpSessionRef.new(flags={HttpClientFlag.NoVerifyHost,
HttpClientFlag.NoVerifyServerName}),
)
let httpSessionFlags = if secure:
{
HttpClientFlag.NoVerifyHost,
HttpClientFlag.NoVerifyServerName
}
else:
T(
maxBodySize: maxBodySize,
httpSession: HttpSessionRef.new(),
)
{}
T(
maxBodySize: maxBodySize,
httpSession: HttpSessionRef.new(flags = httpSessionFlags)
)
proc newRpcHttpClient*(maxBodySize = MaxHttpRequestSize, secure = false): RpcHttpClient =
RpcHttpClient.new(maxBodySize, secure)
@ -55,8 +56,10 @@ method call*(client: RpcHttpClient, name: string,
res =
try:
await req.send()
except CatchableError as exc:
raise (ref RpcPostError)(msg: "Failed to send POST Request with JSON-RPC.", parent: exc)
except CancelledError as e:
raise e
except CatchableError as e:
raise (ref RpcPostError)(msg: "Failed to send POST Request with JSON-RPC.", parent: e)
if res.status < 200 or res.status >= 300: # res.status is not 2xx (success)
raise newException(ErrorResponse, "POST Response: " & $res.status)
@ -68,6 +71,8 @@ method call*(client: RpcHttpClient, name: string,
let resBytes =
try:
await res.getBodyBytes(client.maxBodySize)
except CancelledError as e:
raise e
except CatchableError as exc:
raise (ref FailedHttpResponse)(msg: "Failed to read POST Response for JSON-RPC.", parent: exc)
@ -101,18 +106,15 @@ proc connect*(client: RpcHttpClient, url: string)
if client.httpAddress.isErr:
raise newException(RpcAddressUnresolvableError, client.httpAddress.error)
proc connect*(client: RpcHttpClient, address: string, port: Port, secure=false) {.async.} =
var uri = initUri()
if secure:
uri.scheme = "https"
else:
uri.scheme = "http"
uri.hostname = address
uri.port = $port
proc connect*(client: RpcHttpClient, address: string, port: Port, secure: bool) {.async.} =
var uri = Uri(
scheme: if secure: "https" else: "http",
hostname: address,
port: $port)
let res = getAddress(client.httpSession, uri)
if res.isOk:
client.httpAddress = res
else:
raise newException(RpcAddressUnresolvableError, res.error)

View File

@ -6,7 +6,7 @@ const TestsCount = 100
proc simpleTest(address: string, port: Port): Future[bool] {.async.} =
var client = newRpcHttpClient()
await client.connect(address, port)
await client.connect(address, port, secure = false)
var r = await client.call("noParamsProc", %[])
if r.getStr == "Hello world":
result = true
@ -15,7 +15,7 @@ proc continuousTest(address: string, port: Port): Future[int] {.async.} =
var client = newRpcHttpClient()
result = 0
for i in 0..<TestsCount:
await client.connect(address, port)
await client.connect(address, port, secure = false)
var r = await client.call("myProc", %[%"abc", %[1, 2, 3, i]])
if r.getStr == "Hello abc data: [1, 2, 3, " & $i & "]":
result += 1
@ -23,7 +23,7 @@ proc continuousTest(address: string, port: Port): Future[int] {.async.} =
proc invalidTest(address: string, port: Port): Future[bool] {.async.} =
var client = newRpcHttpClient()
await client.connect(address, port)
await client.connect(address, port, secure = false)
var invalidA, invalidB: bool
try:
var r = await client.call("invalidProcA", %[])