Skip to content

Commit

Permalink
Merge pull request #350 from kramaranya/reuse-buf
Browse files Browse the repository at this point in the history
Reuse buffer with sync.Pool
  • Loading branch information
stlaz authored Jan 22, 2025
2 parents ad71c2c + 6b6bc13 commit 544589f
Showing 1 changed file with 22 additions and 10 deletions.
32 changes: 22 additions & 10 deletions pkg/authorization/rewrite/attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package rewrite
import (
"bytes"
"context"
"sync"
"text/template"

"k8s.io/apiserver/pkg/authorization/authorizer"
Expand Down Expand Up @@ -95,6 +96,8 @@ type TemplatedResourceAttributesGenerator struct {
resource *template.Template
subresource *template.Template
name *template.Template

bufPool *sync.Pool
}

var _ AttributesGenerator = &TemplatedResourceAttributesGenerator{}
Expand All @@ -110,6 +113,12 @@ func NewTemplatedResourceAttributesGenerator(attributes *ResourceAttributes) *Te
resource: template.Must(template.New("resource").Parse(attributes.Resource)),
subresource: template.Must(template.New("subresource").Parse(attributes.Subresource)),
name: template.Must(template.New("name").Parse(attributes.Name)),

bufPool: &sync.Pool{
New: func() interface{} {
return &bytes.Buffer{}
},
},
}
}

Expand All @@ -129,12 +138,12 @@ func (r *TemplatedResourceAttributesGenerator) Generate(ctx context.Context, att
authorizer.AttributesRecord{
User: attr.GetUser(),
Verb: attr.GetVerb(),
Namespace: templateWithValue(r.namespace, param),
APIGroup: templateWithValue(r.apiGroup, param),
APIVersion: templateWithValue(r.apiVersion, param),
Resource: templateWithValue(r.resource, param),
Subresource: templateWithValue(r.subresource, param),
Name: templateWithValue(r.name, param),
Namespace: r.templateWithValue(r.namespace, param),
APIGroup: r.templateWithValue(r.apiGroup, param),
APIVersion: r.templateWithValue(r.apiVersion, param),
Resource: r.templateWithValue(r.resource, param),
Subresource: r.templateWithValue(r.subresource, param),
Name: r.templateWithValue(r.name, param),
ResourceRequest: true,
})
}
Expand All @@ -147,11 +156,14 @@ func (r *TemplatedResourceAttributesGenerator) Generate(ctx context.Context, att
return attrs
}

func templateWithValue(tmpl *template.Template, value string) string {
out := bytes.NewBuffer(nil)
err := tmpl.Execute(out, struct{ Value string }{Value: value})
func (r *TemplatedResourceAttributesGenerator) templateWithValue(tmpl *template.Template, value string) string {
buf := r.bufPool.Get().(*bytes.Buffer)
buf.Reset()
defer r.bufPool.Put(buf)

err := tmpl.Execute(buf, struct{ Value string }{Value: value})
if err != nil {
return ""
}
return out.String()
return buf.String()
}

0 comments on commit 544589f

Please sign in to comment.