Skip to content

Commit

Permalink
fix(apply): make validation concurrent
Browse files Browse the repository at this point in the history
Signed-off-by: Alessio Greggi <[email protected]>
  • Loading branch information
alegrey91 committed Apr 1, 2024
1 parent bf9c63c commit 0496542
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
11 changes: 10 additions & 1 deletion cmd/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,20 @@ var applyCmd = &cobra.Command{

var wg sync.WaitGroup
chErr := make(chan error, len(ruleSet.Rules))
chLimit := make(chan int, 10)
rulesFileIsValid := true

for _, rule := range ruleSet.Rules {
wg.Add(1)
go ipt.Validate(rule.Iface, rule.Proto, rule.Dport, rule.Saddr, rule.Sport, &wg, chErr)
// add slot to buffered channel
chLimit <- 1
go func(rule ipt.Rule, wg *sync.WaitGroup, chErr chan error, chLimit chan int) {
err := ipt.Validate(rule.Iface, rule.Proto, rule.Dport, rule.Saddr, rule.Sport)
wg.Done()
chErr <- err
// free slot from buffered channel
<-chLimit
}(rule, &wg, chErr, chLimit)
}
go func() {
wg.Wait()
Expand Down
7 changes: 2 additions & 5 deletions pkg/iptables/forward.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"net"
"strconv"
"strings"
"sync"

"github.com/alegrey91/fwdctl/internal/extractor"
)
Expand Down Expand Up @@ -59,10 +58,8 @@ func validateAddress(address string) error {
return nil
}

func Validate(iface string, proto string, dport int, saddr string, sport int, wg *sync.WaitGroup, errCh chan error) {
defer wg.Done()
err := validateForward(iface, proto, dport, saddr, sport)
errCh <- err
func Validate(iface string, proto string, dport int, saddr string, sport int) error {
return validateForward(iface, proto, dport, saddr, sport)
}

// validateForward returns both bool and error.
Expand Down

0 comments on commit 0496542

Please sign in to comment.