add show client with jitter

pull/178/head
Chet Husk 2022-11-03 16:15:04 -05:00
parent a4b55f5f3a
commit d27c813785
4 changed files with 45 additions and 13 deletions

View File

@ -1,16 +1,21 @@
using Microsoft.EntityFrameworkCore;
using Podcast.Infrastructure.Data.Models;
using Podcast.Infrastructure.Http;
using Podcast.Infrastructure.Http.Feeds;
namespace Podcast.Infrastructure.Data;
public class PodcastDbContext : DbContext
{
private readonly ShowClient _showClient;
protected PodcastDbContext()
{
}
public PodcastDbContext(DbContextOptions options) : base(options)
public PodcastDbContext(DbContextOptions options, ShowClient showClient) : base(options)
{
_showClient = showClient;
}
public DbSet<Show> Shows => Set<Show>();
@ -23,6 +28,7 @@ public class PodcastDbContext : DbContext
{
modelBuilder.Entity<Feed>().HasData(Seed.Feeds);
modelBuilder.Entity<Category>().HasData(Seed.Categories);
modelBuilder.Entity<Show>().HasQueryFilter(show => _showClient.CheckLink(show.Link).Result);
modelBuilder.Entity<FeedCategory>().HasData(Seed.FeedCategories);
modelBuilder.Entity<FeedCategory>().HasKey(prop => new { prop.FeedId, prop.CategoryId });
base.OnModelCreating(modelBuilder);

View File

@ -7,17 +7,6 @@ using Podcast.Infrastructure.Data.Models;
namespace Podcast.Infrastructure.Http.Feeds;
public class JitterHandler : DelegatingHandler
{
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
// Who would do such a thing?!
// TODO fix this horrible perf leak!
await Task.Delay(TimeSpan.FromSeconds(Random.Shared.NextInt64()));
return await base.SendAsync(request, cancellationToken);
}
}
public class FeedClient : IFeedClient
{
private static readonly XmlSerializer XmlSerializer = new(typeof(Rss));

View File

@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Podcast.Infrastructure.Http
{
public class JitterHandler : DelegatingHandler
{
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
// Who would do such a thing?!
// TODO fix this horrible perf leak!
await Task.Delay(TimeSpan.FromSeconds(Random.Shared.NextInt64()));
return await base.SendAsync(request, cancellationToken);
}
}
public class ShowClient
{
private readonly HttpClient _httpClient;
public ShowClient(HttpClient httpClient)
{
_httpClient = httpClient;
}
public async Task<bool> CheckLink(string showLink)
{
var response = await _httpClient.GetAsync(showLink, HttpCompletionOption.ResponseHeadersRead);
return response.IsSuccessStatusCode;
}
}
}

View File

@ -9,6 +9,7 @@ using Microsoft.AspNetCore.RateLimiting;
using System.Threading.RateLimiting;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Swashbuckle.AspNetCore.SwaggerGen;
using Podcast.Infrastructure.Http;
var builder = WebApplication.CreateBuilder(args);
@ -17,7 +18,8 @@ var connectionString = builder.Configuration.GetConnectionString("PodcastDb")!;
builder.Services.AddSqlServer<PodcastDbContext>(connectionString);
var queueConnectionString = builder.Configuration.GetConnectionString("FeedQueue");
builder.Services.AddSingleton(new QueueClient(queueConnectionString, "feed-queue"));
builder.Services.AddHttpClient<IFeedClient, FeedClient>().AddHttpMessageHandler<JitterHandler>();
builder.Services.AddHttpClient<IFeedClient, FeedClient>();
builder.Services.AddHttpClient<ShowClient>().AddHttpMessageHandler<JitterHandler>();
// Authentication and authorization-related services
builder.Services.AddAuthentication().AddJwtBearer();