Skip to content

Commit

Permalink
Make it thread safe
Browse files Browse the repository at this point in the history
Signed-off-by: Kimmo Lehto <[email protected]>
  • Loading branch information
kke committed Sep 11, 2023
1 parent c3be6fc commit 84e1f87
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion version.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"sort"
"strconv"
"strings"
"sync"

goversion "github.com/hashicorp/go-version"
)
Expand All @@ -17,6 +18,7 @@ var BaseUrl = "https://github.com/k0sproject/k0s/"
type Version struct {
goversion.Version
buf *bytes.Buffer
mu *sync.Mutex
}

func pair(a, b *Version) Collection {
Expand All @@ -29,6 +31,9 @@ func (v *Version) String() string {
return ""
}

v.mu.Lock()
defer v.mu.Unlock()

if v.buf != nil {
return v.buf.String()
}
Expand Down Expand Up @@ -180,6 +185,7 @@ func (v *Version) unmarshal(f func(interface{}) error) error {
return fmt.Errorf("failed to unmarshal version: %w", err)
}
*v = *newV
v.mu = new(sync.Mutex)
return nil
}

Expand Down Expand Up @@ -218,7 +224,7 @@ func NewVersion(v string) (*Version, error) {
return nil, err
}

return &Version{Version: *n}, nil
return &Version{Version: *n, mu: new(sync.Mutex)}, nil
}

// MustParse is like NewVersion but panics if the version cannot be parsed.
Expand Down

0 comments on commit 84e1f87

Please sign in to comment.