GopherCon 2018 - Advanced Networking
These are some notes from my experiences at the GopherCon 2018. I don’t expect these will be laid out in any particularly useful way; I am mostly taking them so I can remember some of the bits I found most useful in the future.
The Problem
-
Usually try to use the higher-level abstractions unless you can’t
-
Most of the low-level net package is blocking/sync
-
Event loops
- worker threads pop from event queue until a block would happen
- notify kernel to notify instead of blocking
- then work on other things and come back when the blocking bit was ready
-
Advantages of go
- goroutines
- (see scheduler notes from earlier)
The net Package
-
net.Conninterface handles the accepting connections -
net.Listenerinterface is the server interface- especially
Accept() (Conn, error), which blocks until a connection is ready - so need to
Accept()in a loop
- especially
-
io.Copyis useful for piping data -
Remember to set read timeouts
-
Defer conn.Close in the read loop
-
Make sure to know how a goroutine will end so you don’t leak it
-
io.Copyinterface upgrades!!
Parsing TLS
-
cryptobyte parser
-
MultiReader!!!! (chain readers) -
tls.Connis anet.Connwrapper! -
mkcert!! (tool for local certificates)