Movatterモバイル変換


[0]ホーム

URL:


expvarx

package
v1.92.3Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 16, 2025 License:BSD-3-ClauseImports:5Imported by:0

Details

Repository

github.com/tailscale/tailscale

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

func NewSafeFunc(fexpvar.Func, limittime.Duration, onSlow func(time.Duration,any)) *SafeFunc

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

func (s *SafeFunc) String()string

String implements stringer in the same pattern asexpvar.Func, callingValue and serializing the result as JSON, ignoring errors.

func (*SafeFunc)Value

func (s *SafeFunc) Value()any

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.

Source Files

View all Source files

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f orF : Jump to
y orY : Canonical URL
go.dev uses cookies from Google to deliver and enhance the quality of its services and to analyze traffic.Learn more.

[8]ページ先頭

©2009-2025 Movatter.jp