Skip to content

Commit 79119ae

Browse files
committed
improve retries
Signed-off-by: Markus Blaschke <mblaschke82@gmail.com>
1 parent 43fd92a commit 79119ae

File tree

5 files changed

+36
-9
lines changed

5 files changed

+36
-9
lines changed

README.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,18 @@ Application Options:
2121
--log.source=[|short|file|full] Show source for every log message (useful for debugging and bug reports) [$LOG_SOURCE]
2222
--log.color=[|auto|yes|no] Enable color for logs [$LOG_COLOR]
2323
--log.time Show log time [$LOG_TIME]
24-
--shelly.request.timeout= Request timeout (default: 5s) [$SHELLY_REQUEST_TIMEOUT]
24+
--shelly.request.timeout= Request timeout (default: 2s) [$SHELLY_REQUEST_TIMEOUT]
25+
--shelly.request.retry.count= Retry count for failing requests (default: 3) [$SHELLY_REQUEST_RETRY_COUNT]
26+
--shelly.request.retry.waittime= Wait time after retry (default: 100ms) [$SHELLY_REQUEST_RETRY_WAITTIME]
27+
--shelly.request.retry.waittimemax= Maximum wait time after retry (default: 1s) [$SHELLY_REQUEST_RETRY_WAITTIMEMAX]
2528
--shelly.auth.username= Username for shelly plug login [$SHELLY_AUTH_USERNAME]
2629
--shelly.auth.password= Password for shelly plug login [$SHELLY_AUTH_PASSWORD]
27-
--shelly.host.shellyplug= shellyplug device IP or hostname to scrape. Pass multiple times for multiple hosts [$SHELLY_HOST_SHELLYPLUGS]
28-
--shelly.host.shellyplus= shellyplus device IP or hostname to scrape. Pass multiple times for multiple hosts [$SHELLY_HOST_SHELLYPLUSES]
29-
--shelly.host.shellypro= shellypro device IP or hostname to scrape. Pass multiple times for multiple hosts [$SHELLY_HOST_SHELLYPROS]
30+
--shelly.host.shellyplug= shellyplug device IP or hostname to scrape. Pass multiple times for multiple hosts
31+
[$SHELLY_HOST_SHELLYPLUGS]
32+
--shelly.host.shellyplus= shellyplus device IP or hostname to scrape. Pass multiple times for multiple hosts
33+
[$SHELLY_HOST_SHELLYPLUSES]
34+
--shelly.host.shellypro= shellypro device IP or hostname to scrape. Pass multiple times for multiple hosts
35+
[$SHELLY_HOST_SHELLYPROS]
3036
--shelly.servicediscovery.timeout= mDNS discovery response timeout (default: 15s) [$SHELLY_SERVICEDISCOVERY_TIMEOUT]
3137
--shelly.servicediscovery.refresh= mDNS discovery refresh time (default: 15m) [$SHELLY_SERVICEDISCOVERY_REFRESH]
3238
--server.bind= Server address (default: :8080) [$SERVER_BIND]

config/opts.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ type (
1818

1919
Shelly struct {
2020
Request struct {
21-
Timeout time.Duration `long:"shelly.request.timeout" env:"SHELLY_REQUEST_TIMEOUT" description:"Request timeout" default:"5s"`
21+
Timeout time.Duration `long:"shelly.request.timeout" env:"SHELLY_REQUEST_TIMEOUT" description:"Request timeout" default:"2s"`
22+
RetryCount int `long:"shelly.request.retry.count" env:"SHELLY_REQUEST_RETRY_COUNT" description:"Retry count for failing requests" default:"3"`
23+
RetryWaitTime time.Duration `long:"shelly.request.retry.waittime" env:"SHELLY_REQUEST_RETRY_WAITTIME" description:"Wait time after retry" default:"100ms"`
24+
RetryWaitTimeMax time.Duration `long:"shelly.request.retry.waittimemax" env:"SHELLY_REQUEST_RETRY_WAITTIMEMAX" description:"Maximum wait time after retry" default:"1s"`
2225
}
2326

2427
Auth struct {

probe.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ func newShellyProber(ctx context.Context, registry *prometheus.Registry, logger
2424
sp := shellyplug.New(ctx, registry, logger)
2525
sp.SetUserAgent(UserAgent + gitTag)
2626
sp.SetTimeout(Opts.Shelly.Request.Timeout)
27+
sp.EnableRetry(Opts.Shelly.Request.RetryCount, Opts.Shelly.Request.RetryWaitTime, Opts.Shelly.Request.RetryWaitTimeMax)
2728
if len(Opts.Shelly.Auth.Username) >= 1 {
2829
sp.SetHttpAuth(Opts.Shelly.Auth.Username, Opts.Shelly.Auth.Password)
2930
}

shellyplug/prober.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@ type (
2626
}
2727

2828
resty struct {
29-
timeout time.Duration
29+
timeout time.Duration
30+
retryCount int
31+
retryWaitTime time.Duration
32+
retryWaitTimeMax time.Duration
33+
3034
userAgent string
3135
}
3236

shellyplug/resty.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ func (sp *ShellyPlug) SetTimeout(timeout time.Duration) {
2626
sp.resty.timeout = timeout
2727
}
2828

29+
func (sp *ShellyPlug) EnableRetry(retries int, waitTime, waitTimeMax time.Duration) {
30+
sp.resty.retryCount = retries
31+
sp.resty.retryWaitTime = waitTime
32+
sp.resty.retryWaitTimeMax = waitTimeMax
33+
}
34+
2935
func (sp *ShellyPlug) SetHttpAuth(username, password string) {
3036
sp.auth.username = username
3137
sp.auth.password = password
@@ -56,9 +62,16 @@ func (sp *ShellyPlug) restyClient(ctx context.Context, target discovery.Discover
5662
client.SetHeader("User-Agent", sp.resty.userAgent)
5763
}
5864

59-
client.SetRetryCount(2).
60-
SetRetryWaitTime(1 * time.Second).
61-
SetRetryMaxWaitTime(5 * time.Second)
65+
if sp.resty.retryCount > 0 {
66+
client.SetRetryCount(sp.resty.retryCount)
67+
68+
if sp.resty.retryWaitTime.Seconds() > 0 {
69+
client.SetRetryWaitTime(sp.resty.retryWaitTime)
70+
}
71+
if sp.resty.retryWaitTimeMax.Seconds() > 0 {
72+
client.SetRetryWaitTime(sp.resty.retryWaitTimeMax)
73+
}
74+
}
6275

6376
if sp.auth.username != "" {
6477
client.SetDisableWarn(true)

0 commit comments

Comments
 (0)