checkRun is a function that executes a list of CheckFunctions concurrently with a specified limit.
(ctx context.Context, functions []CheckFunction, decisionChan chan<- CheckResponse, limit int)
| 818 | |
| 819 | // checkRun is a function that executes a list of CheckFunctions concurrently with a specified limit. |
| 820 | func checkRun(ctx context.Context, functions []CheckFunction, decisionChan chan<- CheckResponse, limit int) func() { |
| 821 | // Create a channel that enforces the concurrency limit |
| 822 | cl := make(chan struct{}, limit) |
| 823 | var wg sync.WaitGroup |
| 824 | |
| 825 | // Define a helper function that calls a CheckFunction and sends the result to the decisionChan |
| 826 | check := func(child CheckFunction) { |
| 827 | result, err := child(ctx) |
| 828 | decisionChan <- CheckResponse{ |
| 829 | resp: result, |
| 830 | err: err, |
| 831 | } |
| 832 | // Once the CheckFunction is done, release the concurrency limit |
| 833 | <-cl |
| 834 | wg.Done() |
| 835 | } |
| 836 | |
| 837 | // Start a goroutine that iterates over the functions |
| 838 | wg.Add(1) |
| 839 | go func() { |
| 840 | run: |
| 841 | // Iterate over the functions |
| 842 | for _, fun := range functions { |
| 843 | child := fun |
| 844 | select { |
| 845 | // If the concurrency limit allows it, start the function in a new goroutine |
| 846 | case cl <- struct{}{}: |
| 847 | wg.Add(1) |
| 848 | go check(child) |
| 849 | // If the context is done, break the loop |
| 850 | case <-ctx.Done(): |
| 851 | break run |
| 852 | } |
| 853 | } |
| 854 | wg.Done() |
| 855 | }() |
| 856 | |
| 857 | // Return a cleanup function that waits for all goroutines to finish and then closes the concurrency limit channel |
| 858 | return func() { |
| 859 | wg.Wait() |
| 860 | close(cl) |
| 861 | } |
| 862 | } |
| 863 | |
| 864 | // checkFail is a helper function that returns a CheckFunction that always returns a denied PermissionCheckResponse |
| 865 | // with the provided error and an empty PermissionCheckResponseMetadata. |
no test coverage detected