bfe_basic

package
v1.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 21, 2021 License: Apache-2.0 Imports: 11 Imported by: 4 modules, 2 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
const (
	HeaderBfeIP         = "X-Bfe-Ip"
	HeaderBfeLogId      = "X-Bfe-Log-Id"
	HeaderForwardedHost = "X-Forwarded-Host"
	HeaderForwardedFor  = "X-Forwarded-For"
	HeaderForwardedPort = "X-Forwarded-Port"
	HeaderRealIP        = "X-Real-Ip"
	HeaderRealPort      = "X-Real-Port"
)
View Source
const (
	SessionNotTrustSource int32 = 0
	SessionTrustSource    int32 = 1
)
View Source
const (
	GlobalProduct = "global"
)

Variables

View Source
var (
	// client error
	ErrClientTlsHandshake   = errors.New("CLIENT_TLS_HANDSHAKE")   // tls handshake error
	ErrClientWrite          = errors.New("CLIENT_WRITE")           // write client error
	ErrClientClose          = errors.New("CLIENT_CLOSE")           // close by peer
	ErrClientLongHeader     = errors.New("CLIENT_LONG_HEADER")     // req too long header
	ErrClientLongUrl        = errors.New("CLIENT_LONG_URL")        // req too long url
	ErrClientTimeout        = errors.New("CLIENT_TIMEOUT")         // timeout
	ErrClientBadRequest     = errors.New("CLIENT_BAD_REQUEST")     // bad request
	ErrClientZeroContentlen = errors.New("CLIENT_ZERO_CONTENTLEN") // zero content length
	ErrClientExpectFail     = errors.New("CLIENT_EXPECT_FAIL")     // expect fail
	ErrClientReset          = errors.New("CLIENT_RESET")           // client reset connection
	ErrClientFrame          = errors.New("CLIENT_LONG_FRAME")      // only used for spdy/http2

	// backend error
	ErrBkFindProduct       = errors.New("BK_FIND_PRODUCT")         // fail to find product
	ErrBkFindLocation      = errors.New("BK_FIND_LOCATION")        // fail to find location
	ErrBkNoCluster         = errors.New("BK_NO_CLUSTER")           // no cluster found
	ErrBkNoSubCluster      = errors.New("BK_NO_SUB_CLUSTER")       // no sub-cluster found
	ErrBkNoBackend         = errors.New("BK_NO_BACKEND")           // no backend found
	ErrBkRequestBackend    = errors.New("BK_REQUEST_BACKEND")      // forward request to backend error
	ErrBkConnectBackend    = errors.New("BK_CONNECT_BACKEND")      // connect backend error
	ErrBkWriteRequest      = errors.New("BK_WRITE_REQUEST")        // write request error (caused by bk or client)
	ErrBkReadRespHeader    = errors.New("BK_READ_RESP_HEADER")     // read response error
	ErrBkRespHeaderTimeout = errors.New("BK_RESP_HEADER_TIMEOUT")  // read response timeout
	ErrBkTransportBroken   = errors.New("BK_TRANSPORT_BROKEN")     // conn broken
	ErrBkRetryTooMany      = errors.New("BK_RETRY_TOOMANY")        // reach retry max
	ErrBkNoSubClusterCross = errors.New("BK_NO_SUB_CLUSTER_CROSS") // no sub-cluster found
	ErrBkCrossRetryBalance = errors.New("BK_CROSS_RETRY_BALANCE")  // cross retry balance failed

	// GSLB error
	ErrGslbBlackhole = errors.New("GSLB_BLACKHOLE") // deny by blackhole
)
View Source
var HopHeaders = []string{
	"Connection",
	"Keep-Alive",
	"Proxy-Authenticate",
	"Proxy-Authorization",
	"Te",
	"Trailers",
	"Transfer-Encoding",
	"Upgrade",
}

Hop-by-hop headers. These are removed when sent to the backend. http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html

Functions

func CreateForbiddenResp

func CreateForbiddenResp(request *Request) *bfe_http.Response

CreateForbiddenResp returns a HTTP 403 response

func CreateInternalResp

func CreateInternalResp(request *Request, code int) *bfe_http.Response

func CreateInternalSrvErrResp

func CreateInternalSrvErrResp(request *Request) *bfe_http.Response

CreateInternalSrvErrResp returns a HTTP 500 response

Types

type BackendInfo

type BackendInfo struct {
	ClusterName    string // name of cluster
	SubclusterName string // name of sub-cluster
	BackendAddr    string // backend ip address
	BackendPort    uint32 // backend's port
	BackendName    string // backend name
}

type OperationStage

type OperationStage int
const (
	StageStartConn OperationStage = iota
	StageReadReqHeader
	StageReadReqBody
	StageConnBackend
	StageWriteBackend
	StageReadResponseHeader
	StageReadResponseBody
	StageWriteClient
	StageEndRequest
)

type RedirectInfo

type RedirectInfo struct {
	Url    string          // URL
	Code   int             // HTTP status code
	Header bfe_http.Header // Extra header
}

type Request

type Request struct {
	Connection net.Conn
	Session    *Session

	RemoteAddr *net.TCPAddr // address of remote peer
	ClientAddr *net.TCPAddr // address of real client. Maybe nil if request is from

	HttpRequest  *bfe_http.Request  // incoming request
	OutRequest   *bfe_http.Request  // forwarded request
	HttpResponse *bfe_http.Response // corresponding response

	CookieMap bfe_http.CookieMap // cookie map
	Query     url.Values         // save url query

	LogId         string // log id for each request
	ReqBody       []byte // req body, size is limited
	ReqBodyPeeked bool   // whether req body has been peeked

	Route RequestRoute // for get backend cluster based on host/path/query/header/...

	Tags RequestTags // request tag info

	Trans RequestTransport // request transport

	BfeStatusCode int // request directly return by bfe

	ErrCode error  // error code for handling request
	ErrMsg  string // additional error msg

	Stat *RequestStat // time, data length, etc.

	RetryTime int         // times of retry
	Backend   BackendInfo // backend info

	Redirect RedirectInfo // redirect info

	SvrDataConf ServerDataConfInterface // interface for ServerDataConf

	// User context associated with this request
	Context map[interface{}]interface{}
}

Request is a wrapper of HTTP request

func NewRequest

func NewRequest(request *bfe_http.Request, conn net.Conn, stat *RequestStat,
	session *Session, svrDataConf ServerDataConfInterface) *Request

NewRequest creates and initializes a new request.

func (*Request) AddTags

func (req *Request) AddTags(name string, ntags []string)

func (*Request) CachedCookie

func (req *Request) CachedCookie() bfe_http.CookieMap

func (*Request) CachedQuery

func (req *Request) CachedQuery() url.Values

func (*Request) Cookie

func (req *Request) Cookie(name string) (*bfe_http.Cookie, bool)

func (*Request) GetContext

func (req *Request) GetContext(key interface{}) interface{}

func (*Request) GetTags

func (req *Request) GetTags(name string) []string

func (*Request) Protocol

func (req *Request) Protocol() string

func (*Request) SetContext

func (req *Request) SetContext(key, val interface{})

func (*Request) SetRequestTransport

func (req *Request) SetRequestTransport(backend *backend.BfeBackend,
	transport bfe_http.RoundTripper)

type RequestRoute

type RequestRoute struct {
	Error       error  // error in request-route
	HostTag     string // tags
	Product     string // name of product
	ClusterName string // clustername req should route to
}

type RequestStat

type RequestStat struct {
	// time stat
	ReadReqStart time.Time // after read first line of http request
	ReadReqEnd   time.Time // after read http request

	FindProStart time.Time // just before find product
	FindProEnd   time.Time // after find product

	LocateStart time.Time // just before find location
	LocateEnd   time.Time // after find location

	ClusterStart time.Time // just before connect backend cluster
	ClusterEnd   time.Time // after get response from backend cluster

	// info for last successful connected backend
	BackendStart time.Time // just before connect backend
	BackendEnd   time.Time // after get response from backend

	ResponseStart time.Time // before write response to client
	ResponseEnd   time.Time // after write response to client

	BackendFirst time.Time // just before connect backend, for first invoke (retry may exist)

	// data length
	HeaderLenIn  int // length of request header
	BodyLenIn    int // length of request body
	HeaderLenOut int // length of response header
	BodyLenOut   int // length of response body

	// some status
	IsCrossCluster bool // with cross-cluster retry?
}

func NewRequestStat

func NewRequestStat(start time.Time) *RequestStat

type RequestTags

type RequestTags struct {
	Error    error               // error in request-tag
	TagTable map[string][]string // type-tags pairs
}

type RequestTransport

type RequestTransport struct {
	Backend   *backend.BfeBackend   // destination backend for request
	Transport bfe_http.RoundTripper // transport to backend
}

type ServerDataConfInterface

type ServerDataConfInterface interface {
	ClusterTableLookup(clusterName string) (*bfe_cluster.BfeCluster, error)
	HostTableLookup(hostname string) (string, error)
}

this interface is used for lookup config for each request

type Session

type Session struct {
	SessionId string    // session id
	StartTime time.Time // time of accept the connection
	EndTime   time.Time // time of close connection
	Overhead  time.Duration

	Connection net.Conn
	RemoteAddr *net.TCPAddr // client address

	Use100Continue bool // "expect 100-continue" is used?

	Proto    string                   // protocol for the connection
	IsSecure bool                     // over tls connection?
	TlsState *bfe_tls.ConnectionState // tls state when using TLS

	Vip     net.IP // the virtual ip visited
	Vport   int    // vip virtual port visited
	Product string // product name of vip
	Rtt     uint32 // smoothed RTT for current connection (us)
	// contains filtered or unexported fields
}

func NewSession

func NewSession(conn net.Conn) *Session

NewSession creates and initializes a new session

func (*Session) ClearContext added in v1.2.0

func (s *Session) ClearContext()

ClearContext clears the old context and makes a new one.

func (*Session) Finish

func (s *Session) Finish()

func (*Session) GetContext

func (s *Session) GetContext(key interface{}) interface{}

func (*Session) GetError

func (s *Session) GetError() (string, error)

func (*Session) GetVip

func (s *Session) GetVip() net.IP

func (*Session) IncReqNum

func (s *Session) IncReqNum(count int) int64

func (*Session) IncReqNumActive

func (s *Session) IncReqNumActive(count int) int64

func (*Session) ReadTotal

func (s *Session) ReadTotal() int

func (*Session) ReqNum

func (s *Session) ReqNum() int64

func (*Session) ReqNumActive

func (s *Session) ReqNumActive() int64

func (*Session) SetContext

func (s *Session) SetContext(key, val interface{})

func (*Session) SetError

func (s *Session) SetError(errCode error, errMsg string)

func (*Session) SetReqNum added in v1.2.0

func (s *Session) SetReqNum(count int)

func (*Session) SetReqNumActive added in v1.2.0

func (s *Session) SetReqNumActive(count int)

func (*Session) SetTrustSource added in v1.2.0

func (s *Session) SetTrustSource(isTrustSource bool)

func (*Session) String

func (s *Session) String() string

func (*Session) TrustSource added in v1.2.0

func (s *Session) TrustSource() bool

func (*Session) UpdateReadTotal

func (s *Session) UpdateReadTotal(total int) int

func (*Session) UpdateWriteTotal

func (s *Session) UpdateWriteTotal(total int) int

func (*Session) WriteTotal

func (s *Session) WriteTotal() int

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL