github.com/searKing/golang/go@v1.2.117/expvar/leakvar.go (about)

     1  // Copyright 2022 The searKing Author. 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 expvar
     6  
     7  import (
     8  	"expvar"
     9  	"fmt"
    10  )
    11  
    12  var _ expvar.Var = (*Leak)(nil)
    13  
    14  // Leak is a pair of 64-bit integer variables that satisfies the Var interface.
    15  type Leak struct {
    16  	Leak expvar.Int // Leak = New - Delete
    17  	New  expvar.Int // New
    18  }
    19  
    20  // Value returns the Leak counter.
    21  func (v *Leak) Value() int64 {
    22  	return v.Leak.Value()
    23  }
    24  
    25  func (v *Leak) String() string {
    26  	return fmt.Sprint([]string{v.Leak.String(), v.New.String()})
    27  }
    28  
    29  // Add adds delta, which may be negative, to the Leak counter.
    30  func (v *Leak) Add(delta int64) {
    31  	if delta >= 0 {
    32  		v.New.Add(delta)
    33  	}
    34  	v.Leak.Add(delta)
    35  }
    36  
    37  // Done decrements the Leak counter by one.
    38  func (v *Leak) Done() {
    39  	v.Add(-1)
    40  }
    41  
    42  // Convenience functions for creating new exported variables.
    43  
    44  func NewLeak(name string) *Leak {
    45  	v := new(Leak)
    46  	expvar.Publish(name, v)
    47  	return v
    48  }