Skip to content

Commit 50a1d31

Browse files
authoredNov 23, 2022
Prevent race condition on server connection
Probably not gonna occur in our case, but surely a good pattern to follow. Explained in : https://www.youtube.com/watch?v=DqHb5KBe7qI starting at 16:20
1 parent 8ca8b96 commit 50a1d31

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed
 

Diff for: ‎waitgroups/rate-limiting/server.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ func main() {
2121
}
2222
connections++
2323

24-
go func() {
24+
go func(con net.Conn) {
2525
defer func() {
26-
_ = conn.Close()
26+
_ = con.Close()
2727
atomic.AddInt32(&connections, -1)
2828
}()
2929
if atomic.LoadInt32(&connections) > 3 {
@@ -36,6 +36,6 @@ func main() {
3636
if err != nil {
3737
log.Fatalf("could not write to connection: %v", err)
3838
}
39-
}()
39+
}(conn)
4040
}
4141
}

0 commit comments

Comments
 (0)
Please sign in to comment.