expvarx
packageThis package is not in the latest version of its module.
Details
Validgo.mod file
The Go module system was introduced in Go 1.11 and is the official dependency management solution for Go.
Redistributable license
Redistributable licenses place minimal restrictions on how software can be used, modified, and redistributed.
Tagged version
Modules with tagged versions give importers more predictable builds.
Stable version
When a project reaches major version v1 it is considered stable.
- Learn more about best practices
Repository
Links
Documentation¶
Overview¶
Package expvarx provides some extensions to theexpvar package.
Index¶
Examples¶
Constants¶
This section is empty.
Variables¶
This section is empty.
Functions¶
This section is empty.
Types¶
typeSafeFunc¶
type SafeFunc struct {// contains filtered or unexported fields}SafeFunc is a wrapper aroundexpvar.Func that guards against unbounded calltime and ensures that only a single call is in progress at any given time.
funcNewSafeFunc¶
NewSafeFunc returns a new SafeFunc that wraps f.If f takes longer than limit to execute then Value calls return nil.If onSlow is non-nil, it is called when f takes longer than limit to execute.onSlow is called with the duration of the slow call and the final computedvalue.
Example¶
// An artificial blocker to emulate a slow operation.blocker := make(chan struct{})// limit is the amount of time a call can take before Value returns nil. No// new calls to the unsafe func will be started until the slow call// completes, at which point onSlow will be called.limit := time.Millisecond// onSlow is called with the final call duration and the final value in the// event a slow call.onSlow := func(d time.Duration, v any) {_ = d // d contains the time the call took_ = v // v contains the final value computed by the slow callfmt.Println("slow call!")}// An unsafe expvar.Func that blocks on the blocker channel.unsafeFunc := expvar.Func(func() any {for range blocker {}return "hello world"})// f implements the same interface as expvar.Func, but returns nil values// when the unsafe func is too slow.f := NewSafeFunc(unsafeFunc, limit, onSlow)fmt.Println(f.Value())fmt.Println(f.Value())close(blocker)time.Sleep(time.Millisecond)fmt.Println(f.Value())Output:<nil><nil>slow call!hello world
func (*SafeFunc)String¶
String implements stringer in the same pattern asexpvar.Func, callingValue and serializing the result as JSON, ignoring errors.
func (*SafeFunc)Value¶
Value acts similarly toexpvar.Func.Value, but if the underlying functiontakes longer than the configured limit, all callers will receive nil untilthe underlying operation completes. On completion of the underlyingoperation, the onSlow callback is called if set.