http

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 18, 2024 License: Apache-2.0, BSD-3-Clause, MIT Imports: 26 Imported by: 0

README

Fasthttp to net/http Forwarding

This conversion is implemented to proxy WebSocket and HTTP chunked protocols.

Why Implement it Separately

The support for HTTP chunked protocol in the Fasthttp client is currently not complete. Additionally, forwarding WebSocket and HTTP chunked protocols cannot fully utilize the advantages of Fasthttp. Therefore, the net/http package is used as the client to implement the forwarding of WebSocket and HTTP chunked protocols.

Implementation Reference

The implementation is based on the reverseproxy.go file in the net/http/httputil package.

Usage Considerations

  • Set the timeout in trpc_go.yaml server.service[0].timeout to 0 because WebSocket and HTTP chunked are long-lived connections.
  • The timeout in router.yaml client[0].timeout will be forcibly changed to 0.
  • HTTP chunked requests need to include the appropriate headers.
  • For all body reading operations in the gateway, the following check should be performed to exclude stream requests:
if fctx.IsBodyStream() {
    return "stream body"
}

Documentation

Overview

Package http converts fasthttp requests to net/http requests It refers to the logic of net/http/httputil/reverseproxy.go

Index

Constants

This section is empty.

Variables

View Source
var (
	// DefaultProtocolHandler 默认的协议转化处理器
	DefaultProtocolHandler = &ProtocolHandler{
		BufferPool: objectpool.NewBytesPool(defaultBufSize),
	}
)
View Source
var TestConnProxy = (&ProtocolHandler{}).connProxy

TestConnProxy is a variable that holds the connProxy function for testing purposes

Functions

This section is empty.

Types

type BufferPool

type BufferPool interface {
	Get() []byte
	Put([]byte)
}

BufferPool is an interface for getting and returning temporary byte slices for use by io.CopyBuffer.

type ProtocolHandler

type ProtocolHandler struct {
	// Director must be a function which modifies
	// the request into a new request to be sent
	// using Transport. Its response is then copied
	// back to the original client unmodified.
	// Director must not access the provided Request
	// after returning.
	Director func(*stdhttp.Request)

	// The transport used to perform proxy requests.
	// If nil, http.DefaultTransport is used.
	Transport stdhttp.RoundTripper

	// FlushInterval specifies the flush interval
	// to flush to the client while copying the
	// response body.
	// If zero, no periodic flushing is done.
	// A negative value means to flush immediately
	// after each write to the client.
	// The FlushInterval is ignored when ReverseProxy
	// recognizes a response as a streaming response, or
	// if its ContentLength is -1; for such responses, writes
	// are flushed to the client immediately.
	FlushInterval time.Duration

	// BufferPool optionally specifies a buffer pool to
	// get byte slices for use by io.CopyBuffer when
	// copying HTTP response bodies.
	BufferPool

	// ErrorHandler is an optional function that handles errors
	// reaching the backend or errors from ModifyResponse.
	//
	// If nil, the default is to log the provided error and return
	// a 502 Status Bad Gateway response.
	ErrorHandler func(context.Context, error)
}

ProtocolHandler is the default protocol handler

func (*ProtocolHandler) GetCliOptions

func (dph *ProtocolHandler) GetCliOptions(_ context.Context) ([]client.Option, error)

GetCliOptions gets specific client options for the request

func (*ProtocolHandler) HandleErr

func (dph *ProtocolHandler) HandleErr(ctx context.Context, err error) error

HandleErr handles error messages

func (*ProtocolHandler) HandleRspBody

func (dph *ProtocolHandler) HandleRspBody(ctx context.Context, _ interface{}) error

HandleRspBody handles the response

func (*ProtocolHandler) TransReqBody

func (dph *ProtocolHandler) TransReqBody(ctx context.Context) (interface{}, error)

TransReqBody converts the request body

func (*ProtocolHandler) TransRspBody

func (dph *ProtocolHandler) TransRspBody(context.Context) (interface{}, error)

TransRspBody converts the response body

func (*ProtocolHandler) WithCtx

func (dph *ProtocolHandler) WithCtx(ctx context.Context) (context.Context, error)

WithCtx sets the context

Jump to

Keyboard shortcuts

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