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

View File

@ -20,11 +20,12 @@ type
HttpStore* = ref HttpStoreObj HttpStore* = ref HttpStoreObj
HttpStoreObj = object of BlobStoreObj 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 url: Uri
proc httpBlobClose(s: BlobStream) = proc httpBlobClose(s: BlobStream) = discard
var s = (HttpBlobStream)s
close s.client
proc setPosHttp(s: BlobStream; pos: BiggestInt) = proc setPosHttp(s: BlobStream; pos: BiggestInt) =
var s = (HttpBlobStream)s 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 = proc httpOpenBlobStream(store: BlobStore; id: BlobId; size: BiggestInt; kind: BlobKind): BlobStream =
var store = HttpStore(store) var store = HttpStore(store)
let stream = HttpBlobStream( let stream = HttpBlobStream(
client: newAsyncHttpClient(), client: store.client,
closeImpl: httpBlobClose, closeImpl: httpBlobClose,
setPosImpl: setPosHttp, setPosImpl: setPosHttp,
getPosImpl: getPosHttp, getPosImpl: getPosHttp,
@ -87,7 +88,7 @@ proc httpIngest(x: IngestStream; buf: pointer; len: Natural) =
proc httpOpenIngestStream(s: BlobStore; size: BiggestInt; kind: BlobKind): IngestStream = proc httpOpenIngestStream(s: BlobStore; size: BiggestInt; kind: BlobKind): IngestStream =
var store = HttpStore(s) var store = HttpStore(s)
let let
client = newAsyncHttpClient() client = store.client
url = (store.url / "ingest") / $kind url = (store.url / "ingest") / $kind
headers = newHttpHeaders({"ingest-size": $size}) headers = newHttpHeaders({"ingest-size": $size})
resp = waitFor client.request($url, HttpPOST, headers=headers) resp = waitFor client.request($url, HttpPOST, headers=headers)
@ -105,9 +106,15 @@ proc httpOpenIngestStream(s: BlobStore; size: BiggestInt; kind: BlobKind): Inges
else: else:
raiseAssert(resp.status) raiseAssert(resp.status)
proc httpCloseStore(s: BlobStore) =
var s = HttpStore(s)
close s.client
proc newHttpStore*(url: string): HttpStore = proc newHttpStore*(url: string): HttpStore =
HttpStore( HttpStore(
url: parseUri url, closeImpl: httpCloseStore,
openBlobStreamImpl: httpOpenBlobStream, openBlobStreamImpl: httpOpenBlobStream,
openIngestStreamImpl: httpOpenIngestStream, openIngestStreamImpl: httpOpenIngestStream,
client: newAsyncHttpClient(),
url: parseUri url,
) )