| 1 |
// build -gcflags=-l=4 |
| 2 |
|
| 3 |
// Copyright 2023 The Go Authors. All rights reserved. |
| 4 |
// Use of this source code is governed by a BSD-style |
| 5 |
// license that can be found in the LICENSE file. |
| 6 |
|
| 7 |
package p |
| 8 |
|
| 9 |
type Interface interface { |
| 10 |
MonitoredResource() (resType string, labels map[string]string) |
| 11 |
Done() |
| 12 |
} |
| 13 |
|
| 14 |
func Autodetect() Interface { |
| 15 |
return func() Interface { |
| 16 |
Do(func() { |
| 17 |
var ad, gd Interface |
| 18 |
|
| 19 |
go func() { |
| 20 |
defer gd.Done() |
| 21 |
ad = aad() |
| 22 |
}() |
| 23 |
go func() { |
| 24 |
defer ad.Done() |
| 25 |
gd = aad() |
| 26 |
defer func() { recover() }() |
| 27 |
}() |
| 28 |
|
| 29 |
autoDetected = ad |
| 30 |
if gd != nil { |
| 31 |
autoDetected = gd |
| 32 |
} |
| 33 |
}) |
| 34 |
return autoDetected |
| 35 |
}() |
| 36 |
} |
| 37 |
|
| 38 |
var autoDetected Interface |
| 39 |
var G int |
| 40 |
|
| 41 |
type If int |
| 42 |
|
| 43 |
func (x If) MonitoredResource() (resType string, labels map[string]string) { |
| 44 |
return "", nil |
| 45 |
} |
| 46 |
|
| 47 |
//go:noinline |
| 48 |
func (x If) Done() { |
| 49 |
G++ |
| 50 |
} |
| 51 |
|
| 52 |
//go:noinline |
| 53 |
func Do(fn func()) { |
| 54 |
fn() |
| 55 |
} |
| 56 |
|
| 57 |
//go:noinline |
| 58 |
func aad() Interface { |
| 59 |
var x If |
| 60 |
return x |
| 61 |
} |