Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
nitely committed Sep 17, 2024
1 parent 80ac619 commit a76b74b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 47 deletions.
2 changes: 2 additions & 0 deletions src/hyperx/server.nim
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,8 @@ proc processClientHandler(
except HyperxError:
debugInfo getCurrentException().getStackTrace()
debugInfo getCurrentException().msg
when defined(hyperxStats):
echoStats client

proc serve*(
server: ServerContext,
Expand Down
65 changes: 18 additions & 47 deletions tests/functional/tserver.nim
Original file line number Diff line number Diff line change
Expand Up @@ -11,62 +11,33 @@ const certFile = getEnv "HYPERX_TEST_CERTFILE"
const keyFile = getEnv "HYPERX_TEST_KEYFILE"

proc processStream(strm: ClientStream) {.async.} =
with strm:
let data = new string
await strm.recvHeaders(data)
if "x-flow-control-check" in data[]:
# let recv buff for a bit
#debugEcho "sleeping"
await sleepAsync(10_000)
await strm.sendHeaders(
@[(":status", "200")], finish = false
)
let data = new string
await strm.recvHeaders(data)
if "x-flow-control-check" in data[]:
# let recv buff for a bit
#debugEcho "sleeping"
await sleepAsync(10_000)
await strm.sendHeaders(
@[(":status", "200")], finish = false
)
await strm.sendBody(data, finish = strm.recvEnded)
while not strm.recvEnded:
data[].setLen 0
await strm.recvBody(data)
await strm.sendBody(data, finish = strm.recvEnded)
while not strm.recvEnded:
data[].setLen 0
await strm.recvBody(data)
await strm.sendBody(data, finish = strm.recvEnded)
#GC_fullCollect()

proc processStreamHandler(strm: ClientStream) {.async.} =
try:
await processStream(strm)
except HyperxStrmError as err:
debugEcho err.msg
except HyperxConnError as err:
debugEcho err.msg

proc processClient(client: ClientContext) {.async.} =
with client:
while client.isConnected:
let strm = await client.recvStream()
asyncCheck processStreamHandler(strm)

proc processClientHandler(client: ClientContext) {.async.} =
try:
await processClient(client)
except HyperxConnError as err:
debugEcho err.msg
when defined(hyperxStats):
echoStats client
#GC_fullCollect()

proc serve*(server: ServerContext) {.async.} =
with server:
while server.isConnected:
let client = await server.recvClient()
asyncCheck processClientHandler(client)
await server.serve(processStream)

proc newServer(): ServerContext =
newServer(
proc main() {.async.} =
echo "Serving forever"
var server = newServer(
localHost, localPort, certFile, keyFile
)
await server.serve()

when isMainModule:
proc main() {.async.} =
echo "Serving forever"
var server = newServer()
await server.serve()
waitFor main()
doAssert not hasPendingOperations()
echo "ok"

0 comments on commit a76b74b

Please sign in to comment.