Golang managing HTTP Proxies

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Description

Since the use of Go's own net/http library and the use of the HTTP proxy in conjunction with too many open files the encounter and too many open connections such errors, resulting in the inability to create a new connection, because the use of the connection is not freed, this article is only to summarize the settings to solve this problem

Code

When an HTTP request is assembled, the connection is automatically closed when access is complete, so that the server and client are not kept connected, causing the resource to be consumed

http_requ, _ := http.NewRequest(req.Method, dest_url, req.Body)http_requ.Header = req.Header//在头部设置使用之后关闭连接http_requ.Header.Set("Connection", "close")//或者直接设置Closehttp_requ.Close = true

Other settings are to look at the comments in the source code and guess

http_client := &http.Client{    Timeout: time.Duration(3 * time.Second),//客户端设置3秒超时}
 http_client. Transport = &http. transport{disablekeepalives:false,//turns off connection multiplexing because too many background connections end up with port exhaustion Maxidleconns:-1,//maximum number of idle connections Idleconntimeout:tim E.duration (3 * time. Second),//Idle connection timeout time proxy:http. Proxyurl (PROXY_ADDR),//Set HTTP proxy address}  
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.