Skip to content

Commit

Permalink
improve performance
Browse files Browse the repository at this point in the history
  • Loading branch information
GrosQuildu committed Apr 10, 2024
1 parent 88a2090 commit d032458
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 4 deletions.
49 changes: 46 additions & 3 deletions go/hanging-goroutine.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ var (
)

func main() {
req3(1)
fmt.Println(result)
fmt.Println(runtime.NumGoroutine())
req5_FP(1)
fmt.Println("Result: ", result)
fmt.Println("Goroutines (must be 1 for FPs):", runtime.NumGoroutine())
}

func req1(timeout time.Duration) string {
Expand Down Expand Up @@ -110,6 +110,49 @@ func req3_FP(timeout time.Duration) {
fmt.Println("finished req3")
}

func req4_FP(timeout time.Duration) string {
ch := make(chan string)
// ok: hanging-goroutine
go func() {
newData := test()
ch <- newData // block
}()
select {
case result = <- ch:
fmt.Println("case result")
return result
case <- time.After(timeout):
result = <- ch
fmt.Println("case time.Afer")
return ""
}
}

func req5_FP(timeout time.Duration) {
ch := make(chan string)
tick := time.Tick(100 * time.Millisecond)
quit := time.After(2 * time.Second)
// ok: hanging-goroutine
go func() {
newData := test()
ch <- newData // block
}()
for {
select {
case <-tick:
fmt.Print("|")
case <-quit:
result = <- ch
fmt.Println("\nquit")
return
default:
fmt.Print(".")
time.Sleep(50 * time.Millisecond)
}
}
}


func test() string {
time.Sleep(time.Second * 2)
return "very important data"
Expand Down
16 changes: 15 additions & 1 deletion go/hanging-goroutine.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,29 @@ rules:
- pattern-not-inside: |
$CHANNEL := make(..., $T)
...
# heuristics to limit FPs, we may miss some leaks because of these
- pattern-not: |
go func(...){
...
$CHANNEL <- $X
...
}(...)
...
select {
case ...
case ...:
...
... =<- $CHANNEL
... = <- $CHANNEL
...
}
- pattern-not: |
go func(...){
...
$CHANNEL <- $X
...
}(...)
...
select {
case ...
case ...:
Expand Down

0 comments on commit d032458

Please sign in to comment.