Skip to content

Commit

Permalink
added retry via polly
Browse files Browse the repository at this point in the history
  • Loading branch information
mplogas committed Mar 16, 2024
1 parent ec72246 commit cb236af
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
2 changes: 2 additions & 0 deletions KernelMemory.FileWatcher/Configuration/KernelMemoryOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@ internal class KernelMemoryOptions
public string Endpoint { get; set; } = "http://localhost:9001";
public string ApiKey { get; set; } = string.Empty;
public TimeSpan Schedule { get; set; }
public int Retries { get; set; } = 2;
public int ParallelUploads { get; set; } = 4;
}
}
2 changes: 2 additions & 0 deletions KernelMemory.FileWatcher/KernelMemory.FileWatcher.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Http.Polly" Version="8.0.3" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0">
<TreatAsUsed>true</TreatAsUsed>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.ObjectPool" Version="8.0.3" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.19.5" />
<PackageReference Include="Polly.Contrib.WaitAndRetry" Version="1.1.1" />
<PackageReference Include="Serilog" Version="3.1.1">
<TreatAsUsed>true</TreatAsUsed>
</PackageReference>
Expand Down
17 changes: 15 additions & 2 deletions KernelMemory.FileWatcher/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Polly;
using Polly.Contrib.WaitAndRetry;
using Polly.Extensions.Http;
using Serilog;

namespace KernelMemory.FileWatcher
Expand Down Expand Up @@ -57,18 +60,28 @@ private static IHostBuilder CreateHostBuilder(string[] args)
{
client.BaseAddress = new Uri(configuration.GetValue<string>("KernelMemory:Endpoint") ??
"http://localhost:9001/");

var apiKey = configuration.GetValue<string>("KernelMemory:ApiKey") ?? string.Empty;
if (!string.IsNullOrWhiteSpace(apiKey))
{
client.DefaultRequestHeaders.Add("Authorization", apiKey);
}
});
}).AddPolicyHandler(GetRetryPolicy(configuration.GetValue<int>("KernelMemory:Retries", 2)));
services.AddSingleton<IFileWatcherFactory, FileWatcherFactory>();
services.AddScoped<IFileWatcherService, FileWatcherService>();
services.AddHostedService<HttpWorker>();
})
.UseConsoleLifetime();
}

private static IAsyncPolicy<HttpResponseMessage> GetRetryPolicy(int retries)
{
var delay = Backoff.DecorrelatedJitterBackoffV2(medianFirstRetryDelay: TimeSpan.FromSeconds(1), retryCount: retries, fastFirst: true);

return HttpPolicyExtensions
.HandleTransientHttpError()
.OrResult(msg => msg.StatusCode == System.Net.HttpStatusCode.NotFound)
//.WaitAndRetryAsync(retries, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)));
.WaitAndRetryAsync(delay);
}
}
}
10 changes: 6 additions & 4 deletions KernelMemory.FileWatcher/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@
"Path": "G:\\tmp\\km-filewatcher\\folder_02",
"Filter": "*.md",
"Index": "folder-02",
"IncludeSubdirectories": true
"IncludeSubdirectories": true,
"InitialScan": false
}
]
},
"KernelMemory": {
"Endpoint": "http://127.0.0.1:9001",
"ApiKey": "",
"Schedule": "00:00:30"
"Endpoint": "http://192.168.65.2:9001",
"Schedule": "00:00:30",
"Retries": 5,
"ParallelUploads": 5
}
}

0 comments on commit cb236af

Please sign in to comment.