|
| 1 | +// |
| 2 | +// DISCLAIMER |
| 3 | +// |
| 4 | +// Copyright 2023 ArangoDB GmbH, Cologne, Germany |
| 5 | +// |
| 6 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +// you may not use this file except in compliance with the License. |
| 8 | +// You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +// See the License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | +// |
| 18 | +// Copyright holder is ArangoDB GmbH, Cologne, Germany |
| 19 | +// |
| 20 | + |
| 21 | +package agency |
| 22 | + |
| 23 | +import ( |
| 24 | + "context" |
| 25 | + "sync" |
| 26 | + "time" |
| 27 | + |
| 28 | + agencyCache "github.com/arangodb/kube-arangodb/pkg/deployment/agency/cache" |
| 29 | +) |
| 30 | + |
| 31 | +func RetryLoader[T interface{}](loader agencyCache.StateLoader[T], retries int) agencyCache.StateLoader[T] { |
| 32 | + if retries <= 0 { |
| 33 | + return loader |
| 34 | + } |
| 35 | + |
| 36 | + return &retryLoader[T]{ |
| 37 | + parent: loader, |
| 38 | + retries: retries, |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +type retryLoader[T interface{}] struct { |
| 43 | + lock sync.Mutex |
| 44 | + |
| 45 | + retries int |
| 46 | + |
| 47 | + parent agencyCache.StateLoader[T] |
| 48 | +} |
| 49 | + |
| 50 | +func (i *retryLoader[T]) UpdateTime() time.Time { |
| 51 | + i.lock.Lock() |
| 52 | + defer i.lock.Unlock() |
| 53 | + |
| 54 | + return i.parent.UpdateTime() |
| 55 | +} |
| 56 | + |
| 57 | +func (i *retryLoader[T]) Valid() bool { |
| 58 | + i.lock.Lock() |
| 59 | + defer i.lock.Unlock() |
| 60 | + |
| 61 | + return i.parent.Valid() |
| 62 | +} |
| 63 | + |
| 64 | +func (i *retryLoader[T]) State() (*T, uint64, bool) { |
| 65 | + i.lock.Lock() |
| 66 | + defer i.lock.Unlock() |
| 67 | + |
| 68 | + return i.parent.State() |
| 69 | +} |
| 70 | + |
| 71 | +func (i *retryLoader[T]) Invalidate() { |
| 72 | + i.lock.Lock() |
| 73 | + defer i.lock.Unlock() |
| 74 | + |
| 75 | + i.parent.Invalidate() |
| 76 | +} |
| 77 | + |
| 78 | +func (i *retryLoader[T]) Refresh(ctx context.Context, discovery agencyCache.LeaderDiscovery) (err error) { |
| 79 | + i.lock.Lock() |
| 80 | + defer i.lock.Unlock() |
| 81 | + |
| 82 | + for z := 0; z < i.retries-1; z++ { |
| 83 | + if err := i.parent.Refresh(ctx, discovery); err != nil { |
| 84 | + logger.Err(err).Debug("Unable to refresh agency while retrying") |
| 85 | + continue |
| 86 | + } |
| 87 | + |
| 88 | + return nil |
| 89 | + } |
| 90 | + |
| 91 | + return i.parent.Refresh(ctx, discovery) |
| 92 | +} |
0 commit comments