1 Star 1 Fork 0

灵狐/go-fox-edge-common

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
HttpClients.go 1.64 KB
一键复制 编辑 原始数据 按行查看 历史
灵狐 提交于 2026-01-15 09:44 +08:00 . 从主线合并代码
package hikvision
import (
"gitee.com/fierce_wolf/go-fox-edge-common/commUtil/String"
"gitee.com/fierce_wolf/go-fox-edge-common/commUtil/SyncMap"
"github.com/icholy/digest"
"net/http"
"strings"
"sync"
"time"
)
type clientManager struct {
clients sync.Map
}
func (e *clientManager) Register(ip string, port int, username, password string) {
host := "http://" + strings.ToLower(ip) + ":" + String.ToString(port)
options := make(map[string]interface{})
options["host"] = host
options["username"] = username
options["password"] = password
SyncMap.SetValue(&e.clients, host, "options", options)
}
func (e *clientManager) UnRegister(host string) {
host = strings.ToLower(host)
SyncMap.DelValue(&e.clients, host)
}
func (e *clientManager) GetClient(host string) (*http.Client, error) {
// 尝试取出client
client, err := SyncMap.GetValue(&e.clients, host, "client")
if err != nil {
return nil, err
}
// 该client存在,就返回该client
if client != nil {
client.(*http.Client).Timeout = 10 * time.Second
return client.(*http.Client), nil
}
// 重置一个Client
return e.ResetClient(host)
}
func (e *clientManager) ResetClient(host string) (*http.Client, error) {
// 不存在,就取出账号密码,生成一个新的client
options, err := SyncMap.GetValue(&e.clients, host, "options")
if err != nil {
return nil, err
}
client := &http.Client{
Transport: &digest.Transport{
Username: options.(map[string]interface{})["username"].(string),
Password: options.(map[string]interface{})["password"].(string),
},
}
SyncMap.SetValue(&e.clients, host, "client", client)
client.Timeout = 10 * time.Second
return client, nil
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/fierce_wolf/go-fox-edge-common.git
git@gitee.com:fierce_wolf/go-fox-edge-common.git
fierce_wolf
go-fox-edge-common
go-fox-edge-common
v1.0.2

搜索帮助