github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/kbfs/libkbfs/crypto_client_rpc.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 libkbfs
     6  
     7  import (
     8  	"time"
     9  
    10  	"github.com/keybase/client/go/kbfs/kbfscodec"
    11  	"github.com/keybase/client/go/libkb"
    12  	"github.com/keybase/client/go/logger"
    13  	"github.com/keybase/client/go/protocol/keybase1"
    14  	"github.com/keybase/go-framed-msgpack-rpc/rpc"
    15  	"golang.org/x/net/context"
    16  )
    17  
    18  // CryptoClientRPC is an RPC based implementation for Crypto.
    19  type CryptoClientRPC struct {
    20  	CryptoClient
    21  	config Config
    22  }
    23  
    24  var _ rpc.ConnectionHandler = (*CryptoClientRPC)(nil)
    25  
    26  // NewCryptoClientRPC constructs a new RPC based Crypto implementation.
    27  func NewCryptoClientRPC(config Config, kbCtx Context) *CryptoClientRPC {
    28  	log := config.MakeLogger("")
    29  	deferLog := log.CloneWithAddedDepth(1)
    30  	c := &CryptoClientRPC{
    31  		CryptoClient: CryptoClient{
    32  			CryptoCommon: MakeCryptoCommon(config.Codec(), config),
    33  			log:          log,
    34  			deferLog:     deferLog,
    35  		},
    36  		config: config,
    37  	}
    38  	conn := NewSharedKeybaseConnection(kbCtx, config, c)
    39  	c.CryptoClient.client = keybase1.CryptoClient{Cli: conn.GetClient()}
    40  	c.CryptoClient.teamsClient = keybase1.TeamsClient{Cli: conn.GetClient()}
    41  	c.CryptoClient.shutdownFn = conn.Shutdown
    42  	return c
    43  }
    44  
    45  // newCryptoClientWithClient should only be used for testing.
    46  func newCryptoClientWithClient(codec kbfscodec.Codec, log logger.Logger, client rpc.GenericClient) *CryptoClientRPC {
    47  	deferLog := log.CloneWithAddedDepth(1)
    48  	return &CryptoClientRPC{
    49  		CryptoClient: CryptoClient{
    50  			CryptoCommon: MakeCryptoCommon(codec, nil),
    51  			log:          log,
    52  			deferLog:     deferLog,
    53  			client:       keybase1.CryptoClient{Cli: client},
    54  			teamsClient:  keybase1.TeamsClient{Cli: client},
    55  		},
    56  	}
    57  }
    58  
    59  // HandlerName implements the ConnectionHandler interface.
    60  func (CryptoClientRPC) HandlerName() string {
    61  	return "CryptoClient"
    62  }
    63  
    64  // OnConnect implements the ConnectionHandler interface.
    65  func (c *CryptoClientRPC) OnConnect(ctx context.Context, conn *rpc.Connection,
    66  	_ rpc.GenericClient, server *rpc.Server) error {
    67  	c.config.KBFSOps().PushConnectionStatusChange(KeybaseServiceName, nil)
    68  	return nil
    69  }
    70  
    71  // OnConnectError implements the ConnectionHandler interface.
    72  func (c *CryptoClientRPC) OnConnectError(err error, wait time.Duration) {
    73  	c.log.Warning("CryptoClient: connection error: %q; retrying in %s",
    74  		err, wait)
    75  	c.config.KBFSOps().PushConnectionStatusChange(KeybaseServiceName, err)
    76  }
    77  
    78  // OnDoCommandError implements the ConnectionHandler interface.
    79  func (c *CryptoClientRPC) OnDoCommandError(err error, wait time.Duration) {
    80  	c.log.Warning("CryptoClient: docommand error: %q; retrying in %s",
    81  		err, wait)
    82  	c.config.KBFSOps().PushConnectionStatusChange(KeybaseServiceName, err)
    83  }
    84  
    85  // OnDisconnected implements the ConnectionHandler interface.
    86  func (c *CryptoClientRPC) OnDisconnected(_ context.Context,
    87  	status rpc.DisconnectStatus) {
    88  	if status == rpc.StartingNonFirstConnection {
    89  		c.log.Warning("CryptoClient is disconnected")
    90  		c.config.KBFSOps().PushConnectionStatusChange(KeybaseServiceName, errDisconnected{})
    91  	}
    92  }
    93  
    94  // ShouldRetry implements the ConnectionHandler interface.
    95  func (c *CryptoClientRPC) ShouldRetry(rpcName string, err error) bool {
    96  	return false
    97  }
    98  
    99  // ShouldRetryOnConnect implements the ConnectionHandler interface.
   100  func (c *CryptoClientRPC) ShouldRetryOnConnect(err error) bool {
   101  	_, inputCanceled := err.(libkb.InputCanceledError)
   102  	return !inputCanceled
   103  }