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

Code before:

		ac.mu.Lock()
		if acCtx.Err() == nil {
			ac.updateConnectivityState(connectivity.Idle, err)
		}
		ac.mu.Unlock()
		return
	}
	// Success; reset backoff.
	ac.mu.Lock()
	channelz.Infof(logger, ac.channelz, "Emitting success metric for subchannel connection")
	connectionAttemptsSucceededMetric.Record(ac.cc.metricsRecorderList, 1, ac.cc.target, backendService, locality)
	openConnectionsMetric.Record(ac.cc.metricsRecorderList, 1, ac.cc.target, backendService, ac.securityLevel(), locality)
	ac.backoffIdx = 0
	ac.mu.Unlock()
}

func fetchLabels(ac *addrConn) (string, string) {
	var locality, backendService string
	labelsFromAddress, ok := internal.AddressToTelemetryLabels.(func(resolver.Address) map[string]string)
	if len(ac.addrs) > 0 && internal.AddressToTelemetryLabels != nil && ok {
		labels := labelsFromAddress(ac.addrs[0])
		locality = labels["grpc.lb.locality"]
		backendService = labels["grpc.lb.backend_service"]
	}
	return locality, backendService
}

type securityLevelKey struct{}

func (ac *addrConn) securityLevel() string {
	var secLevel string
	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()
		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 (suggestion):
This could also be shorted as follows:

```suggestion
	labelsFunc, ok := internal.AddressToTelemetryLabels.(func(resolver.Address) map[string]string)
    if !ok || len(ac.addrs) == 0 {
        return "", ""
    }
	labels := labelsFunc(ac.addrs[0])
	return labels["grpc.lb.locality"], labels["grpc.lb.backend_service"]
```

Follows from the principle of handling errors before proceeding to the rest of the code: See: go/go-style/decisions#indent-error-flow

Code after:

		// Backoff.
		b := ac.resetBackoff
		ac.mu.Unlock()

		timer := time.NewTimer(backoffFor)
		select {
		case <-timer.C:
			ac.mu.Lock()
			ac.backoffIdx++
			ac.mu.Unlock()
		case <-b:
			timer.Stop()
		case <-acCtx.Done():
			timer.Stop()
			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 {