imroc / req

Simple Go HTTP client with Black Magic
https://req.cool
MIT License
4.25k stars 347 forks source link

Reusing the connection pool when cloning a client #281

Open chuanhhoang opened 1 year ago

chuanhhoang commented 1 year ago

I am writing an app that need to use different clients for different goroutines (so for example, requests to same sites with different accounts will be possible because different clients use different cookiejars). So I need to clone the orginial client for each goroutine but the problem is when cloning the client, the transports are cloned too with a new connection pool and it is a big waste of resources because if I have 200 goroutines, there will be 200 connection pools to be created.

chuanhhoang commented 12 months ago

Any help for this?

imroc commented 12 months ago

Sorry for the late response, for now, the connection pool cannot be shared between multiple clients. If you want to support connection pool sharing, there are many things to consider. I will not consider it for now. I've memorized this scenario to see if it's possible to support it in the future

chuanhhoang commented 11 months ago

Hi @imroc What if I modify the code just clone the http client except for Transport? Would there be anything broken?

gospider007 commented 11 months ago

很抱歉响应晚了,目前,连接池无法在多个客户端之间共享。如果要支持连接池共享,需要考虑许多事项。我暂时不会考虑它。我已经记住了这个场景,看看将来是否有可能支持它

请问连接池在多线程下是并发安全的吗?我看您这句话的意思是不安全的,不知道我的理解是否正确

imroc commented 11 months ago

请问连接池在多线程下是并发安全的吗?我看您这句话的意思是不安全的,不知道我的理解是否正确

同一个client并发创建多个请求是并发安全的,这里说的跟并发安全无关