Skip to content

Commit

Permalink
Render managed centrals in parallel (#1434)
Browse files Browse the repository at this point in the history
  • Loading branch information
ludydoo authored Nov 3, 2023
1 parent 449b5de commit 290d813
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 5 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ require (
github.com/zgalor/weberr v0.8.2
golang.org/x/net v0.17.0
golang.org/x/oauth2 v0.13.0
golang.org/x/sync v0.4.0
golang.org/x/sys v0.13.0
gopkg.in/resty.v1 v1.12.0
gopkg.in/yaml.v2 v2.4.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -841,6 +841,8 @@ golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sync v0.0.0-20220601150217-0de741cfad7f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.4.0 h1:zxkM55ReGkDlKSM+Fu41A+zmbZuaPVbGMzvvdUPznYQ=
golang.org/x/sync v0.4.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
Expand Down
4 changes: 4 additions & 0 deletions internal/dinosaur/pkg/gitops/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gitops

import (
"reflect"
"sync"
"sync/atomic"

"github.com/golang/glog"
Expand All @@ -19,6 +20,7 @@ type ConfigProvider interface {
type validationFn func(config Config) error

type provider struct {
lock sync.Mutex
reader Reader
lastWorkingConfig atomic.Pointer[Config]
validationFn validationFn
Expand Down Expand Up @@ -46,6 +48,8 @@ func NewProvider() ConfigProvider {
// Get implements ConfigProvider.Get
func (p *provider) Get() (Config, error) {
// Load the config from the reader
p.lock.Lock()
defer p.lock.Unlock()
cfg, err := p.reader.Read()
if err != nil {
p.increaseErrorCount()
Expand Down
27 changes: 22 additions & 5 deletions internal/dinosaur/pkg/handlers/data_plane_dinosaur.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/stackrox/acs-fleet-manager/internal/dinosaur/pkg/services"
"github.com/stackrox/acs-fleet-manager/pkg/errors"
"github.com/stackrox/acs-fleet-manager/pkg/handlers"
"golang.org/x/sync/errgroup"
)

type dataPlaneDinosaurHandler struct {
Expand Down Expand Up @@ -80,12 +81,28 @@ func (h *dataPlaneDinosaurHandler) GetAll(w http.ResponseWriter, r *http.Request
managedDinosaurList.RhacsOperators = gitopsConfig.RHACSOperators.ToAPIResponse()
}

managedDinosaurList.Items = make([]private.ManagedCentral, len(centralRequests))
g, ctx := errgroup.WithContext(r.Context())
const maxParallel = 50
locks := make(chan struct{}, maxParallel)
for i := range centralRequests {
converted, err := h.presenter.PresentManagedCentral(centralRequests[i])
if err != nil {
return nil, errors.GeneralError("failed to convert central request to managed central: %v", err)
}
managedDinosaurList.Items = append(managedDinosaurList.Items, converted)
index := i
g.Go(func() error {
select {
case locks <- struct{}{}:
case <-ctx.Done():
//nolint:wrapcheck
return ctx.Err()
}
var err error
managedDinosaurList.Items[index], err = h.presenter.PresentManagedCentral(centralRequests[index])
<-locks
//nolint:wrapcheck
return err
})
}
if err := g.Wait(); err != nil {
return nil, errors.GeneralError("failed to convert central request to managed central: %v", err)
}
return managedDinosaurList, nil
},
Expand Down

0 comments on commit 290d813

Please sign in to comment.