github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/kbfs/libkey/key_server_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/kbfscrypto"
     9  	"github.com/keybase/client/go/kbfs/kbfsmd"
    10  	"github.com/keybase/client/go/protocol/keybase1"
    11  	metrics "github.com/rcrowley/go-metrics"
    12  	"golang.org/x/net/context"
    13  )
    14  
    15  // KeyServerMeasured delegates to another KeyServer instance but
    16  // also keeps track of stats.
    17  type KeyServerMeasured struct {
    18  	delegate    KeyServer
    19  	getTimer    metrics.Timer
    20  	putTimer    metrics.Timer
    21  	deleteTimer metrics.Timer
    22  }
    23  
    24  var _ KeyServer = KeyServerMeasured{}
    25  
    26  // NewKeyServerMeasured creates and returns a new KeyServerMeasured
    27  // instance with the given delegate and registry.
    28  func NewKeyServerMeasured(delegate KeyServer, r metrics.Registry) KeyServerMeasured {
    29  	getTimer := metrics.GetOrRegisterTimer("KeyServer.GetTLFCryptKeyServerHalf", r)
    30  	putTimer := metrics.GetOrRegisterTimer("KeyServer.PutTLFCryptKeyServerHalves", r)
    31  	deleteTimer := metrics.GetOrRegisterTimer("KeyServer.DeleteTLFCryptKeyServerHalf", r)
    32  	return KeyServerMeasured{
    33  		delegate:    delegate,
    34  		getTimer:    getTimer,
    35  		putTimer:    putTimer,
    36  		deleteTimer: deleteTimer,
    37  	}
    38  }
    39  
    40  // GetTLFCryptKeyServerHalf implements the KeyServer interface for
    41  // KeyServerMeasured.
    42  func (b KeyServerMeasured) GetTLFCryptKeyServerHalf(ctx context.Context,
    43  	serverHalfID kbfscrypto.TLFCryptKeyServerHalfID, key kbfscrypto.CryptPublicKey) (
    44  	serverHalf kbfscrypto.TLFCryptKeyServerHalf, err error) {
    45  	b.getTimer.Time(func() {
    46  		serverHalf, err = b.delegate.GetTLFCryptKeyServerHalf(ctx, serverHalfID, key)
    47  	})
    48  	return serverHalf, err
    49  }
    50  
    51  // PutTLFCryptKeyServerHalves implements the KeyServer interface for
    52  // KeyServerMeasured.
    53  func (b KeyServerMeasured) PutTLFCryptKeyServerHalves(ctx context.Context,
    54  	keyServerHalves kbfsmd.UserDeviceKeyServerHalves) (err error) {
    55  	b.putTimer.Time(func() {
    56  		err = b.delegate.PutTLFCryptKeyServerHalves(ctx, keyServerHalves)
    57  	})
    58  	return err
    59  }
    60  
    61  // DeleteTLFCryptKeyServerHalf implements the KeyServer interface for
    62  // KeyServerMeasured.
    63  func (b KeyServerMeasured) DeleteTLFCryptKeyServerHalf(ctx context.Context,
    64  	uid keybase1.UID, key kbfscrypto.CryptPublicKey,
    65  	serverHalfID kbfscrypto.TLFCryptKeyServerHalfID) (err error) {
    66  	b.deleteTimer.Time(func() {
    67  		err = b.delegate.DeleteTLFCryptKeyServerHalf(
    68  			ctx, uid, key, serverHalfID)
    69  	})
    70  	return err
    71  }
    72  
    73  // Shutdown implements the KeyServer interface for KeyServerMeasured.
    74  func (b KeyServerMeasured) Shutdown() {
    75  	b.delegate.Shutdown()
    76  }