Skip to content

Commit 3eafbe8

Browse files
author
Wei
authored
Merge pull request #247 from cloudpilot-ai/cherry-pick-245-release-0.2
Cherry pick PR(245)/fix: do not request list api frequently
2 parents c0e569d + 8662517 commit 3eafbe8

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

pkg/providers/instance/instance.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,12 @@ type Provider interface {
6868
}
6969

7070
type DefaultProvider struct {
71-
ecsClient *ecsclient.Client
72-
region string
73-
instanceCache *cache.Cache
71+
ecsClient *ecsclient.Client
72+
region string
73+
74+
lastUpdateTime time.Time
75+
instanceCache *cache.Cache
76+
7477
unavailableOfferings *kcache.UnavailableOfferings
7578

7679
imageFamilyResolver imagefamily.Resolver
@@ -154,7 +157,8 @@ func (p *DefaultProvider) list(ctx context.Context) ([]*Instance, error) {
154157
Value: tea.String("owned"),
155158
},
156159
},
157-
RegionId: tea.String(p.region),
160+
RegionId: tea.String(p.region),
161+
MaxResults: tea.Int32(100),
158162
}
159163

160164
runtime := &util.RuntimeOptions{}
@@ -189,11 +193,19 @@ func (p *DefaultProvider) list(ctx context.Context) ([]*Instance, error) {
189193
}
190194

191195
func (p *DefaultProvider) List(ctx context.Context) ([]*Instance, error) {
196+
if time.Since(p.lastUpdateTime) < instanceCacheExpiration {
197+
items := p.instanceCache.Items()
198+
return lo.Map(lo.Values(items), func(item cache.Item, _ int) *Instance {
199+
return item.Object.(*Instance)
200+
}), nil
201+
}
202+
192203
instances, err := p.list(ctx)
193204
if err != nil {
194205
return nil, err
195206
}
196207
p.syncAllInstances(instances)
208+
p.lastUpdateTime = time.Now()
197209

198210
return instances, nil
199211
}
@@ -259,7 +271,7 @@ func (p *DefaultProvider) CreateTags(ctx context.Context, id string, tags map[st
259271
return fmt.Errorf("tagging instance, %w", err)
260272
}
261273

262-
instances, err := p.list(ctx)
274+
instances, err := p.List(ctx)
263275
if err != nil {
264276
return err
265277
}

0 commit comments

Comments
 (0)