Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 1 | /* |
Joey Armstrong | 9cdee9f | 2024-01-03 04:56:14 -0500 | [diff] [blame^] | 2 | * Copyright 2018-2024 Open Networking Foundation (ONF) and the ONF Contributors |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 3 | |
Joey Armstrong | 7f8436c | 2023-07-09 20:23:27 -0400 | [diff] [blame] | 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 |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 7 | |
Joey Armstrong | 7f8436c | 2023-07-09 20:23:27 -0400 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 9 | |
Joey Armstrong | 7f8436c | 2023-07-09 20:23:27 -0400 | [diff] [blame] | 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. |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [diff] [blame] | 15 | */ |
| 16 | package kvstore |
| 17 | |
serkant.uluderya | 2f2855e | 2021-01-30 12:43:40 +0300 | [diff] [blame] | 18 | import ( |
Girish Gowdra | 248971a | 2021-06-01 15:14:15 -0700 | [diff] [blame] | 19 | "context" |
serkant.uluderya | 2f2855e | 2021-01-30 12:43:40 +0300 | [diff] [blame] | 20 | "fmt" |
Girish Gowdra | 248971a | 2021-06-01 15:14:15 -0700 | [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 |
serkant.uluderya | 2f2855e | 2021-01-30 12:43:40 +0300 | [diff] [blame] | 31 | ) |
Scott Baker | 2c1c482 | 2019-10-16 11:02:41 -0700 | [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 | } |
serkant.uluderya | 2f2855e | 2021-01-30 12:43:40 +0300 | [diff] [blame] | 58 | |
Girish Gowdra | 248971a | 2021-06-01 15:14:15 -0700 | [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 |
| 64 | } |
| 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 |
| 78 | } |