use a sync.Map to avoid concurrency issue accessing log presenters

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
Nicolas De Loof 2021-09-10 12:54:24 +02:00
parent a0bff8be96
commit cc47e5385b
No known key found for this signature in database
GPG Key ID: 9858809D6F8F6E7E
1 changed files with 22 additions and 15 deletions

View File

@ -22,6 +22,7 @@ import (
"io" "io"
"strconv" "strconv"
"strings" "strings"
"sync"
"github.com/docker/compose/v2/pkg/api" "github.com/docker/compose/v2/pkg/api"
) )
@ -30,7 +31,7 @@ import (
func NewLogConsumer(ctx context.Context, w io.Writer, color bool, prefix bool) api.LogConsumer { func NewLogConsumer(ctx context.Context, w io.Writer, color bool, prefix bool) api.LogConsumer {
return &logConsumer{ return &logConsumer{
ctx: ctx, ctx: ctx,
presenters: map[string]*presenter{}, presenters: sync.Map{},
width: 0, width: 0,
writer: w, writer: w,
color: color, color: color,
@ -51,53 +52,59 @@ func (l *logConsumer) register(name string) *presenter {
colors: cf, colors: cf,
name: name, name: name,
} }
l.presenters[name] = p l.presenters.Store(name, p)
if l.prefix { if l.prefix {
l.computeWidth() l.computeWidth()
for _, p := range l.presenters { l.presenters.Range(func(key, value interface{}) bool {
p := value.(*presenter)
p.setPrefix(l.width) p.setPrefix(l.width)
} return true
})
} }
return p return p
} }
func (l *logConsumer) getPresenter(container string) *presenter {
p, ok := l.presenters.Load(container)
if !ok { // should have been registered, but ¯\_(ツ)_/¯
return l.register(container)
}
return p.(*presenter)
}
// Log formats a log message as received from name/container // Log formats a log message as received from name/container
func (l *logConsumer) Log(container, service, message string) { func (l *logConsumer) Log(container, service, message string) {
if l.ctx.Err() != nil { if l.ctx.Err() != nil {
return return
} }
p, ok := l.presenters[container] p := l.getPresenter(container)
if !ok { // should have been registered, but ¯\_(ツ)_/¯
p = l.register(container)
}
for _, line := range strings.Split(message, "\n") { for _, line := range strings.Split(message, "\n") {
fmt.Fprintf(l.writer, "%s %s\n", p.prefix, line) // nolint:errcheck fmt.Fprintf(l.writer, "%s %s\n", p.prefix, line) // nolint:errcheck
} }
} }
func (l *logConsumer) Status(container, msg string) { func (l *logConsumer) Status(container, msg string) {
p, ok := l.presenters[container] p := l.getPresenter(container)
if !ok {
p = l.register(container)
}
s := p.colors(fmt.Sprintf("%s %s\n", container, msg)) s := p.colors(fmt.Sprintf("%s %s\n", container, msg))
l.writer.Write([]byte(s)) // nolint:errcheck l.writer.Write([]byte(s)) // nolint:errcheck
} }
func (l *logConsumer) computeWidth() { func (l *logConsumer) computeWidth() {
width := 0 width := 0
for _, p := range l.presenters { l.presenters.Range(func(key, value interface{}) bool {
p := value.(*presenter)
if len(p.name) > width { if len(p.name) > width {
width = len(p.name) width = len(p.name)
} }
} return true
})
l.width = width + 1 l.width = width + 1
} }
// LogConsumer consume logs from services and format them // LogConsumer consume logs from services and format them
type logConsumer struct { type logConsumer struct {
ctx context.Context ctx context.Context
presenters map[string]*presenter presenters sync.Map // map[string]*presenter
width int width int
writer io.Writer writer io.Writer
color bool color bool