From c9e4ae6afe583f80b814a37596055b4419c9163e Mon Sep 17 00:00:00 2001 From: Tim Tkachev Date: Thu, 1 Jun 2023 16:50:10 +0300 Subject: [PATCH] v1.4.4 --- CHANGELOG.md | 6 ++-- README.md | 17 +++++++++ config/config.go | 10 ++++++ config/legacy-config.go | 10 ++++++ config/timeouts.go | 50 +++++++++++++++++++++++++++ internal/client/http-client.go | 2 +- internal/client/legacy-http-client.go | 3 +- samples/config/config.json | 13 +++---- samples/config/config.yml | 1 + samples/config/legacy-config.json | 11 +++--- samples/config/legacy-config.yml | 1 + 11 files changed, 107 insertions(+), 17 deletions(-) create mode 100644 config/timeouts.go diff --git a/CHANGELOG.md b/CHANGELOG.md index 40e30d0..9e45686 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,4 @@ -## Version 1.4.3 +## Version 1.4.4 -### Bug Fixes -- Fixed possible nil-pointer reference to validator instance (in concurrent conditions). +### Features +- Added "Timeout" param to Config/LegacyConfig that allows configuring HTTP client timeout diff --git a/README.md b/README.md index 141f161..ecf1ec4 100644 --- a/README.md +++ b/README.md @@ -118,6 +118,7 @@ go get -u repository.basistech.ru/BASIS/decort-golang-sdk | SSOURL | string | Да | URL адрес сервиса аутентификации и авторизации | | DecortURL | string | Да | URL адрес платформы, с которой будет осуществляться взаимодействие | | Retries | uint | Нет | Кол-во неудачных попыток выполнения запроса, по умолчанию - 5 | +| Timeout | config.Duration | Нет | Таймаут HTTP клиента, по умолчанию - без ограничений | | SSLSkipVerify | bool | Нет | Пропуск проверки подлинности сертификата, по умолчанию - true | | Token | string | Нет | JWT токен | @@ -127,6 +128,7 @@ go get -u repository.basistech.ru/BASIS/decort-golang-sdk import ( "repository.basistech.ru/BASIS/decort-golang-sdk/config" ) + func main(){ // Настройка конфигурации cfg := config.Config{ @@ -136,6 +138,8 @@ func main(){ DecortURL: "https://mr4.digitalenergy.online", Retries: 5, } + + cfg.SetTimeout(5 * time.Minute) } ``` @@ -165,6 +169,7 @@ func main() { "ssoUrl": "https://sso.digitalenergy.online", "decortUrl": "https://mr4.digitalenergy.online", "retries": 5, + "timeout": "5m", "sslSkipVerify": false } ``` @@ -177,6 +182,7 @@ appSecret: ssoUrl: https://sso.digitalenergy.online decortUrl: https://mr4.digitalenergy.online retries: 5 +timeout: 5m sslSkipVerify: false ``` @@ -204,6 +210,8 @@ func main() { Retries: 5, } + cfg.SetTimeout(5 * time.Minute) + // Создание клиента client := decort.New(cfg) } @@ -647,6 +655,8 @@ func main() { DecortURL: "", Retries: 5, } + + cfg.SetTimeout(5 * time.Minute) // Создание клиента client := decort.New(cfg) @@ -693,6 +703,7 @@ func main() { | Password | string | Да | пароль legacy пользователя | | DecortURL | string | Да | URL адрес платформы, с которой будет осуществляться взаимодействие | | Retries | uint | Нет | Кол-во неудачных попыток выполнения запроса, по умолчанию - 5 | +| Timeout | config.Duration | Нет | Таймаут HTTP клиента, по умолчанию - без ограничений | | SSLSkipVerify | bool | Нет | Пропуск проверки подлинности сертификата, по умолчанию - true | | Token | string | Нет | JWT токен | @@ -711,6 +722,8 @@ func main(){ DecortURL: "https://mr4.digitalenergy.online", Retries: 5, } + + legacyCfg.SetTimeout(5 * time.Minute) } ``` @@ -739,6 +752,7 @@ func main() { "password": "", "decortUrl": "https://mr4.digitalenergy.online", "retries": 5, + "timeout": "5m", "sslSkipVerify": true } ``` @@ -749,6 +763,7 @@ username: password: decortUrl: https://mr4.digitalenergy.online retries: 5 +timeout: 5m sslSkipVerify: true ``` ### Создание legacy клиента @@ -774,6 +789,8 @@ func main() { Retries: 5, } + legacyCfg.SetTimeout(5 * time.Minute) + // Создание клиента legacyClient := decort.NewLegacy(cfg) } diff --git a/config/config.go b/config/config.go index 224da90..f7dfc11 100644 --- a/config/config.go +++ b/config/config.go @@ -3,6 +3,7 @@ package config import ( "encoding/json" "os" + "time" "gopkg.in/yaml.v3" "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" @@ -44,6 +45,15 @@ type Config struct { // Skip verify, true by default // Required: false SSLSkipVerify bool `json:"sslSkipVerify" yaml:"sslSkipVerify"` + + // HTTP client timeout, unlimited if left empty + // Required: false + Timeout Duration `json:"timeout" yaml:"timeout"` +} + +// SetTimeout is used to set HTTP client timeout. +func (c *Config) SetTimeout(dur time.Duration) { + c.Timeout = Duration(dur) } // ParseConfigJSON parses Config from specified JSON-formatted file. diff --git a/config/legacy-config.go b/config/legacy-config.go index 81175c1..93b5495 100644 --- a/config/legacy-config.go +++ b/config/legacy-config.go @@ -3,6 +3,7 @@ package config import ( "encoding/json" "os" + "time" "gopkg.in/yaml.v3" "repository.basistech.ru/BASIS/decort-golang-sdk/internal/validators" @@ -38,6 +39,15 @@ type LegacyConfig struct { // Skip verify, true by default // Required: false SSLSkipVerify bool `json:"sslSkipVerify" yaml:"sslSkipVerify"` + + // HTTP client timeout, unlimited if left empty + // Required: false + Timeout Duration `json:"timeout" yaml:"timeout"` +} + +// SetTimeout is used to set HTTP client timeout. +func (c *LegacyConfig) SetTimeout(dur time.Duration) { + c.Timeout = Duration(dur) } // ParseLegacyConfigJSON parses LegacyConfig from specified JSON-formatted file. diff --git a/config/timeouts.go b/config/timeouts.go new file mode 100644 index 0000000..dd319e8 --- /dev/null +++ b/config/timeouts.go @@ -0,0 +1,50 @@ +package config + +import ( + "encoding/json" + "fmt" + "time" +) + +// Duration is a wrapper around time.Duration (used for better user experience) +type Duration time.Duration + +func (d *Duration) UnmarshalYAML(unmarshal func(interface{}) error) error { + var v interface{} + if err := unmarshal(&v); err != nil { + return err + } + switch value := v.(type) { + case string: + tmp, err := time.ParseDuration(value) + if err != nil { + return err + } + *d = Duration(tmp) + return nil + default: + return fmt.Errorf("Invalid duration %v", value) + } +} + +func (d *Duration) UnmarshalJSON(b []byte) error { + var v interface{} + if err := json.Unmarshal(b, &v); err != nil { + return err + } + switch value := v.(type) { + case string: + tmp, err := time.ParseDuration(value) + if err != nil { + return err + } + *d = Duration(tmp) + return nil + default: + return fmt.Errorf("Invalid duration %v", value) + } +} + +func (d *Duration) Get() time.Duration { + return time.Duration(*d) +} diff --git a/internal/client/http-client.go b/internal/client/http-client.go index 2fb4719..d895df9 100644 --- a/internal/client/http-client.go +++ b/internal/client/http-client.go @@ -35,6 +35,6 @@ func NewHttpClient(cfg config.Config) *http.Client { //TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, }, - Timeout: 5 * time.Minute, + Timeout: cfg.Timeout.Get(), } } diff --git a/internal/client/legacy-http-client.go b/internal/client/legacy-http-client.go index 0f60a07..6754d2a 100644 --- a/internal/client/legacy-http-client.go +++ b/internal/client/legacy-http-client.go @@ -4,7 +4,6 @@ import ( "crypto/tls" "net/http" "net/url" - "time" "repository.basistech.ru/BASIS/decort-golang-sdk/config" ) @@ -28,6 +27,6 @@ func NewLegacyHttpClient(cfg config.LegacyConfig) *http.Client { decortURL: cfg.DecortURL, }, - Timeout: 5 * time.Minute, + Timeout: cfg.Timeout.Get(), } } diff --git a/samples/config/config.json b/samples/config/config.json index b35c237..c8fca0b 100644 --- a/samples/config/config.json +++ b/samples/config/config.json @@ -1,8 +1,9 @@ { - "appId": "", - "appSecret": "", - "ssoUrl": "https://sso.digitalenergy.online", - "decortUrl": "https://mr4.digitalenergy.online", - "retries": 5, - "sslSkipVerify": false + "appId": "", + "appSecret": "", + "ssoUrl": "https://sso.digitalenergy.online", + "decortUrl": "https://mr4.digitalenergy.online", + "retries": 5, + "timeout": "5m", + "sslSkipVerify": false } diff --git a/samples/config/config.yml b/samples/config/config.yml index 181c139..fea256d 100644 --- a/samples/config/config.yml +++ b/samples/config/config.yml @@ -3,4 +3,5 @@ appSecret: ssoUrl: https://sso.digitalenergy.online decortUrl: https://mr4.digitalenergy.online retries: 5 +timeout: 5m sslSkipVerify: false diff --git a/samples/config/legacy-config.json b/samples/config/legacy-config.json index 14dfcaf..9be5381 100644 --- a/samples/config/legacy-config.json +++ b/samples/config/legacy-config.json @@ -1,7 +1,8 @@ { - "username": "", - "password": "", - "decortUrl": "https://mr4.digitalenergy.online", - "retries": 5, - "sslSkipVerify": true + "username": "", + "password": "", + "decortUrl": "https://mr4.digitalenergy.online", + "retries": 5, + "timeout": "5m", + "sslSkipVerify": true } diff --git a/samples/config/legacy-config.yml b/samples/config/legacy-config.yml index dcb7eca..811b80a 100644 --- a/samples/config/legacy-config.yml +++ b/samples/config/legacy-config.yml @@ -2,4 +2,5 @@ username: password: decortUrl: https://mr4.digitalenergy.online retries: 5 +timeout: 5m sslSkipVerify: true