Skip to content

Commit 4d468a1

Browse files
authored
Add Client Caching (#236)
Caching the data is all well and good, but we need to cache the client too to avoid creating a new one for listing images and flavors during cluster creation.
1 parent 1973a62 commit 4d468a1

File tree

4 files changed

+30
-14
lines changed

4 files changed

+30
-14
lines changed

charts/kubernetes/Chart.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ description: A Helm chart for deploying Unikorn Kubernetes Service
44

55
type: application
66

7-
version: v0.2.64
8-
appVersion: v0.2.64
7+
version: v0.2.65-rc1
8+
appVersion: v0.2.65-rc1
99

1010
icon: https://raw.githubusercontent.com/unikorn-cloud/assets/main/images/logos/dark-on-light/icon.png
1111

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ require (
1515
github.com/stretchr/testify v1.10.0
1616
github.com/unikorn-cloud/core v0.1.96-rc1
1717
github.com/unikorn-cloud/identity v0.2.63
18-
github.com/unikorn-cloud/region v0.1.54
18+
github.com/unikorn-cloud/region v0.1.55-rc1
1919
go.opentelemetry.io/otel v1.35.0
2020
go.opentelemetry.io/otel/sdk v1.35.0
2121
gopkg.in/ini.v1 v1.67.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,8 @@ github.com/unikorn-cloud/core v0.1.96-rc1 h1:TgSMyHhUWYmWgLXiTWNUsroO3F2LtTJ/k+n
179179
github.com/unikorn-cloud/core v0.1.96-rc1/go.mod h1:stInT6j9sM9KzDHgNxBtmrdDIAxQuIZI1/TCGo0jNK8=
180180
github.com/unikorn-cloud/identity v0.2.63 h1:cG3Aa3LmweqOwNtOeq9W29/aoJ4wY0uiulLNzWwn7TY=
181181
github.com/unikorn-cloud/identity v0.2.63/go.mod h1:xuOIyB4wDAz4+kJfZk2q+8MqGj+9IhVbd0Q38iqBY24=
182-
github.com/unikorn-cloud/region v0.1.54 h1:orGCMLIMUmSWLOlBBha6q5SgXKAlzFqQJRVhneD4/so=
183-
github.com/unikorn-cloud/region v0.1.54/go.mod h1:wLNamsGnGIpGTQYFsjyuvelLu5LdtKiniZu0rXu8oUo=
182+
github.com/unikorn-cloud/region v0.1.55-rc1 h1:rnEqKMthOboBi1Coi+FQ/IfAU6jYbYfO9yP+Iaselss=
183+
github.com/unikorn-cloud/region v0.1.55-rc1/go.mod h1:wLNamsGnGIpGTQYFsjyuvelLu5LdtKiniZu0rXu8oUo=
184184
github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM=
185185
github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg=
186186
github.com/xlab/treeprint v1.2.0 h1:HzHnuAF1plUN2zGlAFHbSQP2qJ0ZAD3XF5XD7OesXRQ=

pkg/server/handler/region/region.go

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,12 @@ type ClientGetterFunc func(context.Context) (regionapi.ClientWithResponsesInterf
5656

5757
// Client provides a caching layer for retrieval of region assets, and lazy population.
5858
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]
6365
}
6466

6567
// New returns a new client.
@@ -74,12 +76,26 @@ func New(clientGetter ClientGetterFunc) *Client {
7476

7577
// Client returns a client.
7678
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
7894
}
7995

8096
// Get gets a specific region.
8197
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)
8399
if err != nil {
84100
return nil, err
85101
}
@@ -103,7 +119,7 @@ func (c *Client) list(ctx context.Context, organizationID string) ([]regionapi.R
103119
return regions, nil
104120
}
105121

106-
client, err := c.clientGetter(ctx)
122+
client, err := c.Client(ctx)
107123
if err != nil {
108124
return nil, err
109125
}
@@ -147,7 +163,7 @@ func (c *Client) Flavors(ctx context.Context, organizationID, regionID string) (
147163
return flavors, nil
148164
}
149165

150-
client, err := c.clientGetter(ctx)
166+
client, err := c.Client(ctx)
151167
if err != nil {
152168
return nil, err
153169
}
@@ -181,7 +197,7 @@ func (c *Client) Images(ctx context.Context, organizationID, regionID string) ([
181197
return images, nil
182198
}
183199

184-
client, err := c.clientGetter(ctx)
200+
client, err := c.Client(ctx)
185201
if err != nil {
186202
return nil, err
187203
}

0 commit comments

Comments
 (0)