Skip to content

Commit

Permalink
vendor ConnState type in fasthttp package
Browse files Browse the repository at this point in the history
  • Loading branch information
gotoxu authored and kirillDanshin committed Aug 30, 2018
1 parent 71b7e99 commit c88d099
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 13 deletions.
65 changes: 58 additions & 7 deletions server.go
Expand Up @@ -9,7 +9,6 @@ import (
"log"
"mime/multipart"
"net"
"net/http"
"os"
"strings"
"sync"
Expand Down Expand Up @@ -280,7 +279,7 @@ type Server struct {
// ConnState specifies an optional callback function that is
// called when a client connection changes state. See the
// ConnState type and associated constants for details.
ConnState func(net.Conn, http.ConnState)
ConnState func(net.Conn, ConnState)

// Logger, which is used by RequestCtx.Logger().
//
Expand Down Expand Up @@ -1390,14 +1389,14 @@ func (s *Server) Serve(ln net.Listener) error {
}
return err
}
s.setState(c, http.StateNew)
s.setState(c, StateNew)
s.wg.Add(1)
if !wp.Serve(c) {
s.wg.Done()
s.writeFastError(c, StatusServiceUnavailable,
"The connection cannot be served because Server.Concurrency limit exceeded")
c.Close()
s.setState(c, http.StateClosed)
s.setState(c, StateClosed)
if time.Since(lastOverflowErrorTime) > time.Minute {
s.logger().Printf("The incoming connection cannot be served, because %d concurrent connections are served. "+
"Try increasing Server.Concurrency", maxWorkersCount)
Expand Down Expand Up @@ -1645,7 +1644,7 @@ func (s *Server) serveConn(c net.Conn) error {
err = ctx.Request.readLimitBody(br, maxRequestBodySize, s.GetOnly)
if br.Buffered() > 0 {
// If we read any bytes off the wire, we're active.
s.setState(c, http.StateActive)
s.setState(c, StateActive)
}
if br.Buffered() == 0 || err != nil {
releaseReader(s, br)
Expand Down Expand Up @@ -1796,7 +1795,7 @@ func (s *Server) serveConn(c net.Conn) error {
}

currentTime = time.Now()
s.setState(c, http.StateIdle)
s.setState(c, StateIdle)
}

if br != nil {
Expand All @@ -1809,7 +1808,7 @@ func (s *Server) serveConn(c net.Conn) error {
return err
}

func (s *Server) setState(nc net.Conn, state http.ConnState) {
func (s *Server) setState(nc net.Conn, state ConnState) {
if hook := s.ConnState; hook != nil {
hook(nc, state)
}
Expand Down Expand Up @@ -2158,3 +2157,55 @@ func writeErrorResponse(bw *bufio.Writer, ctx *RequestCtx, serverName []byte, er
bw.Flush()
return bw
}

// A ConnState represents the state of a client connection to a server.
// It's used by the optional Server.ConnState hook.
type ConnState int

const (
// StateNew represents a new connection that is expected to
// send a request immediately. Connections begin at this
// state and then transition to either StateActive or
// StateClosed.
StateNew ConnState = iota

// StateActive represents a connection that has read 1 or more
// bytes of a request. The Server.ConnState hook for
// StateActive fires before the request has entered a handler
// and doesn't fire again until the request has been
// handled. After the request is handled, the state
// transitions to StateClosed, StateHijacked, or StateIdle.
// For HTTP/2, StateActive fires on the transition from zero
// to one active request, and only transitions away once all
// active requests are complete. That means that ConnState
// cannot be used to do per-request work; ConnState only notes
// the overall state of the connection.
StateActive

// StateIdle represents a connection that has finished
// handling a request and is in the keep-alive state, waiting
// for a new request. Connections transition from StateIdle
// to either StateActive or StateClosed.
StateIdle

// StateHijacked represents a hijacked connection.
// This is a terminal state. It does not transition to StateClosed.
StateHijacked

// StateClosed represents a closed connection.
// This is a terminal state. Hijacked connections do not
// transition to StateClosed.
StateClosed
)

var stateName = map[ConnState]string{
StateNew: "new",
StateActive: "active",
StateIdle: "idle",
StateHijacked: "hijacked",
StateClosed: "closed",
}

func (c ConnState) String() string {
return stateName[c]
}
7 changes: 3 additions & 4 deletions workerpool.go
Expand Up @@ -2,7 +2,6 @@ package fasthttp

import (
"net"
"net/http"
"runtime"
"strings"
"sync"
Expand Down Expand Up @@ -37,7 +36,7 @@ type workerPool struct {

workerChanPool sync.Pool

connState func(net.Conn, http.ConnState)
connState func(net.Conn, ConnState)
}

type workerChan struct {
Expand Down Expand Up @@ -220,10 +219,10 @@ func (wp *workerPool) workerFunc(ch *workerChan) {
}
}
if err == errHijacked {
wp.connState(c, http.StateHijacked)
wp.connState(c, StateHijacked)
} else {
c.Close()
wp.connState(c, http.StateClosed)
wp.connState(c, StateClosed)
}
c = nil

Expand Down
3 changes: 1 addition & 2 deletions workerpool_test.go
Expand Up @@ -3,7 +3,6 @@ package fasthttp
import (
"io/ioutil"
"net"
"net/http"
"testing"
"time"

Expand Down Expand Up @@ -95,7 +94,7 @@ func testWorkerPoolMaxWorkersCount(t *testing.T) {
},
MaxWorkersCount: 10,
Logger: defaultLogger,
connState: func(net.Conn, http.ConnState) {},
connState: func(net.Conn, ConnState) {},
}
wp.Start()

Expand Down

0 comments on commit c88d099

Please sign in to comment.