Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

metadata: memoize the parsed build versions #22113

Merged
merged 4 commits into from
Feb 3, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 40 additions & 1 deletion agent/metadata/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,51 @@
package metadata

import (
"sync"

"github.com/hashicorp/go-version"
"github.com/hashicorp/serf/serf"
)

type versionTuple struct {
Value *version.Version
Err error
}

var versionCache sync.Map // string->versionTuple

// Build extracts the Consul version info for a member.
func Build(m *serf.Member) (*version.Version, error) {
str := versionFormat.FindString(m.Tags["build"])
build := m.Tags["build"]

ok, v, err := getMemoizedBuildVersion(build)
if ok {
return v, err
}

v, err = parseBuildAsVersion(build)

versionCache.Store(build, versionTuple{Value: v, Err: err})

return v, err
}

func getMemoizedBuildVersion(build string) (bool, *version.Version, error) {
rawTuple, ok := versionCache.Load(build)
if !ok {
return false, nil, nil
}
tuple, ok := rawTuple.(versionTuple)
if !ok {
return false, nil, nil
}
if tuple.Err != nil {
return true, nil, tuple.Err
}
return true, tuple.Value, nil
rboyer marked this conversation as resolved.
Show resolved Hide resolved
}

func parseBuildAsVersion(build string) (*version.Version, error) {
str := versionFormat.FindString(build)
return version.NewVersion(str)
}
Loading