Sin descripción
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_config.go 1.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package subconfigs
  2. import (
  3. "fmt"
  4. "git.x2erp.com/qdy/go-base/config/core"
  5. )
  6. // HTTPConfig HTTP客户端配置
  7. type HTTPConfig struct {
  8. core.BaseConfig
  9. Timeout int `yaml:"timeout" desc:"请求超时时间(秒)"`
  10. MaxIdleConns int `yaml:"max_idle_conns" desc:"最大空闲连接数"`
  11. MaxIdleConnsPerHost int `yaml:"max_idle_conns_per_host" desc:"每个主机的最大空闲连接数"`
  12. IdleConnTimeout int `yaml:"idle_conn_timeout" desc:"空闲连接超时时间(秒)"`
  13. MaxConnsPerHost int `yaml:"max_conns_per_host" desc:"每个主机的最大连接数"`
  14. DisableCompression bool `yaml:"disable_compression" desc:"是否禁用压缩"`
  15. DisableKeepAlives bool `yaml:"disable_keep_alives" desc:"是否禁用长连接"`
  16. }
  17. func (c *HTTPConfig) Description() string {
  18. return "调用其他api的http服务配置"
  19. }
  20. func NewHTTPConfig() *HTTPConfig {
  21. return &HTTPConfig{}
  22. }
  23. func (c *HTTPConfig) SetDefaults() {
  24. c.Timeout = 30
  25. c.MaxIdleConns = 100
  26. c.MaxIdleConnsPerHost = 10
  27. c.IdleConnTimeout = 90
  28. }
  29. func (c *HTTPConfig) Load(data map[string]interface{}) error {
  30. return c.LoadFromYAML(data, c)
  31. }
  32. func (c *HTTPConfig) Validate() error {
  33. if c.Timeout <= 0 {
  34. return fmt.Errorf("http timeout must be positive")
  35. }
  36. return nil
  37. }
  38. func (c *HTTPConfig) IsConfigured() bool {
  39. return c.Timeout > 0
  40. }
  41. // 自动注册
  42. func init() {
  43. core.Register("http", &HTTPConfig{})
  44. }