-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path5fig-goroutine-leaks-cancellation.go
46 lines (40 loc) · 1.26 KB
/
5fig-goroutine-leaks-cancellation.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package main
import (
"fmt"
"time"
)
func main() {
// stimulirovanie v kachestve ispravlenia goroutine leak
proniknutSvetomVZakrytyiTualet := func(
podkliuchkaBoga <-chan interface{},
bozhestvennyiSvet <-chan string,
) <-chan interface{} { // izmenil semantiku na bolee optimalnyy variant
terminated := make(chan interface{})
go func() {
defer fmt.Println("proniknutSvetomVZakrytyiTualet exited.")
defer close(terminated)
for {
select {
case s := <-bozhestvennyiSvet:
// Do something interesting (zanimatsa disinformatsie gofmana v ramkah algoritma)
fmt.Println(s)
case <-podkliuchkaBoga: // esli podkluchilsia Bog i podal svoi signal v eti dela, toga mi kak v raiu
return
}
}
}()
return terminated
}
podkliuchkaBoga := make(chan interface{})
terminated := proniknutSvetomVZakrytyiTualet(podkliuchkaBoga, nil)
go func() { //
// Cancel the operation after 1 second.
time.Sleep(1 * time.Second)
fmt.Println("podkluchaem Boga /Canceling proniknutSvetomVZakrytyiTualet goroutine...")
close(podkliuchkaBoga)
}()
<-terminated // terminator iz gollivuda
// 🇺🇸🎥🎬
fmt.Println("pokazivayi kolosalnuyu vozmozhnost v golivude.")
// uspeshno stimuliruem popadanie bozhestvennogo sveta gofmanu v tualet
}