Files
mika@stocksharp.com 6e7a7dae22 Move instrument lookups and persistence to async
Instrument lookups, configuration, credentials, CSV entities, snapshots and
the alert processing service are read and written asynchronously; the
blocking synchronous forms are obsolete wrappers.

IAsyncPersistable replaces IPersistable: every type that saves and loads its
settings has LoadAsync and SaveAsync only, and nested objects are read with
GetValueAsync, LoadEntireAsync and LoadAllAsync. The synchronous Load and Save
are the obsolete Ecng extensions. The MultiConnect sample keeps IPersistable
for two settings classes, because the settings window of the published
StockSharp.Xaml package takes it.

Copies that were made by saving and loading are made asynchronously.
IMessageAdapter, IIndicator, the risk, commission, PnL, latency, slippage and
position managers, IStorageBuffer, Strategy, DiagramElement, ICompositionModel
and its nodes get CloneAsync and no longer implement ICloneable; Clone is an
obsolete blocking wrapper. A strategy copy keeps every security and portfolio
parameter the original holds. Messages, FieldMapping, BankDetails and
WithdrawInfo are copied in memory. A CommissionRuleMessage shares its rule and
the emulator takes a copy of it. Envelope gets a constructor that takes all
three lines and CreateAsync.

ToBruteForce and ToBruteForceRandom become ToBruteForceAsync and
ToBruteForceRandomAsync and return IAsyncEnumerable; BruteForceOptimizer runs
over it and takes the next strategy through an asynchronous delegate.

Diagram elements keep their own state in SaveState, OnLoading and LoadState.
Undo restores an in-memory snapshot; a chart element holds its series and
axes in it by reference. An alert rule keeps its value in the form a message
carries it. A diagram strategy applies composition settings that were loaded
before the composition was set in SetCompositionAsync or at the start, and
saves them while they wait.

Strategies get an OnStartedAsync hook; diagram strategies resolve securities,
underlyings and strikes before the start so value processing never looks up
instruments. Paths.Serialize, SerializeInvariant, AppStartSettings.TrySave,
ICredentialsProvider.Save, ISnapshotSerializer.Serialize and the composition
registry gain async forms.

ICredentialsProvider.Delete becomes DeleteAsync, so loading, saving and
deleting the credentials wait for one another. Paths.InstalledVersion and
AppNameWithVersion are obsolete in favour of GetInstalledVersionAsync and
GetAppNameWithVersionAsync.

A file is opened for writing only after the last cancellation check, because
opening it empties it; a cancelled read reports the cancellation instead of
an empty result. A snapshot flush that failed leaves what it took due for the
next one.

The indicator templates for C#, F# and Python override LoadAsync and
SaveAsync.
2026-10-04 11:28:17 +03:00

183 lines
5.5 KiB
C#

namespace StockSharp.Configuration.Permissions;
using System.Runtime.CompilerServices;
using System.Text.RegularExpressions;
/// <summary>
/// File-based storage for <see cref="PermissionCredentials"/>.
/// </summary>
/// <remarks>
/// Initializes a new instance of the <see cref="FileCredentialsStorage"/>.
/// </remarks>
/// <param name="fileSystem">File system.</param>
/// <param name="fileName">File name to persist credentials.</param>
/// <param name="asEmail">Use email as login. If <see langword="false"/>, then login can be any string.</param>
public class FileCredentialsStorage(IFileSystem fileSystem, string fileName, bool asEmail = false) : BaseLogReceiver, IPermissionCredentialsStorage
{
private readonly IFileSystem _fileSystem = fileSystem ?? throw new ArgumentNullException(nameof(fileSystem));
private readonly string _fileName = fileName.ThrowIfEmpty(nameof(fileName));
private readonly bool _asEmail = asEmail;
private readonly CachedSynchronizedDictionary<string, PermissionCredentials> _credentials = new(StringComparer.InvariantCultureIgnoreCase);
private bool _initialized;
private async ValueTask EnsureInitializedAsync(CancellationToken cancellationToken)
{
if (_initialized)
return;
var dir = Path.GetDirectoryName(_fileName);
if (!dir.IsEmpty())
_fileSystem.CreateDirectory(dir);
await LoadFromFileAsync(cancellationToken);
// set once the file has been read, so a first read that was cancelled is made again
_initialized = true;
}
IAsyncEnumerable<PermissionCredentials> IPermissionCredentialsStorage.SearchAsync(string loginPattern)
{
return Impl();
async IAsyncEnumerable<PermissionCredentials> Impl([EnumeratorCancellation] CancellationToken cancellationToken = default)
{
await EnsureInitializedAsync(cancellationToken);
var cache = _credentials.CachedValues;
IEnumerable<PermissionCredentials> results;
if (loginPattern.IsEmpty() || loginPattern == "*")
{
results = cache;
}
else
{
var pattern = "^" + Regex.Escape(loginPattern).Replace("\\*", ".*") + "$";
var re = new Regex(pattern, RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
results = cache.Where(c => re.IsMatch(c.Email ?? string.Empty));
}
foreach (var result in results)
{
cancellationToken.ThrowIfCancellationRequested();
yield return await result.CopyAsync(cancellationToken);
}
}
}
async ValueTask IPermissionCredentialsStorage.SaveAsync(PermissionCredentials credentials, CancellationToken cancellationToken)
{
if (credentials == null)
throw new ArgumentNullException(nameof(credentials));
await EnsureInitializedAsync(cancellationToken);
if (!credentials.Email.IsValidLogin(_asEmail))
throw new ArgumentException(credentials.Email, nameof(credentials));
// what the storage answers is what was written down; the caller keeps its own object and
// may go on changing it without granting anything.
var saved = await credentials.CopyAsync(cancellationToken);
// every instance pointed at this file holds the same accounts, so the account is added to
// what the file carries now and not to what this instance last read.
await LoadFromFileAsync(cancellationToken);
var hadPrevious = _credentials.TryGetValue(saved.Email, out var previous);
_credentials[saved.Email] = saved;
try
{
await SaveToFileAsync(cancellationToken);
}
catch
{
if (hadPrevious)
_credentials[saved.Email] = previous;
else
_credentials.Remove(saved.Email);
throw;
}
}
async ValueTask<bool> IPermissionCredentialsStorage.DeleteAsync(string login, CancellationToken cancellationToken)
{
await EnsureInitializedAsync(cancellationToken);
await LoadFromFileAsync(cancellationToken);
if (!_credentials.TryGetAndRemove(login, out var removed))
return false;
try
{
await SaveToFileAsync(cancellationToken);
}
catch
{
// the file still carries the login, so it still grants it: reporting the delete as done
// would revoke it only until the next restart.
_credentials[removed.Email] = removed;
throw;
}
return true;
}
private async ValueTask LoadFromFileAsync(CancellationToken cancellationToken)
{
try
{
if (!_fileName.IsConfigExists(_fileSystem))
return;
var storages = await _fileName.DeserializeInvariantAsync<SettingsStorage[]>(_fileSystem, cancellationToken);
await Do.InvariantAsync(async () =>
{
if (storages == null)
return;
var loaded = new List<PermissionCredentials>();
var ctx = new ContinueOnExceptionContext();
ctx.Error += ex => ex.LogError();
using (ctx.ToScope())
{
foreach (var s in storages)
loaded.Add(await s.LoadAsync<PermissionCredentials>(cancellationToken));
}
using (_credentials.EnterScope())
{
_credentials.Clear();
foreach (var c in loaded)
_credentials[c.Email] = c;
}
});
}
catch (Exception ex) when (ex is not OperationCanceledException || !cancellationToken.IsCancellationRequested)
{
LogError("Load credentials error:\n{0}", ex);
}
}
// A change that never reached the file is not a change, so the failure reaches the caller
// instead of being logged behind a call that answered as if it had worked.
private Task SaveToFileAsync(CancellationToken cancellationToken)
=> Do.InvariantAsync(async () =>
{
var arr = new List<SettingsStorage>();
foreach (var credentials in _credentials.CachedValues)
arr.Add(await credentials.SaveAsync(cancellationToken));
await arr.ToArray().SerializeAsync(_fileSystem, _fileName, true, cancellationToken);
});
}