From 1bebcd1f5dd2a760ed28d8407deb5d2819fa1db9 Mon Sep 17 00:00:00 2001 From: Jan De Dobbeleer Date: Fri, 8 Nov 2024 21:37:25 +0100 Subject: [PATCH] fix(template): store segment data synchronous --- src/config/segment.go | 3 ++- src/maps/concurrent.go | 50 +++++++++++++++++++++++++++++------------ src/maps/simple.go | 6 ++--- src/runtime/terminal.go | 2 +- 4 files changed, 41 insertions(+), 20 deletions(-) diff --git a/src/config/segment.go b/src/config/segment.go index 64b93d4b9fec..a21b4a38b6e1 100644 --- a/src/config/segment.go +++ b/src/config/segment.go @@ -138,8 +138,9 @@ func (segment *Segment) Render() { return } - segment.writer.SetText(text) + segment.SetText(text) segment.setCache() + segment.env.TemplateCache().AddSegmentData(segment.Name(), segment.writer) } func (segment *Segment) Text() string { diff --git a/src/maps/concurrent.go b/src/maps/concurrent.go index c5bb153799e1..7db17305cd67 100644 --- a/src/maps/concurrent.go +++ b/src/maps/concurrent.go @@ -1,36 +1,58 @@ package maps -import "sync" +import ( + "sync" +) func NewConcurrent() *Concurrent { - var cm Concurrent - return &cm + return &Concurrent{ + data: make(map[string]any), + } } -type Concurrent sync.Map +type Concurrent struct { + data map[string]any + sync.RWMutex +} func (cm *Concurrent) Set(key string, value any) { - (*sync.Map)(cm).Store(key, value) + cm.Lock() + defer cm.Unlock() + + if cm.data == nil { + cm.data = make(map[string]any) + } + + cm.data[key] = value } func (cm *Concurrent) Get(key string) (any, bool) { - return (*sync.Map)(cm).Load(key) + cm.RLock() + defer cm.RUnlock() + + if cm.data == nil { + return nil, false + } + + value, ok := cm.data[key] + return value, ok } func (cm *Concurrent) Delete(key string) { - (*sync.Map)(cm).Delete(key) + cm.Lock() + defer cm.Unlock() + + delete(cm.data, key) } func (cm *Concurrent) Contains(key string) bool { - _, ok := (*sync.Map)(cm).Load(key) + _, ok := cm.Get(key) return ok } func (cm *Concurrent) ToSimple() Simple { - list := make(map[string]any) - (*sync.Map)(cm).Range(func(key, value any) bool { - list[key.(string)] = value - return true - }) - return list + cm.RLock() + defer cm.RUnlock() + + return cm.data } diff --git a/src/maps/simple.go b/src/maps/simple.go index 696a45504ea3..a1f39c1ae60c 100644 --- a/src/maps/simple.go +++ b/src/maps/simple.go @@ -3,9 +3,7 @@ package maps type Simple map[string]any func (m Simple) ToConcurrent() *Concurrent { - var cm Concurrent - for k, v := range m { - cm.Set(k, v) + return &Concurrent{ + data: m, } - return &cm } diff --git a/src/runtime/terminal.go b/src/runtime/terminal.go index 12c8f7dd11c7..63727b9556dc 100644 --- a/src/runtime/terminal.go +++ b/src/runtime/terminal.go @@ -37,10 +37,10 @@ type Terminal struct { deviceCache *cache.File sessionCache *cache.File tmplCache *cache.Template - lsDirMap maps.Concurrent cwd string host string networks []*Connection + lsDirMap maps.Concurrent } func (term *Terminal) Init() {