github.com/shogo82148/std@v1.22.1-0.20240327122250-4e474527810c/internal/godebug/godebug.go (about)

     1  // Copyright 2021 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Package godebug makes the settings in the $GODEBUG environment variable
     6  // available to other packages. These settings are often used for compatibility
     7  // tweaks, when we need to change a default behavior but want to let users
     8  // opt back in to the original. For example GODEBUG=http2server=0 disables
     9  // HTTP/2 support in the net/http server.
    10  //
    11  // In typical usage, code should declare a Setting as a global
    12  // and then call Value each time the current setting value is needed:
    13  //
    14  //	var http2server = godebug.New("http2server")
    15  //
    16  //	func ServeConn(c net.Conn) {
    17  //		if http2server.Value() == "0" {
    18  //			disallow HTTP/2
    19  //			...
    20  //		}
    21  //		...
    22  //	}
    23  //
    24  // Each time a non-default setting causes a change in program behavior,
    25  // code must call [Setting.IncNonDefault] to increment a counter that can
    26  // be reported by [runtime/metrics.Read]. The call must only happen when
    27  // the program executes a non-default behavior, not just when the setting
    28  // is set to a non-default value. This is occasionally (but very rarely)
    29  // infeasible, in which case the internal/godebugs table entry must set
    30  // Opaque: true, and the documentation in doc/godebug.md should
    31  // mention that metrics are unavailable.
    32  //
    33  // Conventionally, the global variable representing a godebug is named
    34  // for the godebug itself, with no case changes:
    35  //
    36  //	var gotypesalias = godebug.New("gotypesalias") // this
    37  //	var goTypesAlias = godebug.New("gotypesalias") // NOT THIS
    38  //
    39  // The test in internal/godebugs that checks for use of IncNonDefault
    40  // requires the use of this convention.
    41  //
    42  // Note that counters used with IncNonDefault must be added to
    43  // various tables in other packages. See the [Setting.IncNonDefault]
    44  // documentation for details.
    45  package godebug
    46  
    47  import (
    48  	"github.com/shogo82148/std/sync"
    49  )
    50  
    51  // A Setting is a single setting in the $GODEBUG environment variable.
    52  type Setting struct {
    53  	name string
    54  	once sync.Once
    55  	*setting
    56  }
    57  
    58  // New returns a new Setting for the $GODEBUG setting with the given name.
    59  //
    60  // GODEBUGs meant for use by end users must be listed in ../godebugs/table.go,
    61  // which is used for generating and checking various documentation.
    62  // If the name is not listed in that table, New will succeed but calling Value
    63  // on the returned Setting will panic.
    64  // To disable that panic for access to an undocumented setting,
    65  // prefix the name with a #, as in godebug.New("#gofsystrace").
    66  // The # is a signal to New but not part of the key used in $GODEBUG.
    67  //
    68  // Note that almost all settings should arrange to call [IncNonDefault] precisely
    69  // when program behavior is changing from the default due to the setting
    70  // (not just when the setting is different, but when program behavior changes).
    71  // See the [internal/godebug] package comment for more.
    72  func New(name string) *Setting
    73  
    74  // Name returns the name of the setting.
    75  func (s *Setting) Name() string
    76  
    77  // Undocumented reports whether this is an undocumented setting.
    78  func (s *Setting) Undocumented() bool
    79  
    80  // String returns a printable form for the setting: name=value.
    81  func (s *Setting) String() string
    82  
    83  // IncNonDefault increments the non-default behavior counter
    84  // associated with the given setting.
    85  // This counter is exposed in the runtime/metrics value
    86  // /godebug/non-default-behavior/<name>:events.
    87  //
    88  // Note that Value must be called at least once before IncNonDefault.
    89  func (s *Setting) IncNonDefault()
    90  
    91  // Value returns the current value for the GODEBUG setting s.
    92  //
    93  // Value maintains an internal cache that is synchronized
    94  // with changes to the $GODEBUG environment variable,
    95  // making Value efficient to call as frequently as needed.
    96  // Clients should therefore typically not attempt their own
    97  // caching of Value's result.
    98  func (s *Setting) Value() string