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
|
package auth
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"context"
|
"context"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
@ -11,6 +10,7 @@ import (
|
||||||
"github.com/FAU-CDI/wisski-distillery/internal/dis/component"
|
"github.com/FAU-CDI/wisski-distillery/internal/dis/component"
|
||||||
"github.com/FAU-CDI/wisski-distillery/internal/models"
|
"github.com/FAU-CDI/wisski-distillery/internal/models"
|
||||||
"github.com/FAU-CDI/wisski-distillery/pkg/password"
|
"github.com/FAU-CDI/wisski-distillery/pkg/password"
|
||||||
|
"github.com/FAU-CDI/wisski-distillery/pkg/pools"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"github.com/pquerna/otp"
|
"github.com/pquerna/otp"
|
||||||
"github.com/pquerna/otp/totp"
|
"github.com/pquerna/otp/totp"
|
||||||
|
|
@ -186,8 +186,10 @@ func TOTPLink(secret *otp.Key, width, height int) (string, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// encode image as base64
|
// encode image as base64
|
||||||
var buffer bytes.Buffer
|
buffer := pools.GetBuffer()
|
||||||
if err := png.Encode(&buffer, img); err != nil {
|
defer pools.ReleaseBuffer(buffer)
|
||||||
|
|
||||||
|
if err := png.Encode(buffer, img); err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,6 @@ package component
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"bytes"
|
|
||||||
"context"
|
"context"
|
||||||
"io"
|
"io"
|
||||||
"io/fs"
|
"io/fs"
|
||||||
|
|
@ -13,6 +12,7 @@ import (
|
||||||
"github.com/FAU-CDI/wisski-distillery/pkg/environment"
|
"github.com/FAU-CDI/wisski-distillery/pkg/environment"
|
||||||
"github.com/FAU-CDI/wisski-distillery/pkg/fsx"
|
"github.com/FAU-CDI/wisski-distillery/pkg/fsx"
|
||||||
"github.com/FAU-CDI/wisski-distillery/pkg/logging"
|
"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/FAU-CDI/wisski-distillery/pkg/unpack"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"github.com/tkw1536/goprogram/stream"
|
"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
|
// Ps returns the ids of the containers currently running
|
||||||
func (ds Stack) Ps(ctx context.Context, progress io.Writer) ([]string, error) {
|
func (ds Stack) Ps(ctx context.Context, progress io.Writer) ([]string, error) {
|
||||||
// create a buffer
|
// create a buffer
|
||||||
var buffer bytes.Buffer
|
buffer := pools.GetBuffer()
|
||||||
|
defer pools.ReleaseBuffer(buffer)
|
||||||
|
|
||||||
// read the ids from the command!
|
// 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 {
|
if code != 0 {
|
||||||
return nil, errStackPs
|
return nil, errStackPs
|
||||||
}
|
}
|
||||||
|
|
||||||
// scan each of the lines
|
// scan each of the lines
|
||||||
var results []string
|
var results []string
|
||||||
scanner := bufio.NewScanner(&buffer)
|
scanner := bufio.NewScanner(buffer)
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
if text := scanner.Text(); text != "" {
|
if text := scanner.Text(); text != "" {
|
||||||
results = append(results, text)
|
results = append(results, text)
|
||||||
|
|
|
||||||
|
|
@ -9,9 +9,10 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/FAU-CDI/wisski-distillery/pkg/pools"
|
||||||
"github.com/FAU-CDI/wisski-distillery/pkg/timex"
|
"github.com/FAU-CDI/wisski-distillery/pkg/timex"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"github.com/tkw1536/goprogram/stream"
|
"github.com/rs/zerolog"
|
||||||
)
|
)
|
||||||
|
|
||||||
type TriplestoreUserPayload struct {
|
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 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
|
// 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) {
|
func (ts Triplestore) OpenRaw(ctx context.Context, method, url string, body any, bodyName string, accept string) (*http.Response, error) {
|
||||||
var reader io.Reader
|
var reader io.Reader // to read the body from
|
||||||
|
var contentType string // content-type of the request being sent
|
||||||
var contentType string
|
|
||||||
|
|
||||||
// for "PUT" and "POST" we setup a body
|
// for "PUT" and "POST" we setup a body
|
||||||
if method == "PUT" || method == "POST" {
|
if method == http.MethodPut || method == http.MethodPost {
|
||||||
if bodyName != "" {
|
if bodyName != "" {
|
||||||
buffer := &bytes.Buffer{}
|
// create a new buffer for the body
|
||||||
writer := multipart.NewWriter(buffer)
|
buffer := pools.GetBuffer()
|
||||||
contentType = writer.FormDataContentType()
|
defer pools.ReleaseBuffer(buffer)
|
||||||
|
|
||||||
|
// write the file to it
|
||||||
|
writer := multipart.NewWriter(buffer)
|
||||||
|
{
|
||||||
part, err := writer.CreateFormFile(bodyName, "filename.txt")
|
part, err := writer.CreateFormFile(bodyName, "filename.txt")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
io.Copy(part, bytes.NewReader(body.([]byte)))
|
io.Copy(part, bytes.NewReader(body.([]byte)))
|
||||||
|
}
|
||||||
writer.Close()
|
writer.Close()
|
||||||
|
|
||||||
|
// use it for the request
|
||||||
reader = buffer
|
reader = buffer
|
||||||
|
contentType = writer.FormDataContentType()
|
||||||
} else {
|
} else {
|
||||||
contentType = "application/json"
|
|
||||||
mbytes, err := json.Marshal(body)
|
mbytes, err := json.Marshal(body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
reader = bytes.NewReader(mbytes)
|
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.
|
// Wait waits for the connection to the Triplestore to succeed.
|
||||||
// This is achieved using a polling strategy.
|
// This is achieved using a polling strategy.
|
||||||
func (ts Triplestore) Wait(ctx context.Context) error {
|
func (ts Triplestore) Wait(ctx context.Context) error {
|
||||||
n := stream.FromNil()
|
|
||||||
return timex.TickUntilFunc(func(time.Time) bool {
|
return timex.TickUntilFunc(func(time.Time) bool {
|
||||||
res, err := ts.OpenRaw(ctx, "GET", "/rest/repositories", nil, "", "")
|
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 {
|
if err != nil {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,7 @@ import (
|
||||||
|
|
||||||
"github.com/FAU-CDI/wisski-distillery/internal/models"
|
"github.com/FAU-CDI/wisski-distillery/internal/models"
|
||||||
"github.com/FAU-CDI/wisski-distillery/pkg/errorx"
|
"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/FAU-CDI/wisski-distillery/pkg/unpack"
|
||||||
"github.com/tkw1536/goprogram/exit"
|
"github.com/tkw1536/goprogram/exit"
|
||||||
)
|
)
|
||||||
|
|
@ -38,8 +39,9 @@ func (ts *Triplestore) CreateRepository(ctx context.Context, name, domain, user,
|
||||||
}
|
}
|
||||||
|
|
||||||
// prepare the create repo request
|
// prepare the create repo request
|
||||||
var createRepo bytes.Buffer
|
createRepo := pools.GetBuffer()
|
||||||
err := unpack.WriteTemplate(&createRepo, map[string]string{
|
defer pools.ReleaseBuffer(createRepo)
|
||||||
|
err := unpack.WriteTemplate(createRepo, map[string]string{
|
||||||
"GRAPHDB_REPO": name,
|
"GRAPHDB_REPO": name,
|
||||||
"INSTANCE_DOMAIN": domain,
|
"INSTANCE_DOMAIN": domain,
|
||||||
}, bytes.NewReader(createRepoTTL))
|
}, bytes.NewReader(createRepoTTL))
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,12 @@
|
||||||
package environment
|
package environment
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"context"
|
"context"
|
||||||
"io"
|
"io"
|
||||||
"io/fs"
|
"io/fs"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
"github.com/FAU-CDI/wisski-distillery/pkg/pools"
|
||||||
"github.com/tkw1536/goprogram/stream"
|
"github.com/tkw1536/goprogram/stream"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -60,8 +60,10 @@ func ReadFile(env Environment, path string) ([]byte, error) {
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
|
|
||||||
// copy everything into a buffer!
|
// copy everything into a buffer!
|
||||||
var buffer bytes.Buffer
|
buffer := pools.GetBuffer()
|
||||||
if _, err := io.Copy(&buffer, file); err != nil {
|
defer pools.ReleaseBuffer(buffer)
|
||||||
|
|
||||||
|
if _, err := io.Copy(buffer, file); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,8 @@
|
||||||
|
// Package pools holds various pools for reuse
|
||||||
package pools
|
package pools
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
@ -17,3 +19,16 @@ func ReleaseBuilder(builder *strings.Builder) {
|
||||||
builder.Reset()
|
builder.Reset()
|
||||||
builders.Put(builder)
|
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