From 52c18796306cfb484644b2c21d49702f98c11321 Mon Sep 17 00:00:00 2001 From: Emery Hemingway Date: Mon, 4 Feb 2019 14:54:42 +0100 Subject: [PATCH] Reuse HTTP session for HTTP store transactions --- src/blobsets/httpstores.nim | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/blobsets/httpstores.nim b/src/blobsets/httpstores.nim index b369b21..7db8a26 100644 --- a/src/blobsets/httpstores.nim +++ b/src/blobsets/httpstores.nim @@ -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, )