pools: Add a pool for buffers
This commit is contained in:
parent
a63bb2f669
commit
598de5b289
6 changed files with 56 additions and 28 deletions
|
|
@ -1,7 +1,6 @@
|
|||
package auth
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
|
|
@ -11,6 +10,7 @@ import (
|
|||
"github.com/FAU-CDI/wisski-distillery/internal/dis/component"
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/models"
|
||||
"github.com/FAU-CDI/wisski-distillery/pkg/password"
|
||||
"github.com/FAU-CDI/wisski-distillery/pkg/pools"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/pquerna/otp"
|
||||
"github.com/pquerna/otp/totp"
|
||||
|
|
@ -186,8 +186,10 @@ func TOTPLink(secret *otp.Key, width, height int) (string, error) {
|
|||
}
|
||||
|
||||
// encode image as base64
|
||||
var buffer bytes.Buffer
|
||||
if err := png.Encode(&buffer, img); err != nil {
|
||||
buffer := pools.GetBuffer()
|
||||
defer pools.ReleaseBuffer(buffer)
|
||||
|
||||
if err := png.Encode(buffer, img); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -3,7 +3,6 @@ package component
|
|||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"context"
|
||||
"io"
|
||||
"io/fs"
|
||||
|
|
@ -13,6 +12,7 @@ import (
|
|||
"github.com/FAU-CDI/wisski-distillery/pkg/environment"
|
||||
"github.com/FAU-CDI/wisski-distillery/pkg/fsx"
|
||||
"github.com/FAU-CDI/wisski-distillery/pkg/logging"
|
||||
"github.com/FAU-CDI/wisski-distillery/pkg/pools"
|
||||
"github.com/FAU-CDI/wisski-distillery/pkg/unpack"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/tkw1536/goprogram/stream"
|
||||
|
|
@ -126,17 +126,18 @@ var errStackPs = errors.New("Stack.Ps: Down returned non-zero exit code")
|
|||
// Ps returns the ids of the containers currently running
|
||||
func (ds Stack) Ps(ctx context.Context, progress io.Writer) ([]string, error) {
|
||||
// create a buffer
|
||||
var buffer bytes.Buffer
|
||||
buffer := pools.GetBuffer()
|
||||
defer pools.ReleaseBuffer(buffer)
|
||||
|
||||
// read the ids from the command!
|
||||
code := ds.compose(ctx, stream.NewIOStream(&buffer, progress, nil, 0), "ps", "-q")()
|
||||
code := ds.compose(ctx, stream.NewIOStream(buffer, progress, nil, 0), "ps", "-q")()
|
||||
if code != 0 {
|
||||
return nil, errStackPs
|
||||
}
|
||||
|
||||
// scan each of the lines
|
||||
var results []string
|
||||
scanner := bufio.NewScanner(&buffer)
|
||||
scanner := bufio.NewScanner(buffer)
|
||||
for scanner.Scan() {
|
||||
if text := scanner.Text(); text != "" {
|
||||
results = append(results, text)
|
||||
|
|
|
|||
|
|
@ -9,9 +9,10 @@ import (
|
|||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/FAU-CDI/wisski-distillery/pkg/pools"
|
||||
"github.com/FAU-CDI/wisski-distillery/pkg/timex"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/tkw1536/goprogram/stream"
|
||||
"github.com/rs/zerolog"
|
||||
)
|
||||
|
||||
type TriplestoreUserPayload struct {
|
||||
|
|
@ -31,32 +32,38 @@ type TriplestoreUserAppSettings struct {
|
|||
//
|
||||
// When bodyName is non-empty, expect body to be a byte slice representing a multipart/form-data upload with the given name.
|
||||
// When bodyName is empty, simply marshal body as application/json
|
||||
func (ts Triplestore) OpenRaw(ctx context.Context, method, url string, body interface{}, bodyName string, accept string) (*http.Response, error) {
|
||||
var reader io.Reader
|
||||
|
||||
var contentType string
|
||||
func (ts Triplestore) OpenRaw(ctx context.Context, method, url string, body any, bodyName string, accept string) (*http.Response, error) {
|
||||
var reader io.Reader // to read the body from
|
||||
var contentType string // content-type of the request being sent
|
||||
|
||||
// for "PUT" and "POST" we setup a body
|
||||
if method == "PUT" || method == "POST" {
|
||||
if method == http.MethodPut || method == http.MethodPost {
|
||||
if bodyName != "" {
|
||||
buffer := &bytes.Buffer{}
|
||||
writer := multipart.NewWriter(buffer)
|
||||
contentType = writer.FormDataContentType()
|
||||
// create a new buffer for the body
|
||||
buffer := pools.GetBuffer()
|
||||
defer pools.ReleaseBuffer(buffer)
|
||||
|
||||
part, err := writer.CreateFormFile(bodyName, "filename.txt")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
// write the file to it
|
||||
writer := multipart.NewWriter(buffer)
|
||||
{
|
||||
part, err := writer.CreateFormFile(bodyName, "filename.txt")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
io.Copy(part, bytes.NewReader(body.([]byte)))
|
||||
}
|
||||
io.Copy(part, bytes.NewReader(body.([]byte)))
|
||||
writer.Close()
|
||||
|
||||
// use it for the request
|
||||
reader = buffer
|
||||
contentType = writer.FormDataContentType()
|
||||
} else {
|
||||
contentType = "application/json"
|
||||
mbytes, err := json.Marshal(body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
reader = bytes.NewReader(mbytes)
|
||||
contentType = "application/json"
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -88,10 +95,9 @@ func (ts Triplestore) OpenRaw(ctx context.Context, method, url string, body inte
|
|||
// Wait waits for the connection to the Triplestore to succeed.
|
||||
// This is achieved using a polling strategy.
|
||||
func (ts Triplestore) Wait(ctx context.Context) error {
|
||||
n := stream.FromNil()
|
||||
return timex.TickUntilFunc(func(time.Time) bool {
|
||||
res, err := ts.OpenRaw(ctx, "GET", "/rest/repositories", nil, "", "")
|
||||
n.EPrintf("[Triplestore.Wait]: %s\n", err)
|
||||
zerolog.Ctx(ctx).Trace().Err(err).Msg("Triplestore wait")
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ import (
|
|||
|
||||
"github.com/FAU-CDI/wisski-distillery/internal/models"
|
||||
"github.com/FAU-CDI/wisski-distillery/pkg/errorx"
|
||||
"github.com/FAU-CDI/wisski-distillery/pkg/pools"
|
||||
"github.com/FAU-CDI/wisski-distillery/pkg/unpack"
|
||||
"github.com/tkw1536/goprogram/exit"
|
||||
)
|
||||
|
|
@ -38,8 +39,9 @@ func (ts *Triplestore) CreateRepository(ctx context.Context, name, domain, user,
|
|||
}
|
||||
|
||||
// prepare the create repo request
|
||||
var createRepo bytes.Buffer
|
||||
err := unpack.WriteTemplate(&createRepo, map[string]string{
|
||||
createRepo := pools.GetBuffer()
|
||||
defer pools.ReleaseBuffer(createRepo)
|
||||
err := unpack.WriteTemplate(createRepo, map[string]string{
|
||||
"GRAPHDB_REPO": name,
|
||||
"INSTANCE_DOMAIN": domain,
|
||||
}, bytes.NewReader(createRepoTTL))
|
||||
|
|
|
|||
|
|
@ -1,12 +1,12 @@
|
|||
package environment
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"io"
|
||||
"io/fs"
|
||||
"os"
|
||||
|
||||
"github.com/FAU-CDI/wisski-distillery/pkg/pools"
|
||||
"github.com/tkw1536/goprogram/stream"
|
||||
)
|
||||
|
||||
|
|
@ -60,8 +60,10 @@ func ReadFile(env Environment, path string) ([]byte, error) {
|
|||
defer file.Close()
|
||||
|
||||
// copy everything into a buffer!
|
||||
var buffer bytes.Buffer
|
||||
if _, err := io.Copy(&buffer, file); err != nil {
|
||||
buffer := pools.GetBuffer()
|
||||
defer pools.ReleaseBuffer(buffer)
|
||||
|
||||
if _, err := io.Copy(buffer, file); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,8 @@
|
|||
// Package pools holds various pools for reuse
|
||||
package pools
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
|
|
@ -17,3 +19,16 @@ func ReleaseBuilder(builder *strings.Builder) {
|
|||
builder.Reset()
|
||||
builders.Put(builder)
|
||||
}
|
||||
|
||||
var buffers = sync.Pool{
|
||||
New: func() any { return new(bytes.Buffer) },
|
||||
}
|
||||
|
||||
func GetBuffer() *bytes.Buffer {
|
||||
return buffers.Get().(*bytes.Buffer)
|
||||
}
|
||||
|
||||
func ReleaseBuffer(buffer *bytes.Buffer) {
|
||||
buffer.Reset()
|
||||
buffers.Put(buffer)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue