Skip to content

Commit

Permalink
Reusing of buffers and optimizing memory usage.
Browse files Browse the repository at this point in the history
  • Loading branch information
blaubaer committed Aug 24, 2020
1 parent 3b4bdfb commit d7b412d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 18 deletions.
2 changes: 1 addition & 1 deletion lingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func New(fps support.FileProviders) (*Lingress, error) {
}

result.Http.Handler = result
result.Http.MaxConnections = 512
result.Http.MaxConnections = 256

result.Https.Handler = result
result.Https.Server.Addr = ":8443"
Expand Down
46 changes: 30 additions & 16 deletions proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"net/url"
"runtime/debug"
"strings"
"sync"
"time"
)

Expand All @@ -32,6 +33,8 @@ type Proxy struct {
AccessLogger AccessLogger
Interceptors Interceptors
MetricsCollector lctx.MetricsCollector

bufferPool sync.Pool
}

type AccessLogger func(*lctx.Context)
Expand All @@ -51,12 +54,14 @@ func New(rules rules.Repository) (*Proxy, error) {
RootCAs: support.Pool,
},
}
return &Proxy{
result := &Proxy{
Dialer: dialer,
Transport: transport,
RulesRepository: rules,
Interceptors: DefaultInterceptors.Clone(),
}, nil
}
result.bufferPool.New = result.createBuffer
return result, nil
}

func (instance *Proxy) RegisterFlag(fe support.FlagEnabled, appPrefix string) error {
Expand Down Expand Up @@ -155,6 +160,9 @@ func (instance *Proxy) ServeHTTP(connector server.Connector, resp http.ResponseW
Host: ctx.Client.Host(),
Path: ctx.Client.Request.RequestURI,
}
if u := ctx.Client.Request.URL; u != nil {
query.Path = u.Path
}

ctx.Stage = lctx.StageEvaluateClientRequest
rs, err := instance.RulesRepository.FindBy(query)
Expand Down Expand Up @@ -367,7 +375,7 @@ func (instance *Proxy) execute(ctx *lctx.Context) error {
return nil
}

err = instance.copyResponse(ctx.Client.Response, ctx.Upstream.Response.Body)
_, err = instance.copyBuffered(ctx.Client.Response, ctx.Upstream.Response.Body)
if err != nil {
//noinspection GoUnhandledErrorResult
defer ctx.Upstream.Response.Body.Close()
Expand Down Expand Up @@ -446,27 +454,20 @@ func (instance *Proxy) handleUpgradeResponse(rw http.ResponseWriter, req *http.R
return nil
}

func (instance *Proxy) copyResponse(dst io.Writer, src io.Reader) error {
var buf []byte
_, err := instance.copyBuffer(dst, src, buf)
return err
}

// copyBuffer returns any write errors or non-EOF read errors, and the amount
// copyBuffered returns any write errors or non-EOF read errors, and the amount
// of bytes written.
func (instance *Proxy) copyBuffer(dst io.Writer, src io.Reader, buf []byte) (int64, error) {
if len(buf) == 0 {
buf = make([]byte, 32*1024)
}
func (instance *Proxy) copyBuffered(dst io.Writer, src io.Reader) (int64, error) {
buf := instance.acquireBuffer()
defer instance.releaseBuffer(buf)
var written int64
for {
nr, rErr := src.Read(buf)
nr, rErr := src.Read(*buf)
if rErr != nil && rErr != io.EOF && rErr != context.Canceled {
log.WithError(rErr).
Warn("read error during body copy")
}
if nr > 0 {
nw, wErr := dst.Write(buf[:nr])
nw, wErr := dst.Write((*buf)[:nr])
if nw > 0 {
written += int64(nw)
}
Expand All @@ -485,3 +486,16 @@ func (instance *Proxy) copyBuffer(dst io.Writer, src io.Reader, buf []byte) (int
}
}
}

func (instance *Proxy) createBuffer() interface{} {
result := make([]byte, 32*1024)
return &result
}

func (instance *Proxy) acquireBuffer() *[]byte {
return instance.bufferPool.Get().(*[]byte)
}

func (instance *Proxy) releaseBuffer(buf *[]byte) {
instance.bufferPool.Put(buf)
}
2 changes: 1 addition & 1 deletion server/connector_http.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type HttpConnector struct {
func NewHttpConnector(id ConnectorId) (*HttpConnector, error) {
result := HttpConnector{
Id: id,
MaxConnections: 1024,
MaxConnections: 512,

Server: http.Server{
Addr: ":8080",
Expand Down

0 comments on commit d7b412d

Please sign in to comment.