forked from dgrr/http2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
88 lines (70 loc) · 1.81 KB
/
server.go
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
package http2
import (
"bufio"
"errors"
"net"
"time"
"github.com/valyala/fasthttp"
)
// ServerConfig ...
type ServerConfig struct {
// PingInterval is the interval at which the server will send a
// ping message to a client.
//
// To disable pings set the PingInterval to a negative value.
PingInterval time.Duration
// ...
MaxConcurrentStreams int
// Debug is a flag that will allow the library to print debugging information.
Debug bool
}
func (sc *ServerConfig) defaults() {
if sc.PingInterval == 0 {
sc.PingInterval = time.Second * 10
}
if sc.MaxConcurrentStreams <= 0 {
sc.MaxConcurrentStreams = 1024
}
}
// Server defines an HTTP/2 entity that can handle HTTP/2 connections.
type Server struct {
s *fasthttp.Server
cnf ServerConfig
}
// ServeConn starts serving a net.Conn as HTTP/2.
//
// This function will fail if the connection does not support the HTTP/2 protocol.
func (s *Server) ServeConn(c net.Conn) error {
defer func() { _ = c.Close() }()
if !ReadPreface(c) {
return errors.New("wrong preface")
}
sc := &serverConn{
c: c,
h: s.s.Handler,
br: bufio.NewReader(c),
bw: bufio.NewWriterSize(c, 1<<14*10),
lastID: 0,
writer: make(chan *FrameHeader, 128),
reader: make(chan *FrameHeader, 128),
maxRequestTime: s.s.ReadTimeout,
maxIdleTime: s.s.IdleTimeout,
pingInterval: s.cnf.PingInterval,
logger: s.s.Logger,
debug: s.cnf.Debug,
}
if sc.logger == nil {
sc.logger = logger
}
sc.enc.Reset()
sc.dec.Reset()
sc.maxWindow = 1 << 22
sc.currentWindow = sc.maxWindow
sc.st.Reset()
sc.st.SetMaxWindowSize(uint32(sc.maxWindow))
sc.st.SetMaxConcurrentStreams(uint32(s.cnf.MaxConcurrentStreams))
if err := sc.Handshake(); err != nil {
return err
}
return sc.Serve()
}