Skip to content

Commit 696f1c8

Browse files
committed
Added code comments on how lifetime is managed for CancellationTokenSource with RavenAuditIngestionUnitOfWork
1 parent bb515dc commit 696f1c8

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

src/ServiceControl.Audit.Persistence.RavenDB/UnitOfWork/RavenAuditUnitOfWorkFactory.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,19 @@ class RavenAuditIngestionUnitOfWorkFactory(
1515
{
1616
public async ValueTask<IAuditIngestionUnitOfWork> StartNew(int batchSize, CancellationToken cancellationToken)
1717
{
18-
var timedCancellationSource = new CancellationTokenSource(databaseConfiguration.BulkInsertCommitTimeout);
19-
var bulkInsert = (await documentStoreProvider.GetDocumentStore(timedCancellationSource.Token))
20-
.BulkInsert(new BulkInsertOptions { SkipOverwriteIfUnchanged = true, }, timedCancellationSource.Token);
18+
// DO NOT USE using var, will be disposed by RavenAuditIngestionUnitOfWork
19+
var lifetimeForwardedTimedCancellationSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
20+
lifetimeForwardedTimedCancellationSource.CancelAfter(databaseConfiguration.BulkInsertCommitTimeout);
21+
var bulkInsert = (await documentStoreProvider.GetDocumentStore(lifetimeForwardedTimedCancellationSource.Token))
22+
.BulkInsert(new BulkInsertOptions { SkipOverwriteIfUnchanged = true, }, lifetimeForwardedTimedCancellationSource.Token);
2123

2224
return new RavenAuditIngestionUnitOfWork(
23-
bulkInsert, timedCancellationSource, databaseConfiguration.AuditRetentionPeriod, new RavenAttachmentsBodyStorage(sessionProvider, bulkInsert, databaseConfiguration.MaxBodySizeToStore)
25+
bulkInsert,
26+
lifetimeForwardedTimedCancellationSource, // Transfer ownership for disposal
27+
databaseConfiguration.AuditRetentionPeriod,
28+
new RavenAttachmentsBodyStorage(sessionProvider, bulkInsert, databaseConfiguration.MaxBodySizeToStore)
2429
);
30+
// Intentionally not disposing CTS!
2531
}
2632

2733
public bool CanIngestMore() => customCheckState.CanIngestMore;

0 commit comments

Comments
 (0)