Reuse HTTP session for HTTP store transactions

This commit is contained in:
Ehmry - 2019-02-04 14:54:42 +01:00
parent 319432aad9
commit 52c1879630
1 changed files with 13 additions and 6 deletions

View File

@ -20,11 +20,12 @@ type
HttpStore* = ref HttpStoreObj
HttpStoreObj = object of BlobStoreObj
client: AsyncHttpClient
# TODO: keep a future that completes after the current client transaction completes,
# chain new streams that use the same client connection to this future
url: Uri
proc httpBlobClose(s: BlobStream) =
var s = (HttpBlobStream)s
close s.client
proc httpBlobClose(s: BlobStream) = discard
proc setPosHttp(s: BlobStream; pos: BiggestInt) =
var s = (HttpBlobStream)s
@ -49,7 +50,7 @@ proc httpBlobRead(s: BlobStream; buffer: pointer; len: Natural): int =
proc httpOpenBlobStream(store: BlobStore; id: BlobId; size: BiggestInt; kind: BlobKind): BlobStream =
var store = HttpStore(store)
let stream = HttpBlobStream(
client: newAsyncHttpClient(),
client: store.client,
closeImpl: httpBlobClose,
setPosImpl: setPosHttp,
getPosImpl: getPosHttp,
@ -87,7 +88,7 @@ proc httpIngest(x: IngestStream; buf: pointer; len: Natural) =
proc httpOpenIngestStream(s: BlobStore; size: BiggestInt; kind: BlobKind): IngestStream =
var store = HttpStore(s)
let
client = newAsyncHttpClient()
client = store.client
url = (store.url / "ingest") / $kind
headers = newHttpHeaders({"ingest-size": $size})
resp = waitFor client.request($url, HttpPOST, headers=headers)
@ -105,9 +106,15 @@ proc httpOpenIngestStream(s: BlobStore; size: BiggestInt; kind: BlobKind): Inges
else:
raiseAssert(resp.status)
proc httpCloseStore(s: BlobStore) =
var s = HttpStore(s)
close s.client
proc newHttpStore*(url: string): HttpStore =
HttpStore(
url: parseUri url,
closeImpl: httpCloseStore,
openBlobStreamImpl: httpOpenBlobStream,
openIngestStreamImpl: httpOpenIngestStream,
client: newAsyncHttpClient(),
url: parseUri url,
)