Skip to content

Commit

Permalink
Refactor server
Browse files Browse the repository at this point in the history
  • Loading branch information
dgrr committed Jul 25, 2021
1 parent 6c50ea7 commit 1c39662
Show file tree
Hide file tree
Showing 7 changed files with 252 additions and 251 deletions.
28 changes: 28 additions & 0 deletions configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,32 @@ func ConfigureClient(c *fasthttp.HostClient, opts ClientOpts) error {
return nil
}

// ConfigureServer configures the fasthttp server to handle
// HTTP/2 connections. The HTTP/2 connection can be only
// established if the fasthttp server is using TLS.
//
// Future implementations may support HTTP/2 through plain TCP.
func ConfigureServer(s *fasthttp.Server) *Server {
s2 := &Server{
s: s,
}

s.NextProto(H2TLSProto, s2.ServeConn)

return s2
}

// ConfigureServerAndConfig configures the fasthttp server to handle HTTP/2 connections
// and your own tlsConfig file. If you are NOT using your own tls config, you may want to use ConfigureServer.
func ConfigureServerAndConfig(s *fasthttp.Server, tlsConfig *tls.Config) *Server {
s2 := &Server{
s: s,
}

s.NextProto(H2TLSProto, s2.ServeConn)
tlsConfig.NextProtos = append(tlsConfig.NextProtos, H2TLSProto)

return s2
}

var ErrNotAvailableStreams = errors.New("ran out of available streams")
5 changes: 3 additions & 2 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,9 @@ func (c *Conn) writeRequest(req *fasthttp.Request) (uint32, error) {

_, err := fr.WriteTo(c.bw)
if err == nil && hasBody {
// release headers bc it's going to get replaced by the data frame
ReleaseFrame(h)

err = writeData(c.bw, fr, req.Body())
}

Expand Down Expand Up @@ -406,8 +409,6 @@ func writeData(bw *bufio.Writer, fh *FrameHeader, body []byte) (err error) {
_, err = fh.WriteTo(bw)
}

ReleaseFrame(data)

return err
}

Expand Down
4 changes: 2 additions & 2 deletions examples/simple/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"log"

"github.com/dgrr/http2/fasthttp2"
"github.com/dgrr/http2"
"github.com/valyala/fasthttp"
)

Expand All @@ -23,7 +23,7 @@ func main() {
log.Fatalln(err)
}

fasthttp2.ConfigureServer(s)
http2.ConfigureServer(s)

err = s.ListenAndServeTLS(":8443", "", "")
if err != nil {
Expand Down
210 changes: 0 additions & 210 deletions fasthttp2/server.go

This file was deleted.

24 changes: 12 additions & 12 deletions frame.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,55 +59,55 @@ type Frame interface {
Deserialize(*FrameHeader) error
}

var framePools = func() [FrameContinuation + 1]sync.Pool {
var pools [FrameContinuation + 1]sync.Pool
var framePools = func() [FrameContinuation + 1]*sync.Pool {
var pools [FrameContinuation + 1]*sync.Pool

pools[FrameData] = sync.Pool{
pools[FrameData] = &sync.Pool{
New: func() interface{} {
return &Data{}
},
}
pools[FrameHeaders] = sync.Pool{
pools[FrameHeaders] = &sync.Pool{
New: func() interface{} {
return &Headers{}
},
}
pools[FramePriority] = sync.Pool{
pools[FramePriority] = &sync.Pool{
New: func() interface{} {
return &Priority{}
},
}
pools[FrameResetStream] = sync.Pool{
pools[FrameResetStream] = &sync.Pool{
New: func() interface{} {
return &RstStream{}
},
}
pools[FrameSettings] = sync.Pool{
pools[FrameSettings] = &sync.Pool{
New: func() interface{} {
return &Settings{}
},
}
pools[FramePushPromise] = sync.Pool{
pools[FramePushPromise] = &sync.Pool{
New: func() interface{} {
return &PushPromise{}
},
}
pools[FramePing] = sync.Pool{
pools[FramePing] = &sync.Pool{
New: func() interface{} {
return &Ping{}
},
}
pools[FrameGoAway] = sync.Pool{
pools[FrameGoAway] = &sync.Pool{
New: func() interface{} {
return &GoAway{}
},
}
pools[FrameWindowUpdate] = sync.Pool{
pools[FrameWindowUpdate] = &sync.Pool{
New: func() interface{} {
return &WindowUpdate{}
},
}
pools[FrameContinuation] = sync.Pool{
pools[FrameContinuation] = &sync.Pool{
New: func() interface{} {
return &Continuation{}
},
Expand Down
Loading

0 comments on commit 1c39662

Please sign in to comment.