Skip to content

Commit

Permalink
client: only execute defaultUserAgent once (#222)
Browse files Browse the repository at this point in the history
Reading the build info quite expensive, and this wraps the call in a
sync.Once so it's only executed the first time, and the result is
memoized since the value never changes.
  • Loading branch information
mattrobenolt authored Oct 15, 2024
1 parent ac11e88 commit 9ff8cd1
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions planetscale/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"net/url"
"runtime/debug"
"strconv"
"sync"

"github.com/hashicorp/go-cleanhttp"
"github.com/pkg/errors"
Expand Down Expand Up @@ -129,7 +130,7 @@ func WithPerPage(perPage int) ListOption {
// ClientOption provides a variadic option for configuring the client
type ClientOption func(c *Client) error

func defaultUserAgent() string {
var defaultUserAgent = sync.OnceValue(func() string {
libraryVersion := "unknown"
buildInfo, ok := debug.ReadBuildInfo()
if ok {
Expand All @@ -142,7 +143,7 @@ func defaultUserAgent() string {
}

return "planetscale-go/" + libraryVersion
}
})

// WithUserAgent overrides the User-Agent header.
func WithUserAgent(userAgent string) ClientOption {
Expand Down

0 comments on commit 9ff8cd1

Please sign in to comment.