akamsteeg / AtleX.HaveIBeenPwned

A fully async .NET Standard client library for the API of HaveIBeenPwned.com
https://www.nuget.org/packages/AtleX.HaveIBeenPwned/
MIT License
5 stars 0 forks source link

Cache and reuse internal JsonSerializerOptions #74

Closed akamsteeg closed 1 year ago

akamsteeg commented 1 year ago

Currently, new instance of JsonSerializerOptions is created for every instance of HaveIBeenPwnedClient. Especially in high-load situations we don't want to keep a single instance of the client around for the lifetime of the application because of the injected (or internal) HttpClient and problems like DNS expiration and running out of ephemeral ports. So we want to recreate clients and because of that instantiation is designed to be as light-weight as possible.

However, since (de)serialization info is cached inside the JsonSerializerOptions (pre .NET 7) it's also a performance hit to recreate client instances. It means that for every client the (de)serialization info is created again.

Solution: Cache the JsonSerializerOptions for the lifetime of the application.

akamsteeg commented 1 year ago

Fixed in 7c3bf7f8ddb05b975e0d44d090c3f25f8d3d376b.