No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

http_factory.go 905B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package factory
  2. import (
  3. "fmt"
  4. "net/http"
  5. "time"
  6. "git.x2erp.com/qdy/go-base/config"
  7. )
  8. // HTTPFactory HTTP客户端工厂
  9. type HTTPFactory struct {
  10. config config.IConfig
  11. client *http.Client
  12. }
  13. // NewHTTPFactory 创建HTTP客户端工厂
  14. func NewHTTPFactory() (*HTTPFactory, error) {
  15. cfg := config.GetConfig()
  16. if err := config.GetInitError(); err != nil {
  17. return nil, fmt.Errorf("failed to load config: %v", err)
  18. }
  19. client := &http.Client{
  20. Timeout: 30 * time.Second,
  21. Transport: &http.Transport{
  22. MaxIdleConns: 100,
  23. MaxIdleConnsPerHost: 10,
  24. IdleConnTimeout: 90 * time.Second,
  25. },
  26. }
  27. return &HTTPFactory{
  28. config: cfg,
  29. client: client,
  30. }, nil
  31. }
  32. // CreateHTTPClient 创建HTTP客户端
  33. func (f *HTTPFactory) CreateHTTPClient() *http.Client {
  34. return f.client
  35. }
  36. // GetConfig 获取配置信息
  37. func (f *HTTPFactory) GetConfig() config.IConfig {
  38. return f.config
  39. }