feat(agent): add agent iteration and count functions; refactor Forward method to return *http.Response

This commit is contained in:
yusing
2025-09-04 06:36:25 +08:00
parent 20105534c7
commit 1c80f3e52f
2 changed files with 13 additions and 5 deletions

View File

@@ -1,6 +1,8 @@
package agent
import (
"iter"
"github.com/yusing/go-proxy/internal/common"
"github.com/yusing/go-proxy/internal/utils/functional"
)
@@ -51,6 +53,14 @@ func ListAgents() []*AgentConfig {
return agents
}
func IterAgents() iter.Seq2[string, *AgentConfig] {
return agentPool.Range
}
func NumAgents() int {
return agentPool.Size()
}
func getAgentByAddr(addr string) (agent *AgentConfig, ok bool) {
agent, ok = agentPool.Load(addr)
return

View File

@@ -16,7 +16,7 @@ func (cfg *AgentConfig) Do(ctx context.Context, method, endpoint string, body io
return cfg.httpClient.Do(req)
}
func (cfg *AgentConfig) Forward(req *http.Request, endpoint string) ([]byte, int, error) {
func (cfg *AgentConfig) Forward(req *http.Request, endpoint string) (*http.Response, error) {
req = req.WithContext(req.Context())
req.URL.Host = AgentHost
req.URL.Scheme = "https"
@@ -24,11 +24,9 @@ func (cfg *AgentConfig) Forward(req *http.Request, endpoint string) ([]byte, int
req.RequestURI = ""
resp, err := cfg.httpClient.Do(req)
if err != nil {
return nil, 0, err
return nil, err
}
defer resp.Body.Close()
data, _ := io.ReadAll(resp.Body)
return data, resp.StatusCode, nil
return resp, nil
}
func (cfg *AgentConfig) Fetch(ctx context.Context, endpoint string) ([]byte, int, error) {