SlideShare a Scribd company logo
1
Private & Confidential
Channel in Go
Jaych Su - 3 Feb 2021
2
Private & Confidential 2
What is Channel
”
A channel provides a mechanism for
concurrently executing functions to
communicate by sending and receiving values
of a specified element type.
-- Golang Spec (//golang.org/ref/spec#Channel_types)
3
Private & Confidential 3
What behind Channel
QWhat will happen if read from/write to
a closed channel?
4
Private & Confidential 4
<- Closed Channel <-
What behind Channel
x, ok := <- chan
- [unread data] x is remaining value, ok is true
- [has been read] x is falsy value, ok is false
- [empty] x is falsy value, ok is false
5
Private & Confidential 5
<- Closed Channel <-
What behind Channel
panic
x, ok := <- chan
- [unread data] x is remaining value, ok is true
- [has been read] x is falsy value, ok is false
- [empty] x is falsy value, ok is false
chan <- x
6
Private & Confidential 6
type hchan struct {
// points to an array of
dataqsiz elements
buf unsafe.Pointer
// send index
sendx uint
// receive index
recvx uint
// lock protects all fields
in hchan
lock mutex
...
}
What behind Channel
https://github.com/golang/go/blob/master/src/runtime/chan.go#L32
7
Private & Confidential 7
What behind Channel
https://github.com/golang/go/blob/master/src/runtime/chan.go#L32
c := make(chan int, 4)
c <- 11
c <- 12
c <- 13
<- c
<- c
close(c)
<- c
<- c
c <- 14
type hchan struct {
// points to an array of
dataqsiz elements
buf unsafe.Pointer
// send index
sendx uint
// receive index
recvx uint
// lock protects all fields
in hchan
lock mutex
...
}
8
Private & Confidential 8
What behind Channel
https://github.com/golang/go/blob/master/src/runtime/chan.go#L32
c := make(chan int, 4)
c <- 11
c <- 12
c <- 13
<- c
<- c
close(c)
<- c
<- c
c <- 14
buf (doubly circular linked list)
lock
11
12
13
14
goroutine
sendx
recvx
type hchan struct {
// points to an array of
dataqsiz elements
buf unsafe.Pointer
// send index
sendx uint
// receive index
recvx uint
// lock protects all fields
in hchan
lock mutex
...
}
9
Private & Confidential 9
What behind Channel
https://github.com/golang/go/blob/master/src/runtime/chan.go#L32
c := make(chan int, 4)
c <- 11
c <- 12
c <- 13
<- c
<- c
close(c)
<- c
<- c
c <- 14
buf (doubly circular linked list)
lock
goroutine
sendx
recvx
type hchan struct {
// points to an array of
dataqsiz elements
buf unsafe.Pointer
// send index
sendx uint
// receive index
recvx uint
// lock protects all fields
in hchan
lock mutex
...
}
11
12
13
14
10
Private & Confidential 10
What behind Channel
https://github.com/golang/go/blob/master/src/runtime/chan.go#L32
c := make(chan int, 4)
c <- 11
c <- 12
c <- 13
<- c
<- c
close(c)
<- c
<- c
c <- 14
11
lock
goroutine buf (doubly circular linked list)
sendx
recvx
type hchan struct {
// points to an array of
dataqsiz elements
buf unsafe.Pointer
// send index
sendx uint
// receive index
recvx uint
// lock protects all fields
in hchan
lock mutex
...
}
12
13
14
11
Private & Confidential 11
What behind Channel
https://github.com/golang/go/blob/master/src/runtime/chan.go#L32
c := make(chan int, 4)
c <- 11
c <- 12
c <- 13
<- c
<- c
close(c)
<- c
<- c
c <- 14
11
lock
goroutine buf (doubly circular linked list)
sendx
recvx
type hchan struct {
// points to an array of
dataqsiz elements
buf unsafe.Pointer
// send index
sendx uint
// receive index
recvx uint
// lock protects all fields
in hchan
lock mutex
...
}
12
13
14
12
Private & Confidential 12
What behind Channel
https://github.com/golang/go/blob/master/src/runtime/chan.go#L32
c := make(chan int, 4)
c <- 11
c <- 12
c <- 13
<- c
<- c
close(c)
<- c
<- c
c <- 14
11
lock
goroutine buf (doubly circular linked list)
sendx
recvx
type hchan struct {
// points to an array of
dataqsiz elements
buf unsafe.Pointer
// send index
sendx uint
// receive index
recvx uint
// lock protects all fields
in hchan
lock mutex
...
}
12
13
14
13
Private & Confidential 13
What behind Channel
https://github.com/golang/go/blob/master/src/runtime/chan.go#L32
c := make(chan int, 4)
c <- 11
c <- 12
c <- 13
<- c
<- c
close(c)
<- c
<- c
c <- 14
11 12
lock
goroutine buf (doubly circular linked list)
sendx
recvx
type hchan struct {
// points to an array of
dataqsiz elements
buf unsafe.Pointer
// send index
sendx uint
// receive index
recvx uint
// lock protects all fields
in hchan
lock mutex
...
}
13
14
14
Private & Confidential 14
What behind Channel
https://github.com/golang/go/blob/master/src/runtime/chan.go#L32
c := make(chan int, 4)
c <- 11
c <- 12
c <- 13
<- c
<- c
close(c)
<- c
<- c
c <- 14
11 12
lock
goroutine buf (doubly circular linked list)
sendx
recvx
type hchan struct {
// points to an array of
dataqsiz elements
buf unsafe.Pointer
// send index
sendx uint
// receive index
recvx uint
// lock protects all fields
in hchan
lock mutex
...
}
13
14
15
Private & Confidential 15
What behind Channel
https://github.com/golang/go/blob/master/src/runtime/chan.go#L32
c := make(chan int, 4)
c <- 11
c <- 12
c <- 13
<- c
<- c
close(c)
<- c
<- c
c <- 14
11 12
lock
goroutine buf (doubly circular linked list)
sendx
recvx
type hchan struct {
// points to an array of
dataqsiz elements
buf unsafe.Pointer
// send index
sendx uint
// receive index
recvx uint
// lock protects all fields
in hchan
lock mutex
...
}
13
14
16
Private & Confidential 16
What behind Channel
https://github.com/golang/go/blob/master/src/runtime/chan.go#L32
c := make(chan int, 4)
c <- 11
c <- 12
c <- 13
<- c
<- c
close(c)
<- c
<- c
c <- 14
11 12
13
lock
goroutine buf (doubly circular linked list)
sendx
recvx
type hchan struct {
// points to an array of
dataqsiz elements
buf unsafe.Pointer
// send index
sendx uint
// receive index
recvx uint
// lock protects all fields
in hchan
lock mutex
...
}
14
17
Private & Confidential 17
What behind Channel
https://github.com/golang/go/blob/master/src/runtime/chan.go#L32
c := make(chan int, 4)
c <- 11
c <- 12
c <- 13
<- c
<- c
close(c)
<- c
<- c
c <- 14
11 12
13
lock
goroutine buf (doubly circular linked list)
sendx
recvx
type hchan struct {
// points to an array of
dataqsiz elements
buf unsafe.Pointer
// send index
sendx uint
// receive index
recvx uint
// lock protects all fields
in hchan
lock mutex
...
}
14
18
Private & Confidential 18
What behind Channel
https://github.com/golang/go/blob/master/src/runtime/chan.go#L32
c := make(chan int, 4)
c <- 11
c <- 12
c <- 13
<- c
<- c
close(c)
<- c
<- c
c <- 14
11 12
13
lock
goroutine buf (doubly circular linked list)
sendx
recvx
type hchan struct {
// points to an array of
dataqsiz elements
buf unsafe.Pointer
// send index
sendx uint
// receive index
recvx uint
// lock protects all fields
in hchan
lock mutex
...
}
14
19
Private & Confidential 19
What behind Channel
https://github.com/golang/go/blob/master/src/runtime/chan.go#L32
c := make(chan int, 4)
c <- 11
c <- 12
c <- 13
<- c // 11
<- c
close(c)
<- c
<- c
c <- 14
12
13
lock
goroutine buf (doubly circular linked list)
sendx
recvx
type hchan struct {
// points to an array of
dataqsiz elements
buf unsafe.Pointer
// send index
sendx uint
// receive index
recvx uint
// lock protects all fields
in hchan
lock mutex
...
}
14
11
20
Private & Confidential 20
What behind Channel
https://github.com/golang/go/blob/master/src/runtime/chan.go#L32
c := make(chan int, 4)
c <- 11
c <- 12
c <- 13
<- c // 11
<- c
close(c)
<- c
<- c
c <- 14
12
13
lock
goroutine buf (doubly circular linked list)
sendx
recvx
type hchan struct {
// points to an array of
dataqsiz elements
buf unsafe.Pointer
// send index
sendx uint
// receive index
recvx uint
// lock protects all fields
in hchan
lock mutex
...
}
14
11
21
Private & Confidential 21
What behind Channel
https://github.com/golang/go/blob/master/src/runtime/chan.go#L32
c := make(chan int, 4)
c <- 11
c <- 12
c <- 13
<- c // 11
<- c
close(c)
<- c
<- c
c <- 14
12
13
goroutine buf (doubly circular linked list)
sendx
recvx
lock
type hchan struct {
// points to an array of
dataqsiz elements
buf unsafe.Pointer
// send index
sendx uint
// receive index
recvx uint
// lock protects all fields
in hchan
lock mutex
...
}
14
11
22
Private & Confidential 22
What behind Channel
https://github.com/golang/go/blob/master/src/runtime/chan.go#L32
c := make(chan int, 4)
c <- 11
c <- 12
c <- 13
<- c // 11
<- c // 12
close(c)
<- c
<- c
c <- 14
13
lock
goroutine buf (doubly circular linked list)
sendx recvx
type hchan struct {
// points to an array of
dataqsiz elements
buf unsafe.Pointer
// send index
sendx uint
// receive index
recvx uint
// lock protects all fields
in hchan
lock mutex
...
}
14
11
12
23
Private & Confidential 23
What behind Channel
https://github.com/golang/go/blob/master/src/runtime/chan.go#L32
c := make(chan int, 4)
c <- 11
c <- 12
c <- 13
<- c // 11
<- c // 12
close(c)
<- c
<- c
c <- 14
13
goroutine buf (doubly circular linked list)
sendx recvx
lock
type hchan struct {
// points to an array of
dataqsiz elements
buf unsafe.Pointer
// send index
sendx uint
// receive index
recvx uint
// lock protects all fields
in hchan
lock mutex
...
}
14
11
12
24
Private & Confidential 24
What behind Channel
https://github.com/golang/go/blob/master/src/runtime/chan.go#L32
c := make(chan int, 4)
c <- 11
c <- 12
c <- 13
<- c // 11
<- c // 12
close(c)
<- c // 13
<- c
c <- 14
goroutine buf (doubly circular linked list)
sendx
recvx
lock
type hchan struct {
// points to an array of
dataqsiz elements
buf unsafe.Pointer
// send index
sendx uint
// receive index
recvx uint
// lock protects all fields
in hchan
lock mutex
...
}
14
11
12
13
25
Private & Confidential 25
What behind Channel
https://github.com/golang/go/blob/master/src/runtime/chan.go#L32
c := make(chan int, 4)
c <- 11
c <- 12
c <- 13
<- c // 11
<- c // 12
close(c)
<- c // 13
<- c // 0
c <- 14
goroutine buf (doubly circular linked list)
sendx
recvx
lock
type hchan struct {
// points to an array of
dataqsiz elements
buf unsafe.Pointer
// send index
sendx uint
// receive index
recvx uint
// lock protects all fields
in hchan
lock mutex
...
}
14
11
12
13
26
Private & Confidential 26
What behind Channel
c := make(chan int, 4)
c <- 11
c <- 12
c <- 13
<- c
<- c
close(c)
<- c
<- c
c <- 14
panic: send on closed channel
https://github.com/golang/go/blob/master/src/runtime/chan.go#L32
goroutine buf (doubly circular linked list)
sendx
recvx
lock
type hchan struct {
// points to an array of
dataqsiz elements
buf unsafe.Pointer
// send index
sendx uint
// receive index
recvx uint
// lock protects all fields
in hchan
lock mutex
...
}
27
Private & Confidential 27
chan <- x
<- Closed Channel <-
What behind Channel
panic
x, ok := <- chan
- [unread data] x is remaining value, ok is true
- [has been read] x is falsy value, ok is false
- [empty] x is falsy value, ok is false
28
Private & Confidential 28
What behind Channel
”
Do not communicate by sharing memory;
instead, share memory by communicating.
-- Someone else
memory
goroutine
goroutine
goroutine
memory memory
29
Private & Confidential 29
What behind Channel
”
Do not communicate by sharing memory;
instead, share memory by communicating.
-- Someone else
memory
goroutine
goroutine
goroutine
memory memory
30
Private & Confidential 30
What behind Channel
”
Do not communicate by sharing memory;
instead, share memory by communicating.
-- Someone else
memory
goroutine
goroutine
goroutine
memory memory
31
Private & Confidential 31
What behind Channel
”
Do not communicate by sharing memory;
instead, share memory by communicating.
-- Someone else
memory
goroutine
goroutine
goroutine
memory memory
32
Private & Confidential 32
What crashes Channel
QWhat crashes Channel?
33
Private & Confidential 33
What crashes Channel
QWhat crashes Channel?
What if buf is full?
34
Private & Confidential 34
type hchan struct {
// list of send waiters
sendq waitq
// list of recv waiters
recvq waitq
buf unsafe.Pointer
sendx uint
recvx uint
lock mutex
...
}
What crashes Channel
https://github.com/golang/go/blob/master/src/runtime/chan.go#L32
buf (doubly circular linked list)
lock
sendx
recvx
11 12
14 13
35
Private & Confidential 35
type hchan struct {
// list of send waiters
sendq waitq
// list of recv waiters
recvq waitq
buf unsafe.Pointer
sendx uint
recvx uint
lock mutex
...
}
What crashes Channel
https://github.com/golang/go/blob/master/src/runtime/chan.go#L32
buf (doubly circular linked list)
lock
sendx
recvx
11 12
14 13
sendq
recvq
36
Private & Confidential 36
type hchan struct {
// list of send waiters
sendq waitq
// list of recv waiters
recvq waitq
buf unsafe.Pointer
sendx uint
recvx uint
lock mutex
...
}
What crashes Channel
https://github.com/golang/go/blob/master/src/runtime/chan.go#L32
buf (doubly circular linked list)
lock
scheduling
sendx
recvx
11 12
14 13
sendq
recvq
37
Private & Confidential 37
type hchan struct {
// list of send waiters
sendq waitq
// list of recv waiters
recvq waitq
buf unsafe.Pointer
sendx uint
recvx uint
lock mutex
...
}
What crashes Channel
https://github.com/golang/go/blob/master/src/runtime/chan.go#L32
buf (doubly circular linked list)
lock
scheduling
sendx
recvx
11 12
14 13
sendq
recvq
c <- 15
goroutine
38
Private & Confidential 38
type hchan struct {
// list of send waiters
sendq waitq
// list of recv waiters
recvq waitq
buf unsafe.Pointer
sendx uint
recvx uint
lock mutex
...
}
What crashes Channel
https://github.com/golang/go/blob/master/src/runtime/chan.go#L32
buf (doubly circular linked list)
lock
scheduling
sendx
recvx
11 12
14 13
sendq
recvq
c <- 15
goroutine
39
Private & Confidential 39
type hchan struct {
// list of send waiters
sendq waitq
// list of recv waiters
recvq waitq
buf unsafe.Pointer
sendx uint
recvx uint
lock mutex
...
}
What crashes Channel
https://github.com/golang/go/blob/master/src/runtime/chan.go#L32
buf (doubly circular linked list)
lock
scheduling
sendx
recvx
11 12
14 13
sendq
recvq
c <- 15
goroutine
<- c
goroutine
40
Private & Confidential 40
type hchan struct {
// list of send waiters
sendq waitq
// list of recv waiters
recvq waitq
buf unsafe.Pointer
sendx uint
recvx uint
lock mutex
...
}
What crashes Channel
https://github.com/golang/go/blob/master/src/runtime/chan.go#L32
buf (doubly circular linked list)
lock
scheduling
sendx
recvx
11 12
14 13
sendq
recvq
c <- 15
goroutine
<- c
goroutine
41
Private & Confidential 41
type hchan struct {
// list of send waiters
sendq waitq
// list of recv waiters
recvq waitq
buf unsafe.Pointer
sendx uint
recvx uint
lock mutex
...
}
What crashes Channel
https://github.com/golang/go/blob/master/src/runtime/chan.go#L32
buf (doubly circular linked list)
lock
scheduling
recvx
sendx
12
14 13
sendq
recvq
c <- 15
goroutine
<- c
// 11
goroutine
42
Private & Confidential 42
type hchan struct {
// list of send waiters
sendq waitq
// list of recv waiters
recvq waitq
buf unsafe.Pointer
sendx uint
recvx uint
lock mutex
...
}
What crashes Channel
https://github.com/golang/go/blob/master/src/runtime/chan.go#L32
buf (doubly circular linked list)
scheduling
12
14 13
sendq
recvq
c <- 15
goroutine
<- c
// 11
goroutine
lock
recvx
sendx
43
Private & Confidential 43
type hchan struct {
// list of send waiters
sendq waitq
// list of recv waiters
recvq waitq
buf unsafe.Pointer
sendx uint
recvx uint
lock mutex
...
}
What crashes Channel
https://github.com/golang/go/blob/master/src/runtime/chan.go#L32
buf (doubly circular linked list)
scheduling
12
14 13
sendq
recvq
c <- 15
goroutine
lock
recvx
sendx
44
Private & Confidential 44
type hchan struct {
// list of send waiters
sendq waitq
// list of recv waiters
recvq waitq
buf unsafe.Pointer
sendx uint
recvx uint
lock mutex
...
}
What crashes Channel
https://github.com/golang/go/blob/master/src/runtime/chan.go#L32
buf (doubly circular linked list)
scheduling
12
14 13
sendq
recvq
c <- 15
goroutine
lock
recvx
sendx
45
Private & Confidential 45
type hchan struct {
// list of send waiters
sendq waitq
// list of recv waiters
recvq waitq
buf unsafe.Pointer
sendx uint
recvx uint
lock mutex
...
}
What crashes Channel
https://github.com/golang/go/blob/master/src/runtime/chan.go#L32
buf (doubly circular linked list)
scheduling
12
14 13
sendq
recvq
c <- 15
goroutine
lock
recvx
sendx
46
Private & Confidential 46
type hchan struct {
// list of send waiters
sendq waitq
// list of recv waiters
recvq waitq
buf unsafe.Pointer
sendx uint
recvx uint
lock mutex
...
}
What crashes Channel
https://github.com/golang/go/blob/master/src/runtime/chan.go#L32
buf (doubly circular linked list)
scheduling
sendx
recvx
15 12
14 13
sendq
recvq
goroutine
lock
47
Private & Confidential 47
type hchan struct {
// list of send waiters
sendq waitq
// list of recv waiters
recvq waitq
buf unsafe.Pointer
sendx uint
recvx uint
lock mutex
...
}
What crashes Channel
https://github.com/golang/go/blob/master/src/runtime/chan.go#L32
buf (doubly circular linked list)
scheduling
15 12
14 13
sendq
recvq
goroutine
lock
sendx
recvx
48
Private & Confidential 48
type hchan struct {
// list of send waiters
sendq waitq
// list of recv waiters
recvq waitq
buf unsafe.Pointer
sendx uint
recvx uint
lock mutex
...
}
What crashes Channel
https://github.com/golang/go/blob/master/src/runtime/chan.go#L32
buf (doubly circular linked list)
scheduling
15 12
14 13
sendq
recvq
lock
sendx
recvx
49
Private & Confidential 49
1. communication between goroutine
2. multiplexing
3. timeout handling
4. range channel
5. read-only/write-only channel
When uses Channel
50
Private & Confidential 50
func main() {
var c chan int
go func() {
c <- 1
}()
<-c
}
1. communication between goroutine
2. multiplexing
3. timeout handling
4. range channel
5. read-only/write-only channel
When uses Channel
51
Private & Confidential 51
for {
select {
case x, ok := <-c1:
...
case x, ok := <-c2:
...
default:
...
}
}
1. communication between goroutine
2. multiplexing
3. timeout handling
4. range channel
5. read-only/write-only channel
When uses Channel
52
Private & Confidential 52
select {
case <- c:
// get from channel c
case <- time.After(2 * time.Second)
// timeout when getting from channel c
}
When uses Channel
1. communication between goroutine
2. multiplexing
3. timeout handling
4. range channel
5. read-only/write-only channel
53
Private & Confidential 53
1. communication between goroutine
2. multiplexing
3. timeout handling
4. range channel
5. read-only/write-only channel
When uses Channel
func consumer(c chan int) {
for x := range c {
...
}
}
func producer(c chan int) {
for _, x := range values {
c <- x
}
}
54
Private & Confidential 54
// usually used for declaration
func foo(ch chan<- int) <-chan int {
...
}
When uses Channel
1. communication between goroutine
2. multiplexing
3. timeout handling
4. range channel
5. read-only/write-only channel
55
Private & Confidential 55
# [val, pre, nxt]
dummy = []
dummy[:] = [None, dummy, dummy]
[PLUS] Something interesting
56
Private & Confidential 56
# [val, pre, nxt]
dummy = []
dummy[:] = [None, dummy, dummy]
node1 = [1, None, None]
node1[1] = node1[2] = dummy
dummy[1] = dummy[2] = node1
node2 = [2, None, None]
node2[1] = node1
node2[2] = node1[2]
node1[2][1] = node2
node1[2] = node2
[PLUS] Something interesting
57
Private & Confidential 57
# [val, pre, nxt]
dummy = []
dummy[:] = [None, dummy, dummy]
node1 = [1, None, None]
node1[1] = node1[2] = dummy
dummy[1] = dummy[2] = node1
node2 = [2, None, None]
node2[1] = node1
node2[2] = node1[2]
node1[2][1] = node2
node1[2] = node2
[PLUS] Something interesting
doubly circular linked list
58
Private & Confidential 58
# [val, pre, nxt]
dummy = []
dummy[:] = [None, dummy, dummy]
node1 = [1, None, None]
node1[1] = node1[2] = dummy
dummy[1] = dummy[2] = node1
node2 = [2, None, None]
node2[1] = node1
node2[2] = node1[2]
node1[2][1] = node2
node1[2] = node2
[PLUS] Something interesting
doubly circular linked list
D
59
Private & Confidential 59
# [val, pre, nxt]
dummy = []
dummy[:] = [None, dummy, dummy]
node1 = [1, None, None]
node1[1] = node1[2] = dummy
dummy[1] = dummy[2] = node1
node2 = [2, None, None]
node2[1] = node1
node2[2] = node1[2]
node1[2][1] = node2
node1[2] = node2
[PLUS] Something interesting
doubly circular linked list
1
D
60
Private & Confidential 60
# [val, pre, nxt]
dummy = []
dummy[:] = [None, dummy, dummy]
node1 = [1, None, None]
node1[1] = node1[2] = dummy
dummy[1] = dummy[2] = node1
node2 = [2, None, None]
node2[1] = node1
node2[2] = node1[2]
node1[2][1] = node2
node1[2] = node2
[PLUS] Something interesting
doubly circular linked list
2
1
D
61
Private & Confidential 61
1. What is Channel
2. What behind Channel
3. What crashes Channel
4. When uses Channel
5. [PLUS] Something interesting
62
Private & Confidential 62
1. What is Channel
2. What behind Channel
3. What crashes Channel
4. When uses Channel
5. [PLUS] Something interesting
63
Private & Confidential 63
1. What is Channel
2. What behind Channel
3. What crashes Channel
4. When uses Channel
5. [PLUS] Something interesting
64
Private & Confidential 64
1. What is Channel
2. What behind Channel
3. What crashes Channel
4. When uses Channel
5. [PLUS] Something interesting
65
Private & Confidential 65
1. What is Channel
2. What behind Channel
3. What crashes Channel
4. When uses Channel
5. [PLUS] Something interesting
66
Private & Confidential 66
Thank you!

More Related Content

What's hot

Introduction to Redis
Introduction to RedisIntroduction to Redis
Introduction to Redis
Dvir Volk
 
Git Tutorial For Beginners | What is Git and GitHub? | DevOps Tools | DevOps ...
Git Tutorial For Beginners | What is Git and GitHub? | DevOps Tools | DevOps ...Git Tutorial For Beginners | What is Git and GitHub? | DevOps Tools | DevOps ...
Git Tutorial For Beginners | What is Git and GitHub? | DevOps Tools | DevOps ...
Simplilearn
 

What's hot (20)

Deep dive in Docker Overlay Networks
Deep dive in Docker Overlay NetworksDeep dive in Docker Overlay Networks
Deep dive in Docker Overlay Networks
 
ClickHouse Keeper
ClickHouse KeeperClickHouse Keeper
ClickHouse Keeper
 
Adventures with the ClickHouse ReplacingMergeTree Engine
Adventures with the ClickHouse ReplacingMergeTree EngineAdventures with the ClickHouse ReplacingMergeTree Engine
Adventures with the ClickHouse ReplacingMergeTree Engine
 
Introduction to Redis
Introduction to RedisIntroduction to Redis
Introduction to Redis
 
ClickHouse on Kubernetes, by Alexander Zaitsev, Altinity CTO
ClickHouse on Kubernetes, by Alexander Zaitsev, Altinity CTOClickHouse on Kubernetes, by Alexander Zaitsev, Altinity CTO
ClickHouse on Kubernetes, by Alexander Zaitsev, Altinity CTO
 
OVN DBs HA with scale test
OVN DBs HA with scale testOVN DBs HA with scale test
OVN DBs HA with scale test
 
Ceph and RocksDB
Ceph and RocksDBCeph and RocksDB
Ceph and RocksDB
 
Git Tutorial For Beginners | What is Git and GitHub? | DevOps Tools | DevOps ...
Git Tutorial For Beginners | What is Git and GitHub? | DevOps Tools | DevOps ...Git Tutorial For Beginners | What is Git and GitHub? | DevOps Tools | DevOps ...
Git Tutorial For Beginners | What is Git and GitHub? | DevOps Tools | DevOps ...
 
ClickHouse Monitoring 101: What to monitor and how
ClickHouse Monitoring 101: What to monitor and howClickHouse Monitoring 101: What to monitor and how
ClickHouse Monitoring 101: What to monitor and how
 
Go Concurrency
Go ConcurrencyGo Concurrency
Go Concurrency
 
HTTP Analytics for 6M requests per second using ClickHouse, by Alexander Boc...
HTTP Analytics for 6M requests per second using ClickHouse, by  Alexander Boc...HTTP Analytics for 6M requests per second using ClickHouse, by  Alexander Boc...
HTTP Analytics for 6M requests per second using ClickHouse, by Alexander Boc...
 
Troubleshooting common oslo.messaging and RabbitMQ issues
Troubleshooting common oslo.messaging and RabbitMQ issuesTroubleshooting common oslo.messaging and RabbitMQ issues
Troubleshooting common oslo.messaging and RabbitMQ issues
 
Grafana vs Kibana
Grafana vs KibanaGrafana vs Kibana
Grafana vs Kibana
 
Best practices for MySQL/MariaDB Server/Percona Server High Availability
Best practices for MySQL/MariaDB Server/Percona Server High AvailabilityBest practices for MySQL/MariaDB Server/Percona Server High Availability
Best practices for MySQL/MariaDB Server/Percona Server High Availability
 
Real-time, Exactly-once Data Ingestion from Kafka to ClickHouse at eBay
Real-time, Exactly-once Data Ingestion from Kafka to ClickHouse at eBayReal-time, Exactly-once Data Ingestion from Kafka to ClickHouse at eBay
Real-time, Exactly-once Data Ingestion from Kafka to ClickHouse at eBay
 
HTTP2 and gRPC
HTTP2 and gRPCHTTP2 and gRPC
HTTP2 and gRPC
 
BPF Internals (eBPF)
BPF Internals (eBPF)BPF Internals (eBPF)
BPF Internals (eBPF)
 
CDC Stream Processing with Apache Flink
CDC Stream Processing with Apache FlinkCDC Stream Processing with Apache Flink
CDC Stream Processing with Apache Flink
 
Data warehouse on Kubernetes - gentle intro to Clickhouse Operator, by Robert...
Data warehouse on Kubernetes - gentle intro to Clickhouse Operator, by Robert...Data warehouse on Kubernetes - gentle intro to Clickhouse Operator, by Robert...
Data warehouse on Kubernetes - gentle intro to Clickhouse Operator, by Robert...
 
이벤트 기반 분산 시스템을 향한 여정
이벤트 기반 분산 시스템을 향한 여정이벤트 기반 분산 시스템을 향한 여정
이벤트 기반 분산 시스템을 향한 여정
 

Similar to Channel in Go

Using Git as your VCS with Bioconductor
Using Git as your VCS with BioconductorUsing Git as your VCS with Bioconductor
Using Git as your VCS with Bioconductor
timyates
 

Similar to Channel in Go (20)

Java/Scala Lab: Руслан Шевченко - Implementation of CSP (Communication Sequen...
Java/Scala Lab: Руслан Шевченко - Implementation of CSP (Communication Sequen...Java/Scala Lab: Руслан Шевченко - Implementation of CSP (Communication Sequen...
Java/Scala Lab: Руслан Шевченко - Implementation of CSP (Communication Sequen...
 
Sockets
SocketsSockets
Sockets
 
scala-gopher: async implementation of CSP for scala
scala-gopher:  async implementation of CSP  for  scalascala-gopher:  async implementation of CSP  for  scala
scala-gopher: async implementation of CSP for scala
 
Kernel Recipes 2014 - NDIV: a low overhead network traffic diverter
Kernel Recipes 2014 - NDIV: a low overhead network traffic diverterKernel Recipes 2014 - NDIV: a low overhead network traffic diverter
Kernel Recipes 2014 - NDIV: a low overhead network traffic diverter
 
03 - Refresher on buffer overflow in the old days
03 - Refresher on buffer overflow in the old days03 - Refresher on buffer overflow in the old days
03 - Refresher on buffer overflow in the old days
 
Basics of sockets
Basics of socketsBasics of sockets
Basics of sockets
 
OpenSSL Basic Function Call Flow
OpenSSL Basic Function Call FlowOpenSSL Basic Function Call Flow
OpenSSL Basic Function Call Flow
 
Docker.io
Docker.ioDocker.io
Docker.io
 
Using Git as your VCS with Bioconductor
Using Git as your VCS with BioconductorUsing Git as your VCS with Bioconductor
Using Git as your VCS with Bioconductor
 
From Zero To Production (NixOS, Erlang) @ Erlang Factory SF 2016
From Zero To Production (NixOS, Erlang) @ Erlang Factory SF 2016From Zero To Production (NixOS, Erlang) @ Erlang Factory SF 2016
From Zero To Production (NixOS, Erlang) @ Erlang Factory SF 2016
 
PPT
PPTPPT
PPT
 
Querying 1.8 billion reddit comments with python
Querying 1.8 billion reddit comments with pythonQuerying 1.8 billion reddit comments with python
Querying 1.8 billion reddit comments with python
 
From Zero to Application Delivery with NixOS
From Zero to Application Delivery with NixOSFrom Zero to Application Delivery with NixOS
From Zero to Application Delivery with NixOS
 
tit
tittit
tit
 
HOW TO DEAL WITH BLOCKING CODE WITHIN ASYNCIO EVENT LOOP
HOW TO DEAL WITH BLOCKING CODE WITHIN ASYNCIO EVENT LOOPHOW TO DEAL WITH BLOCKING CODE WITHIN ASYNCIO EVENT LOOP
HOW TO DEAL WITH BLOCKING CODE WITHIN ASYNCIO EVENT LOOP
 
Docker Networking with New Ipvlan and Macvlan Drivers
Docker Networking with New Ipvlan and Macvlan DriversDocker Networking with New Ipvlan and Macvlan Drivers
Docker Networking with New Ipvlan and Macvlan Drivers
 
Netty from the trenches
Netty from the trenchesNetty from the trenches
Netty from the trenches
 
Capturing NIC and Kernel TX and RX Timestamps for Packets in Go
Capturing NIC and Kernel TX and RX Timestamps for Packets in GoCapturing NIC and Kernel TX and RX Timestamps for Packets in Go
Capturing NIC and Kernel TX and RX Timestamps for Packets in Go
 
Networking and Go: An Epic Journey
Networking and Go: An Epic JourneyNetworking and Go: An Epic Journey
Networking and Go: An Epic Journey
 
Os lab final
Os lab finalOs lab final
Os lab final
 

Recently uploaded

Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo DiehlFuture Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Peter Udo Diehl
 
Search and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical FuturesSearch and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical Futures
Bhaskar Mitra
 

Recently uploaded (20)

FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
 
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo DiehlFuture Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
 
UiPath Test Automation using UiPath Test Suite series, part 2
UiPath Test Automation using UiPath Test Suite series, part 2UiPath Test Automation using UiPath Test Suite series, part 2
UiPath Test Automation using UiPath Test Suite series, part 2
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
 
Quantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIsQuantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIs
 
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptxIOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
 
Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*
 
Search and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical FuturesSearch and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical Futures
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
 
PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
 

Channel in Go

  • 1. 1 Private & Confidential Channel in Go Jaych Su - 3 Feb 2021
  • 2. 2 Private & Confidential 2 What is Channel ” A channel provides a mechanism for concurrently executing functions to communicate by sending and receiving values of a specified element type. -- Golang Spec (//golang.org/ref/spec#Channel_types)
  • 3. 3 Private & Confidential 3 What behind Channel QWhat will happen if read from/write to a closed channel?
  • 4. 4 Private & Confidential 4 <- Closed Channel <- What behind Channel x, ok := <- chan - [unread data] x is remaining value, ok is true - [has been read] x is falsy value, ok is false - [empty] x is falsy value, ok is false
  • 5. 5 Private & Confidential 5 <- Closed Channel <- What behind Channel panic x, ok := <- chan - [unread data] x is remaining value, ok is true - [has been read] x is falsy value, ok is false - [empty] x is falsy value, ok is false chan <- x
  • 6. 6 Private & Confidential 6 type hchan struct { // points to an array of dataqsiz elements buf unsafe.Pointer // send index sendx uint // receive index recvx uint // lock protects all fields in hchan lock mutex ... } What behind Channel https://github.com/golang/go/blob/master/src/runtime/chan.go#L32
  • 7. 7 Private & Confidential 7 What behind Channel https://github.com/golang/go/blob/master/src/runtime/chan.go#L32 c := make(chan int, 4) c <- 11 c <- 12 c <- 13 <- c <- c close(c) <- c <- c c <- 14 type hchan struct { // points to an array of dataqsiz elements buf unsafe.Pointer // send index sendx uint // receive index recvx uint // lock protects all fields in hchan lock mutex ... }
  • 8. 8 Private & Confidential 8 What behind Channel https://github.com/golang/go/blob/master/src/runtime/chan.go#L32 c := make(chan int, 4) c <- 11 c <- 12 c <- 13 <- c <- c close(c) <- c <- c c <- 14 buf (doubly circular linked list) lock 11 12 13 14 goroutine sendx recvx type hchan struct { // points to an array of dataqsiz elements buf unsafe.Pointer // send index sendx uint // receive index recvx uint // lock protects all fields in hchan lock mutex ... }
  • 9. 9 Private & Confidential 9 What behind Channel https://github.com/golang/go/blob/master/src/runtime/chan.go#L32 c := make(chan int, 4) c <- 11 c <- 12 c <- 13 <- c <- c close(c) <- c <- c c <- 14 buf (doubly circular linked list) lock goroutine sendx recvx type hchan struct { // points to an array of dataqsiz elements buf unsafe.Pointer // send index sendx uint // receive index recvx uint // lock protects all fields in hchan lock mutex ... } 11 12 13 14
  • 10. 10 Private & Confidential 10 What behind Channel https://github.com/golang/go/blob/master/src/runtime/chan.go#L32 c := make(chan int, 4) c <- 11 c <- 12 c <- 13 <- c <- c close(c) <- c <- c c <- 14 11 lock goroutine buf (doubly circular linked list) sendx recvx type hchan struct { // points to an array of dataqsiz elements buf unsafe.Pointer // send index sendx uint // receive index recvx uint // lock protects all fields in hchan lock mutex ... } 12 13 14
  • 11. 11 Private & Confidential 11 What behind Channel https://github.com/golang/go/blob/master/src/runtime/chan.go#L32 c := make(chan int, 4) c <- 11 c <- 12 c <- 13 <- c <- c close(c) <- c <- c c <- 14 11 lock goroutine buf (doubly circular linked list) sendx recvx type hchan struct { // points to an array of dataqsiz elements buf unsafe.Pointer // send index sendx uint // receive index recvx uint // lock protects all fields in hchan lock mutex ... } 12 13 14
  • 12. 12 Private & Confidential 12 What behind Channel https://github.com/golang/go/blob/master/src/runtime/chan.go#L32 c := make(chan int, 4) c <- 11 c <- 12 c <- 13 <- c <- c close(c) <- c <- c c <- 14 11 lock goroutine buf (doubly circular linked list) sendx recvx type hchan struct { // points to an array of dataqsiz elements buf unsafe.Pointer // send index sendx uint // receive index recvx uint // lock protects all fields in hchan lock mutex ... } 12 13 14
  • 13. 13 Private & Confidential 13 What behind Channel https://github.com/golang/go/blob/master/src/runtime/chan.go#L32 c := make(chan int, 4) c <- 11 c <- 12 c <- 13 <- c <- c close(c) <- c <- c c <- 14 11 12 lock goroutine buf (doubly circular linked list) sendx recvx type hchan struct { // points to an array of dataqsiz elements buf unsafe.Pointer // send index sendx uint // receive index recvx uint // lock protects all fields in hchan lock mutex ... } 13 14
  • 14. 14 Private & Confidential 14 What behind Channel https://github.com/golang/go/blob/master/src/runtime/chan.go#L32 c := make(chan int, 4) c <- 11 c <- 12 c <- 13 <- c <- c close(c) <- c <- c c <- 14 11 12 lock goroutine buf (doubly circular linked list) sendx recvx type hchan struct { // points to an array of dataqsiz elements buf unsafe.Pointer // send index sendx uint // receive index recvx uint // lock protects all fields in hchan lock mutex ... } 13 14
  • 15. 15 Private & Confidential 15 What behind Channel https://github.com/golang/go/blob/master/src/runtime/chan.go#L32 c := make(chan int, 4) c <- 11 c <- 12 c <- 13 <- c <- c close(c) <- c <- c c <- 14 11 12 lock goroutine buf (doubly circular linked list) sendx recvx type hchan struct { // points to an array of dataqsiz elements buf unsafe.Pointer // send index sendx uint // receive index recvx uint // lock protects all fields in hchan lock mutex ... } 13 14
  • 16. 16 Private & Confidential 16 What behind Channel https://github.com/golang/go/blob/master/src/runtime/chan.go#L32 c := make(chan int, 4) c <- 11 c <- 12 c <- 13 <- c <- c close(c) <- c <- c c <- 14 11 12 13 lock goroutine buf (doubly circular linked list) sendx recvx type hchan struct { // points to an array of dataqsiz elements buf unsafe.Pointer // send index sendx uint // receive index recvx uint // lock protects all fields in hchan lock mutex ... } 14
  • 17. 17 Private & Confidential 17 What behind Channel https://github.com/golang/go/blob/master/src/runtime/chan.go#L32 c := make(chan int, 4) c <- 11 c <- 12 c <- 13 <- c <- c close(c) <- c <- c c <- 14 11 12 13 lock goroutine buf (doubly circular linked list) sendx recvx type hchan struct { // points to an array of dataqsiz elements buf unsafe.Pointer // send index sendx uint // receive index recvx uint // lock protects all fields in hchan lock mutex ... } 14
  • 18. 18 Private & Confidential 18 What behind Channel https://github.com/golang/go/blob/master/src/runtime/chan.go#L32 c := make(chan int, 4) c <- 11 c <- 12 c <- 13 <- c <- c close(c) <- c <- c c <- 14 11 12 13 lock goroutine buf (doubly circular linked list) sendx recvx type hchan struct { // points to an array of dataqsiz elements buf unsafe.Pointer // send index sendx uint // receive index recvx uint // lock protects all fields in hchan lock mutex ... } 14
  • 19. 19 Private & Confidential 19 What behind Channel https://github.com/golang/go/blob/master/src/runtime/chan.go#L32 c := make(chan int, 4) c <- 11 c <- 12 c <- 13 <- c // 11 <- c close(c) <- c <- c c <- 14 12 13 lock goroutine buf (doubly circular linked list) sendx recvx type hchan struct { // points to an array of dataqsiz elements buf unsafe.Pointer // send index sendx uint // receive index recvx uint // lock protects all fields in hchan lock mutex ... } 14 11
  • 20. 20 Private & Confidential 20 What behind Channel https://github.com/golang/go/blob/master/src/runtime/chan.go#L32 c := make(chan int, 4) c <- 11 c <- 12 c <- 13 <- c // 11 <- c close(c) <- c <- c c <- 14 12 13 lock goroutine buf (doubly circular linked list) sendx recvx type hchan struct { // points to an array of dataqsiz elements buf unsafe.Pointer // send index sendx uint // receive index recvx uint // lock protects all fields in hchan lock mutex ... } 14 11
  • 21. 21 Private & Confidential 21 What behind Channel https://github.com/golang/go/blob/master/src/runtime/chan.go#L32 c := make(chan int, 4) c <- 11 c <- 12 c <- 13 <- c // 11 <- c close(c) <- c <- c c <- 14 12 13 goroutine buf (doubly circular linked list) sendx recvx lock type hchan struct { // points to an array of dataqsiz elements buf unsafe.Pointer // send index sendx uint // receive index recvx uint // lock protects all fields in hchan lock mutex ... } 14 11
  • 22. 22 Private & Confidential 22 What behind Channel https://github.com/golang/go/blob/master/src/runtime/chan.go#L32 c := make(chan int, 4) c <- 11 c <- 12 c <- 13 <- c // 11 <- c // 12 close(c) <- c <- c c <- 14 13 lock goroutine buf (doubly circular linked list) sendx recvx type hchan struct { // points to an array of dataqsiz elements buf unsafe.Pointer // send index sendx uint // receive index recvx uint // lock protects all fields in hchan lock mutex ... } 14 11 12
  • 23. 23 Private & Confidential 23 What behind Channel https://github.com/golang/go/blob/master/src/runtime/chan.go#L32 c := make(chan int, 4) c <- 11 c <- 12 c <- 13 <- c // 11 <- c // 12 close(c) <- c <- c c <- 14 13 goroutine buf (doubly circular linked list) sendx recvx lock type hchan struct { // points to an array of dataqsiz elements buf unsafe.Pointer // send index sendx uint // receive index recvx uint // lock protects all fields in hchan lock mutex ... } 14 11 12
  • 24. 24 Private & Confidential 24 What behind Channel https://github.com/golang/go/blob/master/src/runtime/chan.go#L32 c := make(chan int, 4) c <- 11 c <- 12 c <- 13 <- c // 11 <- c // 12 close(c) <- c // 13 <- c c <- 14 goroutine buf (doubly circular linked list) sendx recvx lock type hchan struct { // points to an array of dataqsiz elements buf unsafe.Pointer // send index sendx uint // receive index recvx uint // lock protects all fields in hchan lock mutex ... } 14 11 12 13
  • 25. 25 Private & Confidential 25 What behind Channel https://github.com/golang/go/blob/master/src/runtime/chan.go#L32 c := make(chan int, 4) c <- 11 c <- 12 c <- 13 <- c // 11 <- c // 12 close(c) <- c // 13 <- c // 0 c <- 14 goroutine buf (doubly circular linked list) sendx recvx lock type hchan struct { // points to an array of dataqsiz elements buf unsafe.Pointer // send index sendx uint // receive index recvx uint // lock protects all fields in hchan lock mutex ... } 14 11 12 13
  • 26. 26 Private & Confidential 26 What behind Channel c := make(chan int, 4) c <- 11 c <- 12 c <- 13 <- c <- c close(c) <- c <- c c <- 14 panic: send on closed channel https://github.com/golang/go/blob/master/src/runtime/chan.go#L32 goroutine buf (doubly circular linked list) sendx recvx lock type hchan struct { // points to an array of dataqsiz elements buf unsafe.Pointer // send index sendx uint // receive index recvx uint // lock protects all fields in hchan lock mutex ... }
  • 27. 27 Private & Confidential 27 chan <- x <- Closed Channel <- What behind Channel panic x, ok := <- chan - [unread data] x is remaining value, ok is true - [has been read] x is falsy value, ok is false - [empty] x is falsy value, ok is false
  • 28. 28 Private & Confidential 28 What behind Channel ” Do not communicate by sharing memory; instead, share memory by communicating. -- Someone else memory goroutine goroutine goroutine memory memory
  • 29. 29 Private & Confidential 29 What behind Channel ” Do not communicate by sharing memory; instead, share memory by communicating. -- Someone else memory goroutine goroutine goroutine memory memory
  • 30. 30 Private & Confidential 30 What behind Channel ” Do not communicate by sharing memory; instead, share memory by communicating. -- Someone else memory goroutine goroutine goroutine memory memory
  • 31. 31 Private & Confidential 31 What behind Channel ” Do not communicate by sharing memory; instead, share memory by communicating. -- Someone else memory goroutine goroutine goroutine memory memory
  • 32. 32 Private & Confidential 32 What crashes Channel QWhat crashes Channel?
  • 33. 33 Private & Confidential 33 What crashes Channel QWhat crashes Channel? What if buf is full?
  • 34. 34 Private & Confidential 34 type hchan struct { // list of send waiters sendq waitq // list of recv waiters recvq waitq buf unsafe.Pointer sendx uint recvx uint lock mutex ... } What crashes Channel https://github.com/golang/go/blob/master/src/runtime/chan.go#L32 buf (doubly circular linked list) lock sendx recvx 11 12 14 13
  • 35. 35 Private & Confidential 35 type hchan struct { // list of send waiters sendq waitq // list of recv waiters recvq waitq buf unsafe.Pointer sendx uint recvx uint lock mutex ... } What crashes Channel https://github.com/golang/go/blob/master/src/runtime/chan.go#L32 buf (doubly circular linked list) lock sendx recvx 11 12 14 13 sendq recvq
  • 36. 36 Private & Confidential 36 type hchan struct { // list of send waiters sendq waitq // list of recv waiters recvq waitq buf unsafe.Pointer sendx uint recvx uint lock mutex ... } What crashes Channel https://github.com/golang/go/blob/master/src/runtime/chan.go#L32 buf (doubly circular linked list) lock scheduling sendx recvx 11 12 14 13 sendq recvq
  • 37. 37 Private & Confidential 37 type hchan struct { // list of send waiters sendq waitq // list of recv waiters recvq waitq buf unsafe.Pointer sendx uint recvx uint lock mutex ... } What crashes Channel https://github.com/golang/go/blob/master/src/runtime/chan.go#L32 buf (doubly circular linked list) lock scheduling sendx recvx 11 12 14 13 sendq recvq c <- 15 goroutine
  • 38. 38 Private & Confidential 38 type hchan struct { // list of send waiters sendq waitq // list of recv waiters recvq waitq buf unsafe.Pointer sendx uint recvx uint lock mutex ... } What crashes Channel https://github.com/golang/go/blob/master/src/runtime/chan.go#L32 buf (doubly circular linked list) lock scheduling sendx recvx 11 12 14 13 sendq recvq c <- 15 goroutine
  • 39. 39 Private & Confidential 39 type hchan struct { // list of send waiters sendq waitq // list of recv waiters recvq waitq buf unsafe.Pointer sendx uint recvx uint lock mutex ... } What crashes Channel https://github.com/golang/go/blob/master/src/runtime/chan.go#L32 buf (doubly circular linked list) lock scheduling sendx recvx 11 12 14 13 sendq recvq c <- 15 goroutine <- c goroutine
  • 40. 40 Private & Confidential 40 type hchan struct { // list of send waiters sendq waitq // list of recv waiters recvq waitq buf unsafe.Pointer sendx uint recvx uint lock mutex ... } What crashes Channel https://github.com/golang/go/blob/master/src/runtime/chan.go#L32 buf (doubly circular linked list) lock scheduling sendx recvx 11 12 14 13 sendq recvq c <- 15 goroutine <- c goroutine
  • 41. 41 Private & Confidential 41 type hchan struct { // list of send waiters sendq waitq // list of recv waiters recvq waitq buf unsafe.Pointer sendx uint recvx uint lock mutex ... } What crashes Channel https://github.com/golang/go/blob/master/src/runtime/chan.go#L32 buf (doubly circular linked list) lock scheduling recvx sendx 12 14 13 sendq recvq c <- 15 goroutine <- c // 11 goroutine
  • 42. 42 Private & Confidential 42 type hchan struct { // list of send waiters sendq waitq // list of recv waiters recvq waitq buf unsafe.Pointer sendx uint recvx uint lock mutex ... } What crashes Channel https://github.com/golang/go/blob/master/src/runtime/chan.go#L32 buf (doubly circular linked list) scheduling 12 14 13 sendq recvq c <- 15 goroutine <- c // 11 goroutine lock recvx sendx
  • 43. 43 Private & Confidential 43 type hchan struct { // list of send waiters sendq waitq // list of recv waiters recvq waitq buf unsafe.Pointer sendx uint recvx uint lock mutex ... } What crashes Channel https://github.com/golang/go/blob/master/src/runtime/chan.go#L32 buf (doubly circular linked list) scheduling 12 14 13 sendq recvq c <- 15 goroutine lock recvx sendx
  • 44. 44 Private & Confidential 44 type hchan struct { // list of send waiters sendq waitq // list of recv waiters recvq waitq buf unsafe.Pointer sendx uint recvx uint lock mutex ... } What crashes Channel https://github.com/golang/go/blob/master/src/runtime/chan.go#L32 buf (doubly circular linked list) scheduling 12 14 13 sendq recvq c <- 15 goroutine lock recvx sendx
  • 45. 45 Private & Confidential 45 type hchan struct { // list of send waiters sendq waitq // list of recv waiters recvq waitq buf unsafe.Pointer sendx uint recvx uint lock mutex ... } What crashes Channel https://github.com/golang/go/blob/master/src/runtime/chan.go#L32 buf (doubly circular linked list) scheduling 12 14 13 sendq recvq c <- 15 goroutine lock recvx sendx
  • 46. 46 Private & Confidential 46 type hchan struct { // list of send waiters sendq waitq // list of recv waiters recvq waitq buf unsafe.Pointer sendx uint recvx uint lock mutex ... } What crashes Channel https://github.com/golang/go/blob/master/src/runtime/chan.go#L32 buf (doubly circular linked list) scheduling sendx recvx 15 12 14 13 sendq recvq goroutine lock
  • 47. 47 Private & Confidential 47 type hchan struct { // list of send waiters sendq waitq // list of recv waiters recvq waitq buf unsafe.Pointer sendx uint recvx uint lock mutex ... } What crashes Channel https://github.com/golang/go/blob/master/src/runtime/chan.go#L32 buf (doubly circular linked list) scheduling 15 12 14 13 sendq recvq goroutine lock sendx recvx
  • 48. 48 Private & Confidential 48 type hchan struct { // list of send waiters sendq waitq // list of recv waiters recvq waitq buf unsafe.Pointer sendx uint recvx uint lock mutex ... } What crashes Channel https://github.com/golang/go/blob/master/src/runtime/chan.go#L32 buf (doubly circular linked list) scheduling 15 12 14 13 sendq recvq lock sendx recvx
  • 49. 49 Private & Confidential 49 1. communication between goroutine 2. multiplexing 3. timeout handling 4. range channel 5. read-only/write-only channel When uses Channel
  • 50. 50 Private & Confidential 50 func main() { var c chan int go func() { c <- 1 }() <-c } 1. communication between goroutine 2. multiplexing 3. timeout handling 4. range channel 5. read-only/write-only channel When uses Channel
  • 51. 51 Private & Confidential 51 for { select { case x, ok := <-c1: ... case x, ok := <-c2: ... default: ... } } 1. communication between goroutine 2. multiplexing 3. timeout handling 4. range channel 5. read-only/write-only channel When uses Channel
  • 52. 52 Private & Confidential 52 select { case <- c: // get from channel c case <- time.After(2 * time.Second) // timeout when getting from channel c } When uses Channel 1. communication between goroutine 2. multiplexing 3. timeout handling 4. range channel 5. read-only/write-only channel
  • 53. 53 Private & Confidential 53 1. communication between goroutine 2. multiplexing 3. timeout handling 4. range channel 5. read-only/write-only channel When uses Channel func consumer(c chan int) { for x := range c { ... } } func producer(c chan int) { for _, x := range values { c <- x } }
  • 54. 54 Private & Confidential 54 // usually used for declaration func foo(ch chan<- int) <-chan int { ... } When uses Channel 1. communication between goroutine 2. multiplexing 3. timeout handling 4. range channel 5. read-only/write-only channel
  • 55. 55 Private & Confidential 55 # [val, pre, nxt] dummy = [] dummy[:] = [None, dummy, dummy] [PLUS] Something interesting
  • 56. 56 Private & Confidential 56 # [val, pre, nxt] dummy = [] dummy[:] = [None, dummy, dummy] node1 = [1, None, None] node1[1] = node1[2] = dummy dummy[1] = dummy[2] = node1 node2 = [2, None, None] node2[1] = node1 node2[2] = node1[2] node1[2][1] = node2 node1[2] = node2 [PLUS] Something interesting
  • 57. 57 Private & Confidential 57 # [val, pre, nxt] dummy = [] dummy[:] = [None, dummy, dummy] node1 = [1, None, None] node1[1] = node1[2] = dummy dummy[1] = dummy[2] = node1 node2 = [2, None, None] node2[1] = node1 node2[2] = node1[2] node1[2][1] = node2 node1[2] = node2 [PLUS] Something interesting doubly circular linked list
  • 58. 58 Private & Confidential 58 # [val, pre, nxt] dummy = [] dummy[:] = [None, dummy, dummy] node1 = [1, None, None] node1[1] = node1[2] = dummy dummy[1] = dummy[2] = node1 node2 = [2, None, None] node2[1] = node1 node2[2] = node1[2] node1[2][1] = node2 node1[2] = node2 [PLUS] Something interesting doubly circular linked list D
  • 59. 59 Private & Confidential 59 # [val, pre, nxt] dummy = [] dummy[:] = [None, dummy, dummy] node1 = [1, None, None] node1[1] = node1[2] = dummy dummy[1] = dummy[2] = node1 node2 = [2, None, None] node2[1] = node1 node2[2] = node1[2] node1[2][1] = node2 node1[2] = node2 [PLUS] Something interesting doubly circular linked list 1 D
  • 60. 60 Private & Confidential 60 # [val, pre, nxt] dummy = [] dummy[:] = [None, dummy, dummy] node1 = [1, None, None] node1[1] = node1[2] = dummy dummy[1] = dummy[2] = node1 node2 = [2, None, None] node2[1] = node1 node2[2] = node1[2] node1[2][1] = node2 node1[2] = node2 [PLUS] Something interesting doubly circular linked list 2 1 D
  • 61. 61 Private & Confidential 61 1. What is Channel 2. What behind Channel 3. What crashes Channel 4. When uses Channel 5. [PLUS] Something interesting
  • 62. 62 Private & Confidential 62 1. What is Channel 2. What behind Channel 3. What crashes Channel 4. When uses Channel 5. [PLUS] Something interesting
  • 63. 63 Private & Confidential 63 1. What is Channel 2. What behind Channel 3. What crashes Channel 4. When uses Channel 5. [PLUS] Something interesting
  • 64. 64 Private & Confidential 64 1. What is Channel 2. What behind Channel 3. What crashes Channel 4. When uses Channel 5. [PLUS] Something interesting
  • 65. 65 Private & Confidential 65 1. What is Channel 2. What behind Channel 3. What crashes Channel 4. When uses Channel 5. [PLUS] Something interesting
  • 66. 66 Private & Confidential 66 Thank you!