@@ -56,10 +56,12 @@ type ClientGetterFunc func(context.Context) (regionapi.ClientWithResponsesInterf
56
56
57
57
// Client provides a caching layer for retrieval of region assets, and lazy population.
58
58
type Client struct {
59
- clientGetter ClientGetterFunc
60
- regionCache * cache.LRUExpireCache [string , []regionapi.RegionRead ]
61
- flavorCache * cache.LRUExpireCache [string , []regionapi.Flavor ]
62
- imageCache * cache.LRUExpireCache [string , []regionapi.Image ]
59
+ clientGetter ClientGetterFunc
60
+ client regionapi.ClientWithResponsesInterface
61
+ clientTimeout time.Time
62
+ regionCache * cache.LRUExpireCache [string , []regionapi.RegionRead ]
63
+ flavorCache * cache.LRUExpireCache [string , []regionapi.Flavor ]
64
+ imageCache * cache.LRUExpireCache [string , []regionapi.Image ]
63
65
}
64
66
65
67
// New returns a new client.
@@ -74,12 +76,26 @@ func New(clientGetter ClientGetterFunc) *Client {
74
76
75
77
// Client returns a client.
76
78
func (c * Client ) Client (ctx context.Context ) (regionapi.ClientWithResponsesInterface , error ) {
77
- return c .clientGetter (ctx )
79
+ if time .Now ().Before (c .clientTimeout ) {
80
+ return c .client , nil
81
+ }
82
+
83
+ client , err := c .clientGetter (ctx )
84
+ if err != nil {
85
+ return nil , err
86
+ }
87
+
88
+ // TODO: the timeout should be driven by the token expiry, so we need to expose
89
+ // that eventually.
90
+ c .client = client
91
+ c .clientTimeout = time .Now ().Add (10 * time .Minute )
92
+
93
+ return client , nil
78
94
}
79
95
80
96
// Get gets a specific region.
81
97
func (c * Client ) Get (ctx context.Context , organizationID , regionID string ) (* regionapi.RegionDetailRead , error ) {
82
- client , err := c .clientGetter (ctx )
98
+ client , err := c .Client (ctx )
83
99
if err != nil {
84
100
return nil , err
85
101
}
@@ -103,7 +119,7 @@ func (c *Client) list(ctx context.Context, organizationID string) ([]regionapi.R
103
119
return regions , nil
104
120
}
105
121
106
- client , err := c .clientGetter (ctx )
122
+ client , err := c .Client (ctx )
107
123
if err != nil {
108
124
return nil , err
109
125
}
@@ -147,7 +163,7 @@ func (c *Client) Flavors(ctx context.Context, organizationID, regionID string) (
147
163
return flavors , nil
148
164
}
149
165
150
- client , err := c .clientGetter (ctx )
166
+ client , err := c .Client (ctx )
151
167
if err != nil {
152
168
return nil , err
153
169
}
@@ -181,7 +197,7 @@ func (c *Client) Images(ctx context.Context, organizationID, regionID string) ([
181
197
return images , nil
182
198
}
183
199
184
- client , err := c .clientGetter (ctx )
200
+ client , err := c .Client (ctx )
185
201
if err != nil {
186
202
return nil , err
187
203
}
0 commit comments