mirror of
https://github.com/fleetdm/fleet
synced 2026-05-04 22:08:41 +00:00
# Overview This pull request resolves #31165, implementing command-line tooling to migrate GitOps YAML files following the [changes introduced in the upcoming 4.74 release](https://github.com/fleetdm/fleet/pull/32237/files#diff-8769f6e90e8bdf15faad8f390fdf3ffb6fd2238b7d6087d83518c21464109119R7). Aligning with the recommended steps in the `README`; [this is an example of the first step](https://github.com/Illbjorn/fleet/pull/3/files) (`gitops-migrate format`) and [this is an example of the second step](https://github.com/Illbjorn/fleet/pull/4/files) (`gitops-migrate migrate`). --------- Signed-off-by: Illbjorn <am@hades.so> Co-authored-by: Ian Littman <iansltx@gmail.com>
67 lines
1.2 KiB
Go
67 lines
1.2 KiB
Go
package limit
|
|
|
|
import (
|
|
"context"
|
|
"sync/atomic"
|
|
"time"
|
|
)
|
|
|
|
// limiter is a super simple leaky-bucket Goroutine rate limiter.
|
|
//
|
|
// limiter _must_ be instantiated via the constructor ('New'), this is why the
|
|
// type itself is not exported.
|
|
type limiter struct {
|
|
jobsQueued atomic.Int32
|
|
jobsConcurrent atomic.Int32
|
|
jobsConcurrentMax int32
|
|
_ noCopy
|
|
}
|
|
|
|
func New(maxConcurrentJobs int32) *limiter {
|
|
return &limiter{
|
|
jobsConcurrentMax: maxConcurrentJobs,
|
|
}
|
|
}
|
|
|
|
func (l *limiter) Go(fn func()) {
|
|
l.jobsQueued.Add(1)
|
|
go func() {
|
|
l.waitOne()
|
|
l.jobsQueued.Add(-1)
|
|
l.jobsConcurrent.Add(1)
|
|
fn()
|
|
l.jobsConcurrent.Add(-1)
|
|
}()
|
|
}
|
|
|
|
func (l *limiter) Wait() {
|
|
for l.jobsConcurrent.Load() > 0 {
|
|
<-time.After(time.Millisecond)
|
|
}
|
|
for l.jobsQueued.Load() > 0 {
|
|
<-time.After(time.Millisecond)
|
|
}
|
|
}
|
|
|
|
func (l *limiter) WaitContext(ctx context.Context) error {
|
|
for {
|
|
select {
|
|
case <-ctx.Done():
|
|
return context.DeadlineExceeded
|
|
case <-time.After(time.Millisecond):
|
|
if l.jobsConcurrent.Load() > 0 {
|
|
continue
|
|
}
|
|
if l.jobsQueued.Load() > 0 {
|
|
continue
|
|
}
|
|
return nil
|
|
}
|
|
}
|
|
}
|
|
|
|
func (l *limiter) waitOne() {
|
|
for l.jobsConcurrent.Load() >= l.jobsConcurrentMax {
|
|
<-time.After(time.Millisecond)
|
|
}
|
|
}
|