From d0fb3509ccbedc7ac9ff011aeb07a07fc926fe71 Mon Sep 17 00:00:00 2001 From: James Houlahan Date: Mon, 24 Oct 2022 16:12:37 +0200 Subject: [PATCH] Other: Make user-agent implementation threadsafe --- internal/useragent/useragent.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/internal/useragent/useragent.go b/internal/useragent/useragent.go index 17f0f184..4949e871 100644 --- a/internal/useragent/useragent.go +++ b/internal/useragent/useragent.go @@ -21,10 +21,13 @@ import ( "fmt" "regexp" "runtime" + "sync" ) type UserAgent struct { client, platform string + + lock sync.RWMutex } func New() *UserAgent { @@ -35,18 +38,30 @@ func New() *UserAgent { } func (ua *UserAgent) SetClient(name, version string) { + ua.lock.Lock() + defer ua.lock.Unlock() + ua.client = fmt.Sprintf("%v/%v", name, regexp.MustCompile(`(.*) \((.*)\)`).ReplaceAllString(version, "$1-$2")) } func (ua *UserAgent) HasClient() bool { + ua.lock.RLock() + defer ua.lock.RUnlock() + return ua.client != "" } func (ua *UserAgent) SetPlatform(platform string) { + ua.lock.Lock() + defer ua.lock.Unlock() + ua.platform = platform } func (ua *UserAgent) GetUserAgent() string { + ua.lock.RLock() + defer ua.lock.RUnlock() + var client string if ua.client != "" {