Sebastiaan van Stijn
2024-06-04 11:33:43 +02:00
parent dbdd3601eb
commit 9358f84668
146 changed files with 2661 additions and 1102 deletions

View File

@ -0,0 +1,63 @@
package flightcontrol
import (
"context"
"sync"
"github.com/pkg/errors"
)
// Group is a flightcontrol synchronization group that memoizes the results of a function
// and returns the cached result if the function is called with the same key.
// Don't use with long-running groups as the results are cached indefinitely.
type CachedGroup[T any] struct {
// CacheError defines if error results should also be cached.
// It is not safe to change this value after the first call to Do.
// Context cancellation errors are never cached.
CacheError bool
g Group[T]
mu sync.Mutex
cache map[string]result[T]
}
type result[T any] struct {
v T
err error
}
// Do executes a context function syncronized by the key or returns the cached result for the key.
func (g *CachedGroup[T]) Do(ctx context.Context, key string, fn func(ctx context.Context) (T, error)) (T, error) {
return g.g.Do(ctx, key, func(ctx context.Context) (T, error) {
g.mu.Lock()
if v, ok := g.cache[key]; ok {
g.mu.Unlock()
if v.err != nil {
if g.CacheError {
return v.v, v.err
}
} else {
return v.v, nil
}
}
g.mu.Unlock()
v, err := fn(ctx)
if err != nil {
select {
case <-ctx.Done():
if errors.Is(err, context.Cause(ctx)) {
return v, err
}
default:
}
}
if err == nil || g.CacheError {
g.mu.Lock()
if g.cache == nil {
g.cache = make(map[string]result[T])
}
g.cache[key] = result[T]{v: v, err: err}
g.mu.Unlock()
}
return v, err
})
}

View File

@ -1,6 +1,7 @@
package progress
import (
"maps"
"sort"
"sync"
"time"
@ -83,9 +84,7 @@ func (ps *MultiWriter) WriteRawProgress(p *Progress) error {
meta := p.meta
if len(ps.meta) > 0 {
meta = map[string]interface{}{}
for k, v := range p.meta {
meta[k] = v
}
maps.Copy(meta, p.meta)
for k, v := range ps.meta {
if _, ok := meta[k]; !ok {
meta[k] = v

View File

@ -3,6 +3,7 @@ package progress
import (
"context"
"io"
"maps"
"sort"
"sync"
"time"
@ -207,9 +208,7 @@ func pipe() (*progressReader, *progressWriter, func(error)) {
func newWriter(pw *progressWriter) *progressWriter {
meta := make(map[string]interface{})
for k, v := range pw.meta {
meta[k] = v
}
maps.Copy(meta, pw.meta)
pw = &progressWriter{
reader: pw.reader,
meta: meta,
@ -240,9 +239,7 @@ func (pw *progressWriter) WriteRawProgress(p *Progress) error {
meta := p.meta
if len(pw.meta) > 0 {
meta = map[string]interface{}{}
for k, v := range p.meta {
meta[k] = v
}
maps.Copy(meta, p.meta)
for k, v := range pw.meta {
if _, ok := meta[k]; !ok {
meta[k] = v

View File

@ -1,13 +1,13 @@
package progressui
import (
"encoding/csv"
"errors"
"strconv"
"strings"
"github.com/moby/buildkit/util/bklog"
"github.com/morikuni/aec"
"github.com/tonistiigi/go-csvvalue"
)
var termColorMap = map[string]aec.ANSI{
@ -59,9 +59,9 @@ func setUserDefinedTermColors(colorsEnv string) {
}
func readBuildkitColorsEnv(colorsEnv string) []string {
csvReader := csv.NewReader(strings.NewReader(colorsEnv))
csvReader := csvvalue.NewParser()
csvReader.Comma = ':'
fields, err := csvReader.Read()
fields, err := csvReader.Fields(colorsEnv, nil)
if err != nil {
bklog.L.WithError(err).Warnf("Could not parse BUILDKIT_COLORS. Falling back to defaults.")
return nil
@ -70,8 +70,7 @@ func readBuildkitColorsEnv(colorsEnv string) []string {
}
func readRGB(v string) aec.ANSI {
csvReader := csv.NewReader(strings.NewReader(v))
fields, err := csvReader.Read()
fields, err := csvvalue.Fields(v, nil)
if err != nil {
bklog.L.WithError(err).Warnf("Could not parse value %s as valid comma-separated RGB color. Ignoring.", v)
return nil

View File

@ -4,6 +4,7 @@ import (
"bytes"
"context"
"fmt"
"maps"
"math/rand"
"os"
"os/exec"
@ -137,9 +138,7 @@ func WithMirroredImages(m map[string]string) TestOpt {
if tc.mirroredImages == nil {
tc.mirroredImages = map[string]string{}
}
for k, v := range m {
tc.mirroredImages[k] = v
}
maps.Copy(tc.mirroredImages, m)
}
}
@ -418,9 +417,7 @@ func prepareValueMatrix(tc testConf) []matrixValue {
for _, c := range current {
vv := newMatrixValue(featureName, featureValue, v)
vv.fn = append(vv.fn, c.fn...)
for k, v := range c.values {
vv.values[k] = v
}
maps.Copy(vv.values, c.values)
m = append(m, vv)
}
}