Search code examples
kotlinktor

correct pattern of Ktor's HttpClient usage


What's the correct pattern of usage for HttpClient in KTOR. Should I use it like singleton per app lifecycle, or should I create it per each request?


Solution

  • I would say that you may have more than one client per app if you need to connect to more than one logical services. But if you are dealing with one single HTTP server it's better to have one client because it establishes and holds a connection with server. It also allocates the following resources: prepared threads, coroutines and connections. If you have multiple clients you can potentially run out of these resources.