pools: Add a pool for buffers

This commit is contained in:
Tom Wiesing 2023-01-24 11:04:34 +01:00
parent a63bb2f669
commit 598de5b289
No known key found for this signature in database
6 changed files with 56 additions and 28 deletions

View file

@ -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
}

View file

@ -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))