Skip to content

Commit

Permalink
save page size when adjustment needed for url
Browse files Browse the repository at this point in the history
  • Loading branch information
jcollins-axway committed Jul 29, 2024
1 parent 00fe8d4 commit e6920c5
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 1 deletion.
4 changes: 3 additions & 1 deletion pkg/apic/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,9 @@ type Client interface {
// New creates a new Client
func New(cfg corecfg.CentralConfig, tokenRequester auth.PlatformTokenGetter, caches cache2.Manager) Client {
serviceClient := &ServiceClient{
caches: caches,
caches: caches,
pageSizes: map[string]int{},
pageSizeMutex: &sync.Mutex{},
}
serviceClient.logger = log.NewFieldLogger().
WithComponent("serviceClient").
Expand Down
2 changes: 2 additions & 0 deletions pkg/apic/definitions.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ type ServiceClient struct {
DefaultSubscriptionApprovalWebhook corecfg.WebhookConfig
subscriptionRegistrationLock sync.Mutex
logger log.FieldLogger
pageSizes map[string]int
pageSizeMutex *sync.Mutex
}

// APIServerInfoProperty -
Expand Down
3 changes: 3 additions & 0 deletions pkg/apic/mockserviceclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package apic

import (
"net/http"
"sync"
"time"

cache2 "github.com/Axway/agent-sdk/pkg/agent/cache"
Expand Down Expand Up @@ -61,6 +62,8 @@ func GetTestServiceClient() (*ServiceClient, *api.MockHTTPClient) {
DefaultSubscriptionApprovalWebhook: webhook,
DefaultSubscriptionSchema: NewSubscriptionSchema(cfg.GetEnvironmentName() + SubscriptionSchemaNameSuffix),
logger: log.NewFieldLogger(),
pageSizes: map[string]int{},
pageSizeMutex: &sync.Mutex{},
}
svcClient.subscriptionMgr = newSubscriptionManager(svcClient)
return svcClient, apiClient
Expand Down
21 changes: 21 additions & 0 deletions pkg/apic/resourcePagination.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,19 @@ func (c *ServiceClient) GetAPIV1ResourceInstances(queryParams map[string]string,
return c.GetAPIV1ResourceInstancesWithPageSize(queryParams, url, c.cfg.GetPageSize())
}

func (c *ServiceClient) getPageSize(url string) (int, bool) {
c.pageSizeMutex.Lock()
defer c.pageSizeMutex.Unlock()
size, ok := c.pageSizes[url]
return size, ok
}

func (c *ServiceClient) setPageSize(url string, size int) {
c.pageSizeMutex.Lock()
defer c.pageSizeMutex.Unlock()
c.pageSizes[url] = size
}

// GetAPIV1ResourceInstancesWithPageSize - return apiv1 Resource instance
func (c *ServiceClient) GetAPIV1ResourceInstancesWithPageSize(queryParams map[string]string, url string, pageSize int) ([]*apiv1.ResourceInstance, error) {
morePages := true
Expand All @@ -71,6 +84,11 @@ func (c *ServiceClient) GetAPIV1ResourceInstancesWithPageSize(queryParams map[st
url = c.createAPIServerURL(url)
}

// update page size if this endpoint used an adjusted page size before
if size, ok := c.getPageSize(url); ok {
pageSize = size
}

for morePages {
query := map[string]string{
"page": strconv.Itoa(page),
Expand All @@ -92,6 +110,9 @@ func (c *ServiceClient) GetAPIV1ResourceInstancesWithPageSize(queryParams map[st
pageSize = pageSize / 2
log.WithError(err).WithField("newPageSize", pageSize).Debug("error while retrieving resources, retrying with smaller page size")
retries--

// update the page size map so this endpoint uses the same size next time
c.setPageSize(url, pageSize)
continue
} else if err != nil {
log.WithError(err).Debug("error while retrieving resources")
Expand Down

0 comments on commit e6920c5

Please sign in to comment.