Skip to content

Commit e93d861

Browse files
authored
[Feature] Extract Agency timeout (#1344)
1 parent 5ab671d commit e93d861

File tree

3 files changed

+75
-12
lines changed

3 files changed

+75
-12
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
- (Improvement) Block traffic on the services if there is more than 1 active leader in ActiveFailover mode
55
- (Improvement) Improve master endpoint validation.
66
- (Feature) Agency Improvements
7+
- (Bugfix) Fix agency timeout
8+
- (Improvement) Extract Agency Timeout
79

810
## [1.2.30](https://github.com/arangodb/kube-arangodb/tree/1.2.30) (2023-06-16)
911
- (Feature) AgencyCache Interface

pkg/deployment/agency/loader.go

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ import (
3232
func getLoader[T interface{}]() agencyCache.StateLoader[T] {
3333
loader := getLoaderBase[T]()
3434

35+
loader = TimeoutLoader[T](loader, globals.GetGlobalTimeouts().Agency().Get())
36+
3537
loader = InvalidateOnErrorLoader[T](loader)
3638

3739
loader = DelayLoader[T](loader, agencyCache.GlobalConfig().RefreshDelay)
@@ -103,29 +105,20 @@ func (s *simpleStateLoader[T]) Refresh(ctx context.Context, discovery agencyCach
103105
s.lock.Lock()
104106
defer s.lock.Unlock()
105107

106-
lctx, cancel := globals.GetGlobalTimeouts().Agency().WithTimeout(ctx)
107-
defer cancel()
108-
109-
conn, err := discovery.Discover(lctx)
108+
conn, err := discovery.Discover(ctx)
110109
if err != nil {
111110
return err
112111
}
113112

114-
cctx, cancel := globals.GetGlobalTimeouts().Agency().WithTimeout(ctx)
115-
defer cancel()
116-
117-
cfg, err := GetAgencyConfig(cctx, conn)
113+
cfg, err := GetAgencyConfig(ctx, conn)
118114
if err != nil {
119115
return err
120116
}
121117

122118
if !s.valid || s.index != cfg.CommitIndex {
123119
// Full reload
124120

125-
sctx, cancel := globals.GetGlobalTimeouts().Agency().WithTimeout(ctx)
126-
defer cancel()
127-
128-
state, err := GetAgencyState[T](sctx, conn)
121+
state, err := GetAgencyState[T](ctx, conn)
129122
if err != nil {
130123
return err
131124
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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+
"time"
26+
27+
agencyCache "github.com/arangodb/kube-arangodb/pkg/deployment/agency/cache"
28+
)
29+
30+
func TimeoutLoader[T interface{}](loader agencyCache.StateLoader[T], timeout time.Duration) agencyCache.StateLoader[T] {
31+
if timeout <= 0 {
32+
return loader
33+
}
34+
35+
return &timeoutLoader[T]{
36+
parent: loader,
37+
timeout: timeout,
38+
}
39+
}
40+
41+
type timeoutLoader[T interface{}] struct {
42+
parent agencyCache.StateLoader[T]
43+
44+
timeout time.Duration
45+
}
46+
47+
func (i *timeoutLoader[T]) UpdateTime() time.Time {
48+
return i.parent.UpdateTime()
49+
}
50+
51+
func (i *timeoutLoader[T]) Valid() bool {
52+
return i.parent.Valid()
53+
}
54+
55+
func (i *timeoutLoader[T]) State() (*T, uint64, bool) {
56+
return i.parent.State()
57+
}
58+
59+
func (i *timeoutLoader[T]) Invalidate() {
60+
i.parent.Invalidate()
61+
}
62+
63+
func (i *timeoutLoader[T]) Refresh(ctx context.Context, discovery agencyCache.LeaderDiscovery) error {
64+
nctx, c := context.WithTimeout(ctx, i.timeout)
65+
defer c()
66+
67+
return i.parent.Refresh(nctx, discovery)
68+
}

0 commit comments

Comments
 (0)