Skip to content

Commit

Permalink
Appinsights: resolve PR issues (sourcegraph#133)
Browse files Browse the repository at this point in the history
- Change time.Duration fields to ints
  - Remove duplicate code
  • Loading branch information
mcdafydd authored Jul 21, 2020
1 parent 76fc114 commit 77e7567
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 16 deletions.
15 changes: 6 additions & 9 deletions storage/appinsights/appinsights.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,18 @@ type Storage struct {
// MaxRetries specifies the number of retries before returning error
// from close(). To enable retries, both RetryInterval and MaxRetries
// must be greater than 0. Default is 0 (disabled).
MaxRetries time.Duration `json:"max_retries,omitempty"`
MaxRetries int `json:"max_retries,omitempty"`

// RetryInterval specifies the time between retries in seconds. To enable retries,
// both RetryInterval and MaxRetries must
// be greater than 0. Default is 0 (disabled).
RetryInterval time.Duration `json:"retry_interval,omitempty"`
RetryInterval int `json:"retry_interval,omitempty"`

// Timeout specifies the number of seconds to wait for telemetry submission
// before returning error from close() if retries are disabled. If omitted or
// set to 0, timeout will be 2 seconds. If retries are enabled, this setting
// is ignored.
Timeout time.Duration `json:"timeout,omitempty"`
Timeout int `json:"timeout,omitempty"`

// telemetryConfig defines the settings for client
telemetryConfig *appinsights.TelemetryConfiguration
Expand All @@ -62,9 +62,6 @@ func New(config json.RawMessage) (Storage, error) {
if storage.MaxRetries < 0 {
err = fmt.Errorf("Invalid storage max_retries: %d", storage.MaxRetries)
}
if storage.MaxRetries < 0 {
err = fmt.Errorf("Invalid storage max_retries: %d", storage.MaxRetries)
}
if storage.RetryInterval < 0 {
err = fmt.Errorf("Invalid storage retry_interval: %d", storage.RetryInterval)
}
Expand Down Expand Up @@ -113,14 +110,14 @@ func (c Storage) close() error {
select {
case <-c.client.Channel().Close():
return nil
case <-time.After(c.Timeout * time.Second):
case <-time.After(time.Duration(c.Timeout) * time.Second):
return fmt.Errorf("Failed to submit telemetry before timeout expired")
}
}
select {
case <-c.client.Channel().Close(c.RetryInterval * time.Second):
case <-c.client.Channel().Close(time.Duration(c.RetryInterval) * time.Second):
return nil
case <-time.After((c.MaxRetries + 1) * c.RetryInterval * time.Second):
case <-time.After((time.Duration(c.MaxRetries) + 1) * time.Duration(c.RetryInterval) * time.Second):
return fmt.Errorf("Failed to submit telemetry after retries")
}
}
Expand Down
14 changes: 7 additions & 7 deletions storage/appinsights/appinsights_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ var results = []types.Result{{

func TestNew(t *testing.T) {
type test struct {
retries time.Duration
interval time.Duration
timeout time.Duration
retries int
interval int
timeout int
wantErr bool
}
tests := []test{
Expand Down Expand Up @@ -64,9 +64,9 @@ func TestNew(t *testing.T) {
}
func TestStoreNoRetry(t *testing.T) {
type test struct {
retries time.Duration
interval time.Duration
timeout time.Duration
retries int
interval int
timeout int
}
tests := []test{
{retries: 0, interval: 0, timeout: 0},
Expand Down Expand Up @@ -106,7 +106,7 @@ func TestStoreWithRetry(t *testing.T) {
}
}

func setup(delay time.Duration, retries time.Duration, interval time.Duration, timeout time.Duration, results []types.Result) (Storage, *httptest.Server) {
func setup(delay time.Duration, retries int, interval int, timeout int, results []types.Result) (Storage, *httptest.Server) {
forceRetry := false
if interval > 0 && retries > 0 {
forceRetry = true
Expand Down

0 comments on commit 77e7567

Please sign in to comment.