@@ -50,6 +50,17 @@ public class RDAPBootstrapService : SingletonBase<RDAPBootstrapService>
5050 /// </summary>
5151 private static readonly TimeSpan RefreshInterval = TimeSpan . FromDays ( 7 ) ;
5252
53+ /// <summary>
54+ /// Wait time before the next automatic attempt after a failed update.
55+ /// </summary>
56+ private static readonly TimeSpan RetryInterval = TimeSpan . FromHours ( 1 ) ;
57+
58+ /// <summary>
59+ /// Number of failed automatic attempts after which a cached file is used until the next regular
60+ /// <see cref="RefreshInterval" /> (e.g. if data.iana.org is blocked). "Update now" always checks.
61+ /// </summary>
62+ private const int MaxFailedAttempts = 3 ;
63+
5364 private readonly HttpClient _client ;
5465 private readonly SemaphoreSlim _semaphore = new ( 1 , 1 ) ;
5566
@@ -184,8 +195,7 @@ public List<string> GetBaseUrls(RDAPQuery query)
184195 /// </summary>
185196 private List < string > FindDomain ( string domain )
186197 {
187- var dns = _dns ?? throw new RDAPException ( RDAPErrorKind . BootstrapUnavailable ,
188- GetFileName ( RDAPBootstrapFileType . DNS ) ) ;
198+ var dns = _dns ?? throw CreateBootstrapUnavailableException ( RDAPBootstrapFileType . DNS ) ;
189199
190200 var labels = domain . Split ( '.' ) ;
191201
@@ -212,8 +222,10 @@ private List<string> FindIPAddress(IPAddress ipAddress, int? prefixLength)
212222 {
213223 var isIPv4 = ipAddress . AddressFamily == AddressFamily . InterNetwork ;
214224
215- var registry = ( isIPv4 ? _ipv4 : _ipv6 ) ?? throw new RDAPException ( RDAPErrorKind . BootstrapUnavailable ,
216- GetFileName ( isIPv4 ? RDAPBootstrapFileType . IPv4 : RDAPBootstrapFileType . IPv6 ) ) ;
225+ var registry = ( isIPv4 ? _ipv4 : _ipv6 ) ??
226+ throw CreateBootstrapUnavailableException ( isIPv4
227+ ? RDAPBootstrapFileType . IPv4
228+ : RDAPBootstrapFileType . IPv6 ) ;
217229
218230 var length = prefixLength ?? ( isIPv4 ? 32 : 128 ) ;
219231
@@ -231,8 +243,7 @@ private List<string> FindIPAddress(IPAddress ipAddress, int? prefixLength)
231243 /// </summary>
232244 private List < string > FindASN ( uint asn )
233245 {
234- var registry = _asn ?? throw new RDAPException ( RDAPErrorKind . BootstrapUnavailable ,
235- GetFileName ( RDAPBootstrapFileType . ASN ) ) ;
246+ var registry = _asn ?? throw CreateBootstrapUnavailableException ( RDAPBootstrapFileType . ASN ) ;
236247
237248 var match = registry . FirstOrDefault ( x => asn >= x . Start && asn <= x . End ) . Urls ;
238249
@@ -244,8 +255,7 @@ private List<string> FindASN(uint asn)
244255 /// </summary>
245256 private List < string > FindEntity ( string handle )
246257 {
247- var objectTags = _objectTags ?? throw new RDAPException ( RDAPErrorKind . BootstrapUnavailable ,
248- GetFileName ( RDAPBootstrapFileType . ObjectTags ) ) ;
258+ var objectTags = _objectTags ?? throw CreateBootstrapUnavailableException ( RDAPBootstrapFileType . ObjectTags ) ;
249259
250260 var index = handle . LastIndexOf ( '-' ) ;
251261
@@ -255,6 +265,17 @@ private List<string> FindEntity(string handle)
255265 return objectTags . GetValueOrDefault ( handle [ ( index + 1 ) .. ] ) ;
256266 }
257267
268+ /// <summary>
269+ /// Creates the exception for a bootstrap file that is not available, including the reason of the last update.
270+ /// </summary>
271+ private RDAPException CreateBootstrapUnavailableException ( RDAPBootstrapFileType type )
272+ {
273+ var info = _cacheInfos ? . FirstOrDefault ( x => x . Type == type ) ;
274+
275+ return new RDAPException ( RDAPErrorKind . BootstrapUnavailable ,
276+ string . IsNullOrEmpty ( info ? . LastError ) ? GetFileName ( type ) : $ "{ GetFileName ( type ) } : { info . LastError } ") ;
277+ }
278+
258279 /// <summary>
259280 /// Orders the base URLs: HTTPS is preferred and tried first (RFC 9224 3). If only HTTP is listed, the same URL
260281 /// is tried over HTTPS first (some registries serve HTTPS without listing it), then over HTTP. HTTP is never
@@ -297,39 +318,56 @@ private async Task UpdateAsync(string cacheDirectory, bool force, CancellationTo
297318
298319 Directory . CreateDirectory ( _cacheDirectory ) ;
299320
300- var changed = false ;
321+ var attempted = false ;
322+ var contentChanged = false ;
323+ Exception networkError = null ;
301324
302325 foreach ( var info in _cacheInfos )
303326 {
304- var path = Path . Combine ( _cacheDirectory , info . FileName ) ;
305- var exists = File . Exists ( path ) ;
327+ // "Expires" is the time of the next check, after a successful update as well as after a failure.
328+ if ( ! force && info . Expires > DateTime . Now )
329+ continue ;
330+
331+ attempted = true ;
306332
307- if ( ! force && exists && info . Expires > DateTime . Now )
333+ // All files are on the same server. After a network error (DNS, connection, timeout), the other files
334+ // are not tried, so a blocked server costs at most one timeout per update.
335+ if ( networkError != null )
336+ {
337+ RecordFailure ( info , networkError ) ;
338+ errors . Add ( $ "{ info . FileName } : { networkError . Message } ") ;
308339 continue ;
340+ }
341+
342+ var path = Path . Combine ( _cacheDirectory , info . FileName ) ;
309343
310344 try
311345 {
312- changed |= await DownloadAsync ( info , path , exists , cancellationToken ) . ConfigureAwait ( false ) ;
346+ contentChanged |= await DownloadAsync ( info , path , File . Exists ( path ) , cancellationToken )
347+ . ConfigureAwait ( false ) ;
313348 }
314349 catch ( Exception ex ) when ( ex is not OperationCanceledException ||
315350 ! cancellationToken . IsCancellationRequested )
316351 {
317352 Log . Warn ( $ "Could not update RDAP bootstrap file { info . FileName } .", ex ) ;
318353
319- info . LastError = ex . Message ;
320- changed = true ;
354+ if ( IsNetworkError ( ex ) )
355+ networkError = ex ;
321356
357+ RecordFailure ( info , ex ) ;
322358 errors . Add ( $ "{ info . FileName } : { ex . Message } ") ;
323359 }
324360 }
325361
326- SaveCacheInfos ( ) ;
327-
328- if ( changed || _dns == null )
362+ // The cached copy is kept and used, if an update fails.
363+ if ( contentChanged || _dns == null )
329364 LoadRegistries ( ) ;
330365
331- if ( changed )
366+ if ( attempted )
367+ {
368+ SaveCacheInfos ( ) ;
332369 CacheInfoChanged ? . Invoke ( this , EventArgs . Empty ) ;
370+ }
333371 }
334372 finally
335373 {
@@ -340,10 +378,35 @@ private async Task UpdateAsync(string cacheDirectory, bool force, CancellationTo
340378 throw new RDAPException ( RDAPErrorKind . BootstrapUnavailable , string . Join ( Environment . NewLine , errors ) ) ;
341379 }
342380
381+ /// <summary>
382+ /// Records a failed update. A cached file is retried after <see cref="RetryInterval" />, at most
383+ /// <see cref="MaxFailedAttempts" /> times, then after the regular <see cref="RefreshInterval" />. A missing file
384+ /// is retried after <see cref="RetryInterval" />, since the query type cannot be used without it.
385+ /// </summary>
386+ private void RecordFailure ( RDAPBootstrapCacheInfo info , Exception ex )
387+ {
388+ info . LastError = ex . Message ;
389+ info . FailedAttempts ++ ;
390+
391+ var exists = File . Exists ( Path . Combine ( _cacheDirectory , info . FileName ) ) ;
392+
393+ info . Expires = DateTime . Now + ( exists && info . FailedAttempts >= MaxFailedAttempts
394+ ? RefreshInterval
395+ : RetryInterval ) ;
396+ }
397+
398+ /// <summary>
399+ /// Network errors (DNS, connection, TLS, timeout) as opposed to an HTTP error status or invalid content.
400+ /// </summary>
401+ private static bool IsNetworkError ( Exception ex )
402+ {
403+ return ex is TaskCanceledException || ex is HttpRequestException { StatusCode : null } ;
404+ }
405+
343406 /// <summary>
344407 /// Downloads a bootstrap file with a conditional request (If-None-Match / If-Modified-Since).
345408 /// </summary>
346- /// <returns>True if the cache info changed.</returns>
409+ /// <returns>True if the file content changed.</returns>
347410 private async Task < bool > DownloadAsync ( RDAPBootstrapCacheInfo info , string path , bool exists ,
348411 CancellationToken cancellationToken )
349412 {
@@ -371,8 +434,9 @@ private async Task<bool> DownloadAsync(RDAPBootstrapCacheInfo info, string path,
371434 if ( response . StatusCode == HttpStatusCode . NotModified && exists )
372435 {
373436 info . LastError = null ;
437+ info . FailedAttempts = 0 ;
374438
375- return true ;
439+ return false ;
376440 }
377441
378442 response . EnsureSuccessStatusCode ( ) ;
@@ -395,6 +459,7 @@ private async Task<bool> DownloadAsync(RDAPBootstrapCacheInfo info, string path,
395459 info . Publication = ParsePublication ( json ) ;
396460 info . Downloaded = DateTime . Now ;
397461 info . LastError = null ;
462+ info . FailedAttempts = 0 ;
398463 info . Size = content . LongLength ;
399464 info . ETag = response . Headers . ETag ? . ToString ( ) ;
400465 info . LastModified = response . Content . Headers . LastModified ;
@@ -438,19 +503,36 @@ private static async Task ReplaceFileAsync(string path, byte[] content, Cancella
438503 {
439504 var tempPath = path + ".tmp" ;
440505
441- await File . WriteAllBytesAsync ( tempPath , content , cancellationToken ) . ConfigureAwait ( false ) ;
506+ try
507+ {
508+ await File . WriteAllBytesAsync ( tempPath , content , cancellationToken ) . ConfigureAwait ( false ) ;
442509
443- for ( var attempt = 1 ; ; attempt ++ )
510+ for ( var attempt = 1 ; ; attempt ++ )
511+ {
512+ try
513+ {
514+ File . Move ( tempPath , path , true ) ;
515+ return ;
516+ }
517+ catch ( Exception ex ) when ( ex is IOException or UnauthorizedAccessException && attempt < 5 )
518+ {
519+ await Task . Delay ( 200 * attempt , cancellationToken ) . ConfigureAwait ( false ) ;
520+ }
521+ }
522+ }
523+ catch
444524 {
525+ // Don't leave a partial file behind, the cached copy is still used.
445526 try
446527 {
447- File . Move ( tempPath , path , true ) ;
448- return ;
528+ File . Delete ( tempPath ) ;
449529 }
450- catch ( Exception ex ) when ( ex is IOException or UnauthorizedAccessException && attempt < 5 )
530+ catch ( Exception ex )
451531 {
452- await Task . Delay ( 200 * attempt , cancellationToken ) . ConfigureAwait ( false ) ;
532+ Log . Warn ( $ "Could not delete { tempPath } ." , ex ) ;
453533 }
534+
535+ throw ;
454536 }
455537 }
456538
@@ -508,6 +590,7 @@ private void LoadCacheInfos(string cacheDirectory)
508590 // The file may have been deleted manually.
509591 info . Downloaded = null ;
510592 info . Size = 0 ;
593+ info . Expires = null ;
511594 }
512595 else
513596 {
@@ -555,7 +638,8 @@ private static RDAPBootstrapCacheInfo Clone(RDAPBootstrapCacheInfo info)
555638 ETag = info . ETag ,
556639 LastModified = info . LastModified ,
557640 Expires = info . Expires ,
558- LastError = info . LastError
641+ LastError = info . LastError ,
642+ FailedAttempts = info . FailedAttempts
559643 } ;
560644 }
561645
0 commit comments