Skip to content

Commit

Permalink
detec and fix race condition
Browse files Browse the repository at this point in the history
  • Loading branch information
jreisinger committed Jun 7, 2024
1 parent fe27977 commit 04863bc
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 16 deletions.
10 changes: 5 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
test:
go test -cover ./...
go test ./...

install: test
go install checkip.go
run: test
go run -race ./checkip.go 91.228.166.47

run: install
checkip 91.228.166.47
install: run
go install ./checkip.go
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ Checkip is easy to extend. If you want to add a new way of checking IP addresses
Typical workflow:

```
make run # test, install and run
make run # test and run
git commit -m "backwards compatible bug fix" main.go
Expand Down
30 changes: 20 additions & 10 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,36 @@ import (
)

// Run runs checks concurrently against the ippaddr.
func Run(checks []check.Func, ipaddr net.IP) (Checks, []error) {
var results Checks
var errors []error
func Run(checkFuncs []check.Func, ipaddr net.IP) (Checks, []error) {
var checksMu struct {
sync.Mutex
checks []check.Check
}
var errorsMu struct {
sync.Mutex
errors []error
}

var wg sync.WaitGroup
for _, chk := range checks {
for _, cf := range checkFuncs {
wg.Add(1)
go func(c check.Func) {
go func(cf check.Func) {
defer wg.Done()
r, err := c(ipaddr)
c, err := cf(ipaddr)
if err != nil {
errors = append(errors, err)
errorsMu.Lock()
errorsMu.errors = append(errorsMu.errors, err)
errorsMu.Unlock()
return
}
results = append(results, r)
}(chk)
checksMu.Lock()
checksMu.checks = append(checksMu.checks, c)
checksMu.Unlock()
}(cf)
}
wg.Wait()

return results, errors
return checksMu.checks, errorsMu.errors
}

// Checks are generic or security information provided by a Check.
Expand Down

0 comments on commit 04863bc

Please sign in to comment.