Reusing http connections in Golang

前端 未结 9 810
时光取名叫无心
时光取名叫无心 2020-12-02 04:27

I\'m currently struggling to find a way to reuse connections when making HTTP posts in Golang.

I\'ve created a transport and client like so:

// Crea         


        
9条回答
  •  渐次进展
    2020-12-02 05:13

    There are two possible ways:

    1. Use a library that internally reuses and manages the file descriptors, associated with each requests. Http Client does the same thing internally, but then you would have the control over how many concurrent connections to open, and how to manage your resources. If you are interested, look at the netpoll implementation, which internally uses epoll/kqueue to manage them.

    2. The easy one would be, instead of pooling network connections, create a worker pool, for your goroutines. This would be easy, and better solution, that would not hinder with your current codebase, and would require minor changes.

    Let's assume you need to make n POST request, after you recieve a request.

    You could use channels, to implement this.

    Or, simply you could use third party libraries.
    Like: https://github.com/ivpusic/grpool

提交回复
热议问题