Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ResourceFetcher.cs
1using System;
2using System.Threading;
3using System.Threading.Tasks;
8
10{
14 public sealed class ResourceFetcher : IResourceFetcher
15 {
16 private readonly IInternetCacheService cache;
17
18 public ResourceFetcher() : this(ServiceRef.InternetCacheService) { }
20 {
21 this.cache = cache;
22 }
23
24 public async Task<ResourceResult<byte[]>> GetBytesAsync(Uri uri, ResourceFetchOptions options, CancellationToken ct = default)
25 {
26 (byte[]? Data, string ContentType) = await this.cache.TryGet(uri).ConfigureAwait(false);
27 if (Data is not null)
28 return new ResourceResult<byte[]>(Data, ResourceOrigin.Disk, ContentType);
29
30 IAsyncPolicy Timeout = Policies.Timeout(options.Timeout ?? Constants.Timeouts.DownloadFile);
31 IAsyncPolicy Retry = Policies.Retry(
32 maxAttempts: options.RetryAttempts ?? 3,
33 delayProvider: (attempt, ex) => JitterBackoff.DecorrelatedJitter(TimeSpan.FromMilliseconds(200), attempt),
34 shouldRetry: Transient.IsTransient);
35
36 // Fetch via cache service under policies
37 await Services.Resilience.PolicyRunner.RunAsync(async cts =>
38 {
39 (byte[]? Bytes, string Type) = await this.cache.GetOrFetch(uri, options.ParentId, options.Permanent).ConfigureAwait(false);
40 Data = Bytes;
41 ContentType = Type;
42 }, ct, Timeout, Retry).ConfigureAwait(false);
43
44 return new ResourceResult<byte[]>(Data, ResourceOrigin.Network, ContentType);
45 }
46
47 public async Task<ResourceResult<byte[]>> GetBytesAsync(
48 Uri uri,
50 IEnumerable<IAsyncPolicy>? policies,
51 CancellationToken ct = default)
52 {
53 (byte[]? Data, string ContentType) = await this.cache.TryGet(uri).ConfigureAwait(false);
54 if (Data is not null)
55 return new ResourceResult<byte[]>(Data, ResourceOrigin.Disk, ContentType);
56
57 IEnumerable<IAsyncPolicy> ToApply;
58 if (policies is null)
59 {
60 IAsyncPolicy Timeout = Policies.Timeout(options.Timeout ?? Constants.Timeouts.DownloadFile);
61 IAsyncPolicy Retry = Policies.Retry(
62 maxAttempts: options.RetryAttempts ?? 3,
63 delayProvider: (attempt, ex) => JitterBackoff.DecorrelatedJitter(TimeSpan.FromMilliseconds(200), attempt),
64 shouldRetry: Transient.IsTransient);
65 ToApply = new[] { Timeout, Retry };
66 }
67 else
68 {
69 ToApply = policies;
70 }
71
72 await Services.Resilience.PolicyRunner.RunAsync(async cts =>
73 {
74 (byte[]? Bytes, string Type) = await this.cache.GetOrFetch(uri, options.ParentId, options.Permanent).ConfigureAwait(false);
75 Data = Bytes;
76 ContentType = Type;
77 }, ct, ToApply is IAsyncPolicy[] Arr ? Arr : System.Linq.Enumerable.ToArray(ToApply)).ConfigureAwait(false);
78
79 return new ResourceResult<byte[]>(Data, ResourceOrigin.Network, ContentType);
80 }
81 }
82}
static readonly TimeSpan DownloadFile
Download file timeout
Definition: Constants.cs:717
A set of never changing property constants and helpful values.
Definition: Constants.cs:24
int? RetryAttempts
Optional per-call retry attempts override. If null, defaults are used.
TimeSpan? Timeout
Optional per-call timeout override. If null, defaults are used.
Default resource fetcher: retrieves content using Internet cache and applies resilience policies.
async Task< ResourceResult< byte[]> > GetBytesAsync(Uri uri, ResourceFetchOptions options, IEnumerable< IAsyncPolicy >? policies, CancellationToken ct=default)
Retrieves bytes for a URI with optional custom resilience policies. When policies is null,...
static TimeSpan DecorrelatedJitter(TimeSpan baseDelay, int attempt, TimeSpan? maxDelay=null)
Decorrelated jitter backoff ("full jitter") based on attempt number.
static bool IsTransient(Exception ex)
Heuristic transient error classification for network-bound work.
Definition: Transient.cs:13
Base class that references services in the app.
Definition: ServiceRef.cs:43
Defines operations for caching arbitrary internet-fetchable content.
Task<(byte[]? Data, string ContentType)> TryGet(Uri Uri)
Tries to retrieve a cached blob for the specified URI.