divyadesai | 81bb7ba | 2020-03-11 11:45:23 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 3 | |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package kvstore |
| 17 | |
David K. Bainbridge | 595b670 | 2021-04-09 16:10:58 +0000 | [diff] [blame] | 18 | import ( |
Andrea Campanella | 18448bc | 2021-07-08 18:47:22 +0200 | [diff] [blame^] | 19 | "context" |
David K. Bainbridge | 595b670 | 2021-04-09 16:10:58 +0000 | [diff] [blame] | 20 | "fmt" |
Andrea Campanella | 18448bc | 2021-07-08 18:47:22 +0200 | [diff] [blame^] | 21 | "math" |
| 22 | "math/rand" |
| 23 | "time" |
| 24 | ) |
| 25 | |
| 26 | const ( |
| 27 | minRetryInterval = 100 |
| 28 | maxRetryInterval = 5000 |
| 29 | incrementalFactor = 1.2 |
| 30 | jitter = 0.2 |
David K. Bainbridge | 595b670 | 2021-04-09 16:10:58 +0000 | [diff] [blame] | 31 | ) |
divyadesai | 81bb7ba | 2020-03-11 11:45:23 +0000 | [diff] [blame] | 32 | |
| 33 | // ToString converts an interface value to a string. The interface should either be of |
| 34 | // a string type or []byte. Otherwise, an error is returned. |
| 35 | func ToString(value interface{}) (string, error) { |
| 36 | switch t := value.(type) { |
| 37 | case []byte: |
| 38 | return string(value.([]byte)), nil |
| 39 | case string: |
| 40 | return value.(string), nil |
| 41 | default: |
| 42 | return "", fmt.Errorf("unexpected-type-%T", t) |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | // ToByte converts an interface value to a []byte. The interface should either be of |
| 47 | // a string type or []byte. Otherwise, an error is returned. |
| 48 | func ToByte(value interface{}) ([]byte, error) { |
| 49 | switch t := value.(type) { |
| 50 | case []byte: |
| 51 | return value.([]byte), nil |
| 52 | case string: |
| 53 | return []byte(value.(string)), nil |
| 54 | default: |
| 55 | return nil, fmt.Errorf("unexpected-type-%T", t) |
| 56 | } |
| 57 | } |
David K. Bainbridge | 595b670 | 2021-04-09 16:10:58 +0000 | [diff] [blame] | 58 | |
Andrea Campanella | 18448bc | 2021-07-08 18:47:22 +0200 | [diff] [blame^] | 59 | // backoff waits an amount of time that is proportional to the attempt value. The wait time in a range of |
| 60 | // minRetryInterval and maxRetryInterval. |
| 61 | func backoff(ctx context.Context, attempt int) error { |
| 62 | if attempt == 0 { |
| 63 | return nil |
David K. Bainbridge | 595b670 | 2021-04-09 16:10:58 +0000 | [diff] [blame] | 64 | } |
Andrea Campanella | 18448bc | 2021-07-08 18:47:22 +0200 | [diff] [blame^] | 65 | backoff := int(minRetryInterval + incrementalFactor*math.Exp(float64(attempt))) |
| 66 | backoff *= 1 + int(jitter*(rand.Float64()*2-1)) |
| 67 | if backoff > maxRetryInterval { |
| 68 | backoff = maxRetryInterval |
| 69 | } |
| 70 | ticker := time.NewTicker(time.Duration(backoff) * time.Millisecond) |
| 71 | defer ticker.Stop() |
| 72 | select { |
| 73 | case <-ctx.Done(): |
| 74 | return ctx.Err() |
| 75 | case <-ticker.C: |
| 76 | } |
| 77 | return nil |
David K. Bainbridge | 595b670 | 2021-04-09 16:10:58 +0000 | [diff] [blame] | 78 | } |