You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: replica_rate_limit.go
    path of file: ./repos/cockroach/pkg/kv/kvserver
    
    the code of the file until where you have to start completion: // Copyright 2020 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file 
    
func (r *Replica) maybeRateLimitBatch(ctx context.Context, ba *kvpb.BatchRequest) error {
	if r.tenantLimiter == nil {
		return nil
	}
	tenantID, ok := roachpb.ClientTenantFromContext(ctx)
	if !ok || tenantID == roachpb.SystemTenantID {
		return nil
	}

	// writeMultiplier isn't needed here since it's only used to calculate RUs.
	err := r.tenantLimiter.Wait(ctx, tenantcostmodel.MakeRequestInfo(ba, 1, 1))

	// For performance reasons, we do not hold any Replica's mutexes while waiting
	// on the tenantLimiter, and so we are racing with the Replica lifecycle. The
	// Replica can be destroyed and release the limiter before or during the Wait
	// call. In this case Wait returns an ErrClosed error. Instead of ErrClosed,
	// return the destruction status error which upper layers recognize.
	if err != nil && errors.HasType(err, (*quotapool.ErrClosed)(nil)) {
		if _, err := r.IsDestroyed(); err != nil {
			return err
		}
		return errors.AssertionFailedf("replica not marked as destroyed but limiter is closed: %v", r)
	}

	return err
}