Skip to content

Commit

Permalink
chore: lazily load linux device model to speed up init() (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
kylecarbs authored Apr 1, 2024
1 parent 86ba201 commit d329bbd
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
25 changes: 16 additions & 9 deletions hostinfo/hostinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func New() *tailcfg.Hostinfo {
GoArchVar: lazyGoArchVar.Get(),
GoVersion: runtime.Version(),
Machine: condCall(unameMachine),
DeviceModel: deviceModel(),
DeviceModel: deviceModelCached(),
PushDeviceToken: pushDeviceToken(),
Cloud: string(cloudenv.Get()),
NoLogsNoSupport: envknob.NoLogsNoSupport(),
Expand All @@ -68,6 +68,7 @@ var (
distroVersion func() string
distroCodeName func() string
unameMachine func() string
deviceModel func() string
)

func condCall[T any](fn func() T) T {
Expand Down Expand Up @@ -115,6 +116,20 @@ func GetOSVersion() string {
return ""
}

func deviceModelCached() string {
if v, _ := deviceModelAtomic.Load().(string); v != "" {
return v
}
if deviceModel == nil {
return ""
}
v := deviceModel()
if v != "" {
deviceModelAtomic.Store(v)
}
return v
}

func appTypeCached() string {
if v, ok := appType.Load().(string); ok {
return v
Expand Down Expand Up @@ -175,9 +190,6 @@ var (
// SetPushDeviceToken sets the device token for use in Hostinfo updates.
func SetPushDeviceToken(token string) { pushDeviceTokenAtomic.Store(token) }

// SetDeviceModel sets the device model for use in Hostinfo updates.
func SetDeviceModel(model string) { deviceModelAtomic.Store(model) }

// SetOSVersion sets the OS version.
func SetOSVersion(v string) { osVersionAtomic.Store(v) }

Expand All @@ -192,11 +204,6 @@ func SetPackage(v string) { packagingType.Store(v) }
// and "k8s-operator".
func SetApp(v string) { appType.Store(v) }

func deviceModel() string {
s, _ := deviceModelAtomic.Load().(string)
return s
}

func pushDeviceToken() string {
s, _ := pushDeviceTokenAtomic.Load().(string)
return s
Expand Down
6 changes: 2 additions & 4 deletions hostinfo/hostinfo_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ func init() {
distroName = distroNameLinux
distroVersion = distroVersionLinux
distroCodeName = distroCodeNameLinux
if v := linuxDeviceModel(); v != "" {
SetDeviceModel(v)
}
deviceModel = deviceModelLinux
}

var (
Expand All @@ -50,7 +48,7 @@ func distroCodeNameLinux() string {
return lazyVersionMeta.Get().DistroCodeName
}

func linuxDeviceModel() string {
func deviceModelLinux() string {
for _, path := range []string{
// First try the Synology-specific location.
// Example: "DS916+-j"
Expand Down

0 comments on commit d329bbd

Please sign in to comment.