代码拉取完成,页面将自动刷新
package tools
import (
"crypto/tls"
"fmt"
"net/http"
"time"
)
const timeout = 10 * time.Second
type ClientPool struct {
clients chan *http.Client
clientUsed int
clientMax int
}
func NewClientPool(maxClients int) *ClientPool {
pool := new(ClientPool)
pool.Init(maxClients)
return pool
}
func (p *ClientPool) Init(maxClients int) {
p.clientMax = maxClients
p.clients = make(chan *http.Client, maxClients)
}
func (p *ClientPool) GetClient() (client *http.Client) {
t := time.NewTicker(timeout)
for {
select {
case client = <-p.clients:
p.clientUsed += 1
return client
case <-t.C:
fmt.Println("number of clients reach max, no more available")
return nil
default:
if p.clientUsed < p.clientMax {
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
TLSHandshakeTimeout: timeout,
ResponseHeaderTimeout: timeout,
ExpectContinueTimeout: timeout,
}
client = &http.Client{
Transport: tr,
Timeout: timeout,
}
p.clientUsed += 1
fmt.Printf("generate new client, %d use / %d max\n", p.clientUsed, p.clientMax)
return client
}
}
}
}
func (p *ClientPool) Recycle(client *http.Client) {
if client != nil {
p.clientUsed -= 1
p.clients <- client
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。