This document discusses HTTP/2 support in Go 1.6. It notes that the net/http package now supports HTTP/2 out of the box. It provides code samples showing how a server can be configured with and without HTTP/2 support. It also describes how HTTP/2 functionality like HPACK header compression, server push, flow control, and priority are implemented in Go net/http. Finally, it summarizes the current level of HTTP/2 specification compliance and areas still in development.
7. 7
before
import (
fmt
log
net/http
)
func main() {
var s http.Server
s.Addr = ":8080"
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
fmt.Fprintf(w, "Hello World")
})
log.Fatal(s.ListenAndServeTLS(cert, key))
}
8. 8
after
import (
fmt
log
net/http
)
func main() {
var s http.Server
s.Addr = ":8080"
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
fmt.Fprintf(w, "Hello World")
})
log.Fatal(s.ListenAndServeTLS(cert, key))
}
9. 9
you don’t need this after 1.6
$ git clone --depth=1 https://go.googlesource.com/go $HOME/gotip
$ cd $HOME/gotip/src
$ ./make.bash
$ cd $YOUR_PROJ
$ $HOME/gotip/bin/go {install,build,test}.
10. 10
how http2 bundled ?
$ tree $GOROOT/src/net/http
|-- fs.go
|-- fs_test.go
|-- h2_bundle.go
|-- header.go
|-- header_test.go
|-- http_test.go
$ bundle golang.org/x/net/http2 net/http http2
bundle golang.org/x/net/http2
into net/http package
prefixed with `http2`
14. hpack
14
● compress http header
○ with static/dynamic table
○ with huffman encoding
○ with binary frame
● Encode Strategy
○ depends on implement
● Benchmark
○ using hpack-test-case
○ https://gist.github.com/Jxck/a2125cfc162229e4f54f
implementation go-hpack haskell nghttp2 node-http2
ration(small is better) 31% 31% 31% 31%
2016/2/18
benchmark script has a bug, results are updated.
15. Server Push
15
● Push data from server
○ sending PUSH_PROMISE frame
● Low Level API
○ WritePushPromise()
● High Level API
○ not yet
○ wip ? https://goo.gl/IV0fyI
// CAUTION: wip code
if p, ok := w.(http2.Pusher); push && ok {
p.Push("GET", “/path/to/push”, nil)
}
16. Flow Control
16
● 2 Level Flow Control
○ Stream level
○ Connection level
○ with WindowUpdate Frame
● Current Implement
○ supported
○ default sends big window update for connection
○ stream window update each 4M transfer
17. Priority
17
● Priorities Contents
○ with weight of Stream
○ make dependency tree
○ ignorable
○ ex) high for html, low for image..
● Low Level
○ WritePriority()
● High Level
○ no high level api
○ no auto priorities content like html, css, img etc
○ priority respected ?? I can’t find.