Repository: grpc/grpc-go
File: clientconn.go (Go)

Code before:
	// Success; reset backoff.
	ac.mu.Lock()
	connectionAttemptsSucceededMetric.Record(ac.cc.metricsRecorderList, 1, ac.cc.target, ac.backendServiceLabel, ac.localityLabel)
	openConnectionsMetric.Record(ac.cc.metricsRecorderList, 1, ac.cc.target, ac.backendServiceLabel, ac.securityLevel(), ac.localityLabel)
	ac.backoffIdx = 0
	ac.mu.Unlock()
}

// updateTelemetryLabelsLocked calculates and caches the telemetry labels based on the
// current addresses.
func (ac *addrConn) updateTelemetryLabelsLocked() {
	// Reset defaults
	ac.localityLabel = ""
	ac.backendServiceLabel = ""
	labelsFunc, ok := internal.AddressToTelemetryLabels.(func(resolver.Address) map[string]string)
	if !ok || len(ac.addrs) == 0 {
		return
	}
	labels := labelsFunc(ac.addrs[0])
	ac.localityLabel = labels["grpc.lb.locality"]
	ac.backendServiceLabel = labels["grpc.lb.backend_service"]
}

type securityLevelKey struct{}

func (ac *addrConn) securityLevel() string {
	var secLevel string
	// During disconnection, ac.transport is nil. Fall back to the security level
	// stored in the current address during connection.
	if ac.transport == nil {
		secLevel, _ = ac.curAddr.Attributes.Value(securityLevelKey{}).(string)
		return secLevel
	}
	authInfo := ac.transport.AuthInfo()
	if ci, ok := authInfo.(interface {
		GetCommonAuthInfo() credentials.CommonAuthInfo
	}); ok {
		secLevel = ci.GetCommonAuthInfo().SecurityLevel.String()
		// Store the security level in the current address' attributes so
		// that it remains available for disconnection metrics after the
		// transport is closed.
		ac.curAddr.Attributes = ac.curAddr.Attributes.WithValue(securityLevelKey{}, secLevel)
	}
	return secLevel
}

// tryAllAddrs tries to create a connection to the addresses, and stop when at
// the first successful one. It returns an error if no address was successfully
// connected, or updates ac appropriately with the new transport.
func (ac *addrConn) tryAllAddrs(ctx context.Context, addrs []resolver.Address, connectDeadline time.Time) error {
	var firstConnErr error

Review comment (security):
Should this be called `securityLevelLocked()` instead since this assumes that the lock is being held?

Code after:
			return
		}

		ac.mu.Lock()
		if acCtx.Err() == nil {
			ac.updateConnectivityState(connectivity.Idle, err)
		}
		ac.mu.Unlock()
		return
	}
	// Success; reset backoff.
	ac.mu.Lock()
	connectionAttemptsSucceededMetric.Record(ac.cc.metricsRecorderList, 1, ac.cc.target, ac.backendServiceLabel, ac.localityLabel)
	openConnectionsMetric.Record(ac.cc.metricsRecorderList, 1, ac.cc.target, ac.backendServiceLabel, ac.securityLevelLocked(), ac.localityLabel)
	ac.backoffIdx = 0
	ac.mu.Unlock()
}

// updateTelemetryLabelsLocked calculates and caches the telemetry labels based on the
// first address in addrConn.
func (ac *addrConn) updateTelemetryLabelsLocked() {
	labelsFunc, ok := internal.AddressToTelemetryLabels.(func(resolver.Address) map[string]string)
	if !ok || len(ac.addrs) == 0 {
		// Reset defaults
		ac.localityLabel = ""
		ac.backendServiceLabel = ""
		return
	}
	labels := labelsFunc(ac.addrs[0])
	ac.localityLabel = labels["grpc.lb.locality"]
	ac.backendServiceLabel = labels["grpc.lb.backend_service"]
}

type securityLevelKey struct{}

func (ac *addrConn) securityLevelLocked() string {
	var secLevel string
	// During disconnection, ac.transport is nil. Fall back to the security level
	// stored in the current address during connection.
	if ac.transport == nil {
		secLevel, _ = ac.curAddr.Attributes.Value(securityLevelKey{}).(string)
		return secLevel
	}
	authInfo := ac.transport.Peer().AuthInfo
	if ci, ok := authInfo.(interface {
		GetCommonAuthInfo() credentials.CommonAuthInfo
	}); ok {
		secLevel = ci.GetCommonAuthInfo().SecurityLevel.String()
		// Store the security level in the current address' attributes so
		// that it remains available for disconnection metrics after the
		// transport is closed.