FDB2003: Database obtained only to run a retry loop
| Analyzer | FoundationDB.Client |
| Severity | Warning (performance) |
| Code fix | Yes |
Message
IFdbDatabaseProvider has ReadAsync, WriteAsync, ReadWriteAsync and Root. Call them on the provider and drop the GetDatabase call.
Why
IFdbDatabaseProvider (and every IFdbDatabaseScopeProvider) runs ReadAsync, WriteAsync, and ReadWriteAsync itself, and exposes Root. Awaiting GetDatabase(ct) first adds an await, a local, and a second place where the connection state is checked, for a database that the code then uses only to call what the provider already offers. Calling the provider keeps the method one line shorter and lets the provider wait for the connection inside the retry loop.
The rule reports a local initialized with await provider.GetDatabase(ct) whose every use is the receiver of Root, or of a ReadAsync, WriteAsync, or ReadWriteAsync call that takes only a handler and a cancellation token (the overloads the provider has). A database passed to another method, or used with a state or a success callback overload, is not reported.
Bad
var db = await provider.GetDatabase(ct);
return await db.ReadAsync(tr => tr.GetReadVersionAsync(), ct);
Good
return await provider.ReadAsync(tr => tr.GetReadVersionAsync(), ct);
Code fix
The fix replaces each use of the local with the provider expression and removes the declaration. A provider that is not a plain reference (a method call, for example) gets no fix, because repeating the call at each use could change behavior.
Suppress
#pragma warning disable FDB2003 // reason
or, for a whole project, in .editorconfig:
dotnet_diagnostic.FDB2003.severity = none
The whole performance tier can be muted at once:
dotnet_analyzer_diagnostic.category-FdbPerformance.severity = none