github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/kbfs/libkey/key_bundle_cache_measured.go (about)

     1  // Copyright 2016 Keybase Inc. All rights reserved.
     2  // Use of this source code is governed by a BSD
     3  // license that can be found in the LICENSE file.
     4  
     5  package libkey
     6  
     7  import (
     8  	"github.com/keybase/client/go/kbfs/kbfsmd"
     9  	metrics "github.com/rcrowley/go-metrics"
    10  )
    11  
    12  // KeyBundleCacheMeasured delegates to another KeyBundleCache instance but
    13  // also keeps track of stats.
    14  type KeyBundleCacheMeasured struct {
    15  	delegate                      kbfsmd.KeyBundleCache
    16  	getReaderBundleTimer          metrics.Timer
    17  	getWriterBundleTimer          metrics.Timer
    18  	putReaderBundleTimer          metrics.Timer
    19  	putWriterBundleTimer          metrics.Timer
    20  	hitReaderBundleCountMeter     metrics.Meter
    21  	hitWriterBundleCountMeter     metrics.Meter
    22  	attemptReaderBundleCountMeter metrics.Meter
    23  	attemptWriterBundleCountMeter metrics.Meter
    24  }
    25  
    26  var _ kbfsmd.KeyBundleCache = KeyBundleCacheMeasured{}
    27  
    28  // NewKeyBundleCacheMeasured creates and returns a new KeyBundleCacheMeasured
    29  // instance with the given delegate and registry.
    30  func NewKeyBundleCacheMeasured(delegate kbfsmd.KeyBundleCache, r metrics.Registry) KeyBundleCacheMeasured {
    31  	getReaderBundleTimer := metrics.GetOrRegisterTimer("KeyBundleCache.GetTLFReaderKeyBundle", r)
    32  	putReaderBundleTimer := metrics.GetOrRegisterTimer("KeyBundleCache.PutTLFReaderKeyBundle", r)
    33  	getWriterBundleTimer := metrics.GetOrRegisterTimer("KeyBundleCache.GetTLFWriterKeyBundle", r)
    34  	putWriterBundleTimer := metrics.GetOrRegisterTimer("KeyBundleCache.PutTLFWriterKeyBundle", r)
    35  	hitReaderBundleCountMeter := metrics.GetOrRegisterMeter("KeyBundleCache.TLFReaderKeyBundleHitCount", r)
    36  	hitWriterBundleCountMeter := metrics.GetOrRegisterMeter("KeyBundleCache.TLFWriterKeyBundleHitCount", r)
    37  	attemptReaderBundleCountMeter := metrics.GetOrRegisterMeter("KeyBundleCache.TLFReaderKeyBundleAttemptCount", r)
    38  	attemptWriterBundleCountMeter := metrics.GetOrRegisterMeter("KeyBundleCache.TLFWriterKeyBundleAttemptCount", r)
    39  	return KeyBundleCacheMeasured{
    40  		delegate:                      delegate,
    41  		getReaderBundleTimer:          getReaderBundleTimer,
    42  		getWriterBundleTimer:          getWriterBundleTimer,
    43  		putReaderBundleTimer:          putReaderBundleTimer,
    44  		putWriterBundleTimer:          putWriterBundleTimer,
    45  		hitReaderBundleCountMeter:     hitReaderBundleCountMeter,
    46  		hitWriterBundleCountMeter:     hitWriterBundleCountMeter,
    47  		attemptReaderBundleCountMeter: attemptReaderBundleCountMeter,
    48  		attemptWriterBundleCountMeter: attemptWriterBundleCountMeter,
    49  	}
    50  }
    51  
    52  // GetTLFReaderKeyBundle implements the KeyBundleCache interface for
    53  // KeyBundleCacheMeasured.
    54  func (b KeyBundleCacheMeasured) GetTLFReaderKeyBundle(
    55  	bundleID kbfsmd.TLFReaderKeyBundleID) (rkb *kbfsmd.TLFReaderKeyBundleV3, err error) {
    56  	b.attemptReaderBundleCountMeter.Mark(1)
    57  	b.getReaderBundleTimer.Time(func() {
    58  		rkb, err = b.delegate.GetTLFReaderKeyBundle(bundleID)
    59  	})
    60  	if err == nil && rkb != nil {
    61  		b.hitReaderBundleCountMeter.Mark(1)
    62  	}
    63  	return rkb, err
    64  }
    65  
    66  // GetTLFWriterKeyBundle implements the KeyBundleCache interface for
    67  // KeyBundleCacheMeasured.
    68  func (b KeyBundleCacheMeasured) GetTLFWriterKeyBundle(
    69  	bundleID kbfsmd.TLFWriterKeyBundleID) (wkb *kbfsmd.TLFWriterKeyBundleV3, err error) {
    70  	b.attemptWriterBundleCountMeter.Mark(1)
    71  	b.getWriterBundleTimer.Time(func() {
    72  		wkb, err = b.delegate.GetTLFWriterKeyBundle(bundleID)
    73  	})
    74  	if err == nil && wkb != nil {
    75  		b.hitWriterBundleCountMeter.Mark(1)
    76  	}
    77  	return wkb, err
    78  }
    79  
    80  // PutTLFReaderKeyBundle implements the KeyBundleCache interface for
    81  // KeyBundleCacheMeasured.
    82  func (b KeyBundleCacheMeasured) PutTLFReaderKeyBundle(
    83  	bundleID kbfsmd.TLFReaderKeyBundleID, rkb kbfsmd.TLFReaderKeyBundleV3) {
    84  	b.putReaderBundleTimer.Time(func() {
    85  		b.delegate.PutTLFReaderKeyBundle(bundleID, rkb)
    86  	})
    87  }
    88  
    89  // PutTLFWriterKeyBundle implements the KeyBundleCache interface for
    90  // KeyBundleCacheMeasured.
    91  func (b KeyBundleCacheMeasured) PutTLFWriterKeyBundle(
    92  	bundleID kbfsmd.TLFWriterKeyBundleID, wkb kbfsmd.TLFWriterKeyBundleV3) {
    93  	b.putWriterBundleTimer.Time(func() {
    94  		b.delegate.PutTLFWriterKeyBundle(bundleID, wkb)
    95  	})
    96  }