-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext_250301.go
More file actions
95 lines (81 loc) · 1.94 KB
/
context_250301.go
File metadata and controls
95 lines (81 loc) · 1.94 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package main
import (
"context"
"fmt"
"os"
"strconv"
"time"
)
// f1 고루틴을 만들고 실행.
// c1 컨텍스트가 4초가 지나기 전 Done함수를 호출하면 고루틴이 끝날 시간이 없음
func f1(t int) {
c1 := context.Background()
// WithCancle은 기존 Context를 사용해서 캔슬레이션으로 자식을 생성.
// CanceFunc()함수를 호출하면 부모가 자식을 가리키는 레퍼 삭제하고 타이머를 멈춤
c1, cancel := context.WithCancel(c1)
defer cancel() // context.CancelFunc() 값
go func() {
time.Sleep(5 * time.Second)
cancel()
}()
select {
case <-c1.Done():
fmt.Println("f1() Done:", c1.Err())
return
case r := <-time.After(time.Duration(t) * time.Second):
fmt.Println("f1():", r)
}
}
// f2
func f2(t int) {
c2 := context.Background()
// WithTimeout은 ctx, duration 매개변수 필요 -> 시간 지나면 자동으로 cancel()호출됨
c2, cancel := context.WithTimeout(c2, time.Duration(t)*time.Second)
defer cancel() // context.CancelFunc() 값
go func() {
time.Sleep(5 * time.Second)
cancel()
}()
select {
case <-c2.Done():
fmt.Println("f2() Done:", c2.Err())
return
case r := <-time.After(time.Duration(t) * time.Second):
fmt.Println("f2():", r)
}
return
}
func f3(t int) {
c3 := context.Background()
// 데드라인이 지나면 자동으로 cancel 호출
deadline := time.Now().Add(time.Duration(2*t) * time.Second)
c3, cancel := context.WithDeadline(c3, deadline)
defer cancel()
go func() {
time.Sleep(5 * time.Second)
cancel()
}()
select {
case <-c3.Done():
fmt.Println("f3() Done:", c3.Err())
return
case r := <-time.After(time.Duration(t) * time.Second):
fmt.Println("f3():", r)
}
return
}
func main() {
if len(os.Args) != 2 {
fmt.Println("Need Parameter. Please Write Delay")
return
}
delay, err := strconv.Atoi(os.Args[1])
if err != nil {
fmt.Println(err)
return
}
fmt.Println("Delay:", delay)
f1(delay)
f2(delay)
f3(delay)
}