Skip to content

Commit

Permalink
Performance Improvments and Spelling Fixes
Browse files Browse the repository at this point in the history
iDigitalFlame committed Sep 14, 2022

Verified

This commit was signed with the committer’s verified signature.
iDigitalFlame iDigitalFlame
1 parent 79b4fb4 commit 860c8a9
Showing 3 changed files with 13 additions and 24 deletions.
22 changes: 7 additions & 15 deletions new.go
Original file line number Diff line number Diff line change
@@ -38,17 +38,15 @@ type Parameter interface {
}

func (k keys) config(p *Proxy) {
p.key = k.Key
p.cert = k.Cert
p.key, p.cert = k.Key, k.Cert
}
func (t Timeout) config(p *Proxy) {
p.server.ReadTimeout = time.Duration(t)
p.server.IdleTimeout = time.Duration(t)
p.server.WriteTimeout = time.Duration(t)
p.server.ReadHeaderTimeout = time.Duration(t)
p.server.IdleTimeout, p.server.WriteTimeout = p.server.ReadTimeout, p.server.ReadTimeout
p.server.ReadHeaderTimeout = p.server.ReadTimeout
}

// TLS creates a config paramater with the specified Key and Value file paths.
// TLS creates a config parameter with the specified Key and Value file paths.
func TLS(cert, key string) Parameter {
return &keys{Cert: cert, Key: key}
}
@@ -62,22 +60,16 @@ func New(listen string, c ...Parameter) *Proxy {
// NewContext creates a new Proxy instance from the specified listen address and
// optional parameters.
//
// This function allows the caller to specify a context to specify when to shutdown
// This function allows the caller to specify a context to specify when to shut down
// the Proxy.
func NewContext(x context.Context, listen string, c ...Parameter) *Proxy {
p := &Proxy{
pool: &sync.Pool{
New: func() interface{} {
return &transfer{
out: new(bytes.Buffer),
read: new(bytes.Buffer),
}
return &transfer{out: new(bytes.Buffer), read: new(bytes.Buffer)}
},
},
server: &http.Server{
Addr: listen,
Handler: &http.ServeMux{},
},
server: &http.Server{Addr: listen, Handler: &http.ServeMux{}},
secondary: make([]*Switch, 0),
}
p.server.BaseContext = p.context
5 changes: 2 additions & 3 deletions proxy.go
Original file line number Diff line number Diff line change
@@ -52,7 +52,7 @@ type transfer struct {
}

// Close attempts to gracefully close and stop the proxy and all remaining
// connextions.
// connections.
func (p *Proxy) Close() error {
p.cancel()
return p.server.Close()
@@ -77,7 +77,6 @@ func (p *Proxy) Start() error {
tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
},
CurvePreferences: []tls.CurveID{tls.CurveP256, tls.X25519},
PreferServerCipherSuites: true,
}
err = p.server.ListenAndServeTLS(p.cert, p.key)
} else {
@@ -98,7 +97,7 @@ func (p *Proxy) clear(t *transfer) {
p.pool.Put(t)
}

// AddSecondary adds an additional one-way Switch context.
// AddSecondary adds a one-way Switch context.
func (p *Proxy) AddSecondary(s ...*Switch) {
p.secondary = append(p.secondary, s...)
}
10 changes: 4 additions & 6 deletions switch.go
Original file line number Diff line number Diff line change
@@ -81,8 +81,8 @@ func (r Result) IsResponse() bool {

// Rewrite adds a URL rewrite from the Switch.
//
// If a URL starts with the 'from' paramater, it will be replaced with the 'to'
// paramater, only if starting with on the URL path.
// If a URL starts with the 'from' parameter, it will be replaced with the 'to'
// parameter, only if starting with on the URL path.
func (s *Switch) Rewrite(from, to string) {
s.rewrite[from] = to
}
@@ -119,7 +119,6 @@ func NewSwitchTimeout(target string, t time.Duration) (*Switch, error) {
DialContext: (&net.Dialer{
Timeout: t,
KeepAlive: t,
DualStack: true,
}).DialContext,
IdleConnTimeout: t,
TLSHandshakeTimeout: t,
@@ -144,7 +143,7 @@ func (s Switch) process(x context.Context, r *http.Request, t *transfer) (int, h
s.Path = path.Join(v, s.Path[len(k):])
}
}
var f func()
f := func() {}
if s.timeout > 0 {
x, f = context.WithTimeout(x, s.timeout)
}
@@ -165,8 +164,7 @@ func (s Switch) process(x context.Context, r *http.Request, t *transfer) (int, h
Headers: r.Header,
})
}
q.Header = r.Header
q.Trailer = r.Trailer
q.Header, q.Trailer = r.Header, r.Trailer
q.TransferEncoding = r.TransferEncoding
o, err := s.client.Do(q)
if err != nil {

0 comments on commit 860c8a9

Please sign in to comment.