Skip to content

Commit 27038ee

Browse files
kylecarbsbradfitz
authored andcommitted
hostinfo: cache device model to speed up init
This was causing a relatively consistent ~10ms of delay on Linux. Signed-off-by: Kyle Carberry <kyle@carberry.com>
1 parent ec87e21 commit 27038ee

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

hostinfo/hostinfo.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func New() *tailcfg.Hostinfo {
5252
GoArchVar: lazyGoArchVar.Get(),
5353
GoVersion: runtime.Version(),
5454
Machine: condCall(unameMachine),
55-
DeviceModel: deviceModel(),
55+
DeviceModel: deviceModelCached(),
5656
Cloud: string(cloudenv.Get()),
5757
NoLogsNoSupport: envknob.NoLogsNoSupport(),
5858
AllowsUpdate: envknob.AllowsRemoteUpdate(),
@@ -68,6 +68,7 @@ var (
6868
distroVersion func() string
6969
distroCodeName func() string
7070
unameMachine func() string
71+
deviceModel func() string
7172
)
7273

7374
func condCall[T any](fn func() T) T {
@@ -176,6 +177,20 @@ var (
176177
// SetDeviceModel sets the device model for use in Hostinfo updates.
177178
func SetDeviceModel(model string) { deviceModelAtomic.Store(model) }
178179

180+
func deviceModelCached() string {
181+
if v, _ := deviceModelAtomic.Load().(string); v != "" {
182+
return v
183+
}
184+
if deviceModel == nil {
185+
return ""
186+
}
187+
v := deviceModel()
188+
if v != "" {
189+
deviceModelAtomic.Store(v)
190+
}
191+
return v
192+
}
193+
179194
// SetOSVersion sets the OS version.
180195
func SetOSVersion(v string) { osVersionAtomic.Store(v) }
181196

@@ -193,11 +208,6 @@ func SetPackage(v string) { packagingType.Store(v) }
193208
// and "k8s-operator".
194209
func SetApp(v string) { appType.Store(v) }
195210

196-
func deviceModel() string {
197-
s, _ := deviceModelAtomic.Load().(string)
198-
return s
199-
}
200-
201211
// FirewallMode returns the firewall mode for the app.
202212
// It is empty if unset.
203213
func FirewallMode() string {

hostinfo/hostinfo_linux.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@ func init() {
2222
distroName = distroNameLinux
2323
distroVersion = distroVersionLinux
2424
distroCodeName = distroCodeNameLinux
25-
if v := linuxDeviceModel(); v != "" {
26-
SetDeviceModel(v)
27-
}
25+
deviceModel = deviceModelLinux
2826
}
2927

3028
var (
@@ -50,7 +48,7 @@ func distroCodeNameLinux() string {
5048
return lazyVersionMeta.Get().DistroCodeName
5149
}
5250

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

0 commit comments

Comments
 (0)