package engine

import "context"

type rebuildLimiter struct {
	semaphore chan struct{}
}

func newRebuildLimiter(workersCount uint32) *rebuildLimiter {
	return &rebuildLimiter{
		semaphore: make(chan struct{}, workersCount),
	}
}

func (l *rebuildLimiter) AcquireWorkSlot(ctx context.Context) error {
	select {
	case l.semaphore <- struct{}{}:
		return nil
	case <-ctx.Done():
		return ctx.Err()
	}
}

func (l *rebuildLimiter) ReleaseWorkSlot() {
	<-l.semaphore
}