mirror of
https://github.com/yusing/godoxy.git
synced 2026-03-23 01:29:12 +01:00
refactor(agent): extract agent pool and HTTP utilities to dedicated package
Moved non-agent-specific logic from agent/pkg/agent/ to internal/agentpool/: - pool.go: Agent pool management (Get, Add, Remove, List, Iter, etc.) - http_requests.go: HTTP utilities (health checks, forwarding, websockets, reverse proxy) - agent.go: Agent struct with HTTP client management This separates general-purpose pool management from agent-specific configuration, improving code organization and making the agent package focused on agent config only.
This commit is contained in:
79
internal/agentpool/pool.go
Normal file
79
internal/agentpool/pool.go
Normal file
@@ -0,0 +1,79 @@
|
||||
package agentpool
|
||||
|
||||
import (
|
||||
"iter"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/puzpuzpuz/xsync/v4"
|
||||
"github.com/yusing/godoxy/agent/pkg/agent"
|
||||
)
|
||||
|
||||
var agentPool = xsync.NewMap[string, *Agent](xsync.WithPresize(10))
|
||||
|
||||
func init() {
|
||||
if strings.HasSuffix(os.Args[0], ".test") {
|
||||
agentPool.Store("test-agent", &Agent{
|
||||
AgentConfig: &agent.AgentConfig{
|
||||
Addr: "test-agent",
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Get(agentAddrOrDockerHost string) (*Agent, bool) {
|
||||
if !agent.IsDockerHostAgent(agentAddrOrDockerHost) {
|
||||
return getAgentByAddr(agentAddrOrDockerHost)
|
||||
}
|
||||
return getAgentByAddr(agent.GetAgentAddrFromDockerHost(agentAddrOrDockerHost))
|
||||
}
|
||||
|
||||
func GetAgent(name string) (*Agent, bool) {
|
||||
for _, agent := range agentPool.Range {
|
||||
if agent.Name == name {
|
||||
return agent, true
|
||||
}
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
|
||||
func Add(cfg *agent.AgentConfig) (added bool) {
|
||||
_, loaded := agentPool.LoadOrCompute(cfg.Addr, func() (*Agent, bool) {
|
||||
return newAgent(cfg), false
|
||||
})
|
||||
return !loaded
|
||||
}
|
||||
|
||||
func Has(cfg *agent.AgentConfig) bool {
|
||||
_, ok := agentPool.Load(cfg.Addr)
|
||||
return ok
|
||||
}
|
||||
|
||||
func Remove(cfg *agent.AgentConfig) {
|
||||
agentPool.Delete(cfg.Addr)
|
||||
}
|
||||
|
||||
func RemoveAll() {
|
||||
agentPool.Clear()
|
||||
}
|
||||
|
||||
func List() []*Agent {
|
||||
agents := make([]*Agent, 0, agentPool.Size())
|
||||
for _, agent := range agentPool.Range {
|
||||
agents = append(agents, agent)
|
||||
}
|
||||
return agents
|
||||
}
|
||||
|
||||
func Iter() iter.Seq2[string, *Agent] {
|
||||
return agentPool.Range
|
||||
}
|
||||
|
||||
func Num() int {
|
||||
return agentPool.Size()
|
||||
}
|
||||
|
||||
func getAgentByAddr(addr string) (agent *Agent, ok bool) {
|
||||
agent, ok = agentPool.Load(addr)
|
||||
return agent, ok
|
||||
}
|
||||
Reference in New Issue
Block a user