Skip to content

Commit 3472b38

Browse files
authored
Remove 304/404 exceptions (#257)
1 parent 4a54129 commit 3472b38

2 files changed

Lines changed: 12 additions & 10 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ The `Unreleased` section name is replaced by the expected version of next releas
2727
- Rename `Equinox.Cosmos.Context` -> `Equinox.CosmosStore.CosmosStoreContext`
2828
- Rename `Equinox.Cosmos.Resolver` -> `Equinox.CosmosStore.CosmosStoreCategory`
2929
- Rename `Equinox.Cosmos.Connector` -> `Equinox.CosmosStore.CosmosStoreClientFactory`
30+
- Remove exceptions from 304/404 paths when reading Tip [#257](https://github.com/jet/equinox/pull/257)
3031
- Reorganized `QueryRetryPolicy` to handle `IAsyncEnumerable` coming in Cosmos SDK V4 [#246](https://github.com/jet/equinox/pull/246) :pray: [@ylibrach](https://github.com/ylibrach)
3132
- Added Secondary store fallback for Event loading, enabling Streams to be hot-migrated (archived to a secondary/clone, then pruned from the primary/active) between Primary and Secondary stores [#247](https://github.com/jet/equinox/pull/247)
3233
- Replaced `BatchingPolicy`, `RetryPolicy` with `TipOptions`, `QueryOptions` to better align with Cosmos SDK V4 [#253](https://github.com/jet/equinox/pull/253)

src/Equinox.CosmosStore/CosmosStore.fs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -371,21 +371,22 @@ module private MicrosoftAzureCosmosWrappers =
371371
let (|CosmosStatusCode|) (e : CosmosException) =
372372
e.StatusCode
373373

374+
type Headers with
375+
member headers.GetRequestCharge () =
376+
match headers.TryGetValue "x-ms-request-charge" with
377+
| true, charge -> float charge
378+
| _ -> 0.
379+
374380
type ReadResult<'T> = Found of 'T | NotFound | NotModified
375381
type Container with
376382
member container.TryReadItem(partitionKey : PartitionKey, documentId : string, ?options : ItemRequestOptions): Async<float * ReadResult<'T>> = async {
377383
let options = defaultArg options null
378384
let! ct = Async.CancellationToken
379-
// TODO use TryReadItemStreamAsync to avoid the exception https://github.com/Azure/azure-cosmos-dotnet-v3/issues/692#issuecomment-521936888
380-
try let! item = async { return! container.ReadItemAsync(documentId, partitionKey, requestOptions = options, cancellationToken = ct) |> Async.AwaitTaskCorrect }
381-
// if item.StatusCode = System.Net.HttpStatusCode.NotModified then return item.RequestCharge, NotModified
382-
// NB `.Document` will NRE if a IfNoneModified precondition triggers a NotModified result
383-
// else
384-
return item.RequestCharge, Found item.Resource
385-
with CosmosException (CosmosStatusCode System.Net.HttpStatusCode.NotFound as e) -> return e.RequestCharge, NotFound
386-
| CosmosException (CosmosStatusCode System.Net.HttpStatusCode.NotModified as e) -> return e.RequestCharge, NotModified
387-
// NB while the docs suggest you may see a 412, the NotModified in the body of the try/with is actually what happens
388-
| CosmosException (CosmosStatusCode System.Net.HttpStatusCode.PreconditionFailed as e) -> return e.RequestCharge, NotModified }
385+
use! rm = async { return! container.ReadItemStreamAsync(documentId, partitionKey, requestOptions = options, cancellationToken = ct) |> Async.AwaitTaskCorrect }
386+
let rc = rm.Headers.GetRequestCharge()
387+
if rm.StatusCode = System.Net.HttpStatusCode.NotFound then return rc, NotFound
388+
elif rm.StatusCode = System.Net.HttpStatusCode.NotModified then return rc, NotModified
389+
else return rc, Found (container.Database.Client.ClientOptions.Serializer.FromStream<'T>(rm.EnsureSuccessStatusCode().Content)) }
389390

390391
// NB don't nest in a private module, or serialization will fail miserably ;)
391392
[<CLIMutable; NoEquality; NoComparison; Newtonsoft.Json.JsonObject(ItemRequired=Newtonsoft.Json.Required.AllowNull)>]

0 commit comments

Comments
 (0)