Include a buffer pool to improve memory usage
This commit is contained in:
parent
73fe95722c
commit
97577c07a5
2 changed files with 58 additions and 14 deletions
51
internal/ingress/controller/template/buffer_pool.go
Normal file
51
internal/ingress/controller/template/buffer_pool.go
Normal file
|
@ -0,0 +1,51 @@
|
||||||
|
/*
|
||||||
|
Copyright 2017 The Kubernetes Authors.
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package template
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
|
// BufferPool defines a Pool of Buffers
|
||||||
|
type BufferPool struct {
|
||||||
|
sync.Pool
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewBufferPool creates a new BufferPool with a custom buffer size
|
||||||
|
func NewBufferPool(s int) *BufferPool {
|
||||||
|
return &BufferPool{
|
||||||
|
Pool: sync.Pool{
|
||||||
|
New: func() interface{} {
|
||||||
|
b := bytes.NewBuffer(make([]byte, s))
|
||||||
|
b.Reset()
|
||||||
|
return b
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get returns a Buffer from the pool
|
||||||
|
func (bp *BufferPool) Get() *bytes.Buffer {
|
||||||
|
return bp.Pool.Get().(*bytes.Buffer)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Put resets ans returns a Buffer to the pool
|
||||||
|
func (bp *BufferPool) Put(b *bytes.Buffer) {
|
||||||
|
b.Reset()
|
||||||
|
bp.Pool.Put(b)
|
||||||
|
}
|
|
@ -17,7 +17,6 @@ limitations under the License.
|
||||||
package template
|
package template
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
@ -52,7 +51,7 @@ const (
|
||||||
type Template struct {
|
type Template struct {
|
||||||
tmpl *text_template.Template
|
tmpl *text_template.Template
|
||||||
fw watch.FileWatcher
|
fw watch.FileWatcher
|
||||||
s int
|
bp *BufferPool
|
||||||
}
|
}
|
||||||
|
|
||||||
//NewTemplate returns a new Template instance or an
|
//NewTemplate returns a new Template instance or an
|
||||||
|
@ -70,7 +69,7 @@ func NewTemplate(file string, onChange func()) (*Template, error) {
|
||||||
return &Template{
|
return &Template{
|
||||||
tmpl: tmpl,
|
tmpl: tmpl,
|
||||||
fw: fw,
|
fw: fw,
|
||||||
s: defBufferSize,
|
bp: NewBufferPool(defBufferSize),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -82,15 +81,11 @@ func (t *Template) Close() {
|
||||||
// Write populates a buffer using a template with NGINX configuration
|
// Write populates a buffer using a template with NGINX configuration
|
||||||
// and the servers and upstreams created by Ingress rules
|
// and the servers and upstreams created by Ingress rules
|
||||||
func (t *Template) Write(conf config.TemplateConfig) ([]byte, error) {
|
func (t *Template) Write(conf config.TemplateConfig) ([]byte, error) {
|
||||||
tmplBuf := bytes.NewBuffer(make([]byte, 0, t.s))
|
tmplBuf := t.bp.Get()
|
||||||
outCmdBuf := bytes.NewBuffer(make([]byte, 0, t.s))
|
defer t.bp.Put(tmplBuf)
|
||||||
|
|
||||||
defer func() {
|
outCmdBuf := t.bp.Get()
|
||||||
if t.s < tmplBuf.Cap() {
|
defer t.bp.Put(outCmdBuf)
|
||||||
glog.V(2).Infof("adjusting template buffer size from %v to %v", t.s, tmplBuf.Cap())
|
|
||||||
t.s = tmplBuf.Cap()
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
if glog.V(3) {
|
if glog.V(3) {
|
||||||
b, err := json.Marshal(conf)
|
b, err := json.Marshal(conf)
|
||||||
|
@ -115,9 +110,7 @@ func (t *Template) Write(conf config.TemplateConfig) ([]byte, error) {
|
||||||
return tmplBuf.Bytes(), nil
|
return tmplBuf.Bytes(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
a := make([]byte, outCmdBuf.Len())
|
return outCmdBuf.Bytes(), nil
|
||||||
copy(a, outCmdBuf.Bytes())
|
|
||||||
return a, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
|
Loading…
Reference in a new issue