| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- package factory
-
- import (
- "fmt"
- "net/http"
- "time"
-
- "git.x2erp.com/qdy/go-base/config"
- )
-
- // HTTPFactory HTTP客户端工厂
- type HTTPFactory struct {
- config config.IConfig
- client *http.Client
- }
-
- // NewHTTPFactory 创建HTTP客户端工厂
- func NewHTTPFactory() (*HTTPFactory, error) {
- cfg := config.GetConfig()
-
- if err := config.GetInitError(); err != nil {
- return nil, fmt.Errorf("failed to load config: %v", err)
- }
-
- client := &http.Client{
- Timeout: 30 * time.Second,
- Transport: &http.Transport{
- MaxIdleConns: 100,
- MaxIdleConnsPerHost: 10,
- IdleConnTimeout: 90 * time.Second,
- },
- }
-
- return &HTTPFactory{
- config: cfg,
- client: client,
- }, nil
- }
-
- // CreateHTTPClient 创建HTTP客户端
- func (f *HTTPFactory) CreateHTTPClient() *http.Client {
- return f.client
- }
-
- // GetConfig 获取配置信息
- func (f *HTTPFactory) GetConfig() config.IConfig {
- return f.config
- }
|