golang 1 - critical.today 1.8 - at close quarters.pdf · at golang/net. all the magic is hidden in...

64
Golang 1.8 At Close Quarters

Upload: lamnguyet

Post on 30-Jul-2018

230 views

Category:

Documents


1 download

TRANSCRIPT

Golang 1.8At Close Quarters

A lot of new stuff

The most

important are

(this is subjective list)

Concurrent Map Misuse

Mutex Contention

Profiling

Sorting Slices

HTTP Server

Graceful Shutdown

HTTP/2 Push

Concurrent Map Misuse

It is really

straightforward

What does

mapiternext()

do?

How mapiternext() differs from

mapaccessK()?

mapaccessK() is not called

It will be called only if the key has been deleted,

updated, or deleted and reinserted

This is why it’s always

good to know the

sources

Mutex Contention Profiling

Just run tests with -mutexprofile mutex.out

And parse the

output

Don’t you dare to use

go tool pprof

go get github.com/google/pprof

How it works? It’s

complicated...

Do you know

//go:linkname?

It allows compiler to

rename a

function

Mutex.Unlock()

sync.runtime_Semrelease()

//go:linkname

runtime.sync_runtime_Semrelease()

runtime.semrelease()

runtime.mutexevent()

You will probably

never need to use //go:linkname

Slice Sorting

EASY!

HTTP Server Graceful Shutdown

Close() vs

Shutdown()

Both close listening sockets and

synchronization structures

Close() kills all the

connections

Shutdown() drains connections

with timeout

HTTP/2 Push

You can find it in a

8411 line-long file

That is generated from

another repository

So we should rather look

at golang/net

All the magic is hidden in unexported struct

http2.responseWriter

Before we proceed you

should read RFC

Short summary: you need to send a request before sending a

response

Now we can cast glance at

implementation

First message is passed

to connection handling goroutine

And then request is

added to the queue

This is as just easy as HTTP/2 is

Conclusion?

Do not trust release notes.

Read the code!

Little bit of code

https://github.com/iwankgb/go1.8

Credits

● Gopher looking into abyss, https://www.flickr.com/photos/8510057@N02/4769775073, Moosealope, CC BY● Gopher, https://commons.wikimedia.org/wiki/File:Gophercolor.jpg, Renee French, CC BY-SA● Benny Hill, https://giphy.com/gifs/wtf-reaction-surprised-lT4sgCJwC7B4c, author and license unknown● Thump Up, http://www.publicdomainpictures.net/view-image.php?image=174627, George Hodan, public domain● Rupees, https://pixabay.com/en/indian-currency-money-rupees-523221/, itkannan4u, public domain● Sunset, https://pixabay.com/en/youth-active-jump-happy-sunrise-570881/, jill111, public domain● Pinocchio,

https://commons.wikimedia.org/wiki/File:C._COLLODI,_Le_avventure_di_Pinocchio_(prefazione_di_Attilio_Momigliano,_lineografie_di_B._Albi_Bachini_di_anni_15,_Editrice_La_Tifernate,_Libreria_G._PACI,_Unione_Arti_grafiche,_1944-1948)_edizione_celeste.jpg, public domain