Repository: grpc/grpc-go
File: balancer/lazy/lazy.go (Go)

Code before:
// later release.
package lazy

import (
	"fmt"
	"sync"

	"google.golang.org/grpc/balancer"
	"google.golang.org/grpc/connectivity"
	"google.golang.org/grpc/grpclog"
	"google.golang.org/grpc/resolver"

	internalgrpclog "google.golang.org/grpc/internal/grpclog"
)

var (
	logger = grpclog.Component("lazy-lb")
)

const (
	logPrefix = "[lazy-lb %p] "
)

// childBuilderFunc creates a new balancer with the ClientConn. It has the same
// type as the balancer.Builder.Build method.
type childBuilderFunc = func(cc balancer.ClientConn, opts balancer.BuildOptions) balancer.Balancer

// NewBalancer is the constructor for the lazy balancer.
func NewBalancer(cc balancer.ClientConn, bOpts balancer.BuildOptions, childBuilder childBuilderFunc) balancer.Balancer {
	b := &lazyBalancer{
		cc:           cc,
		buildOptions: bOpts,
		childBuilder: childBuilder,
	}
	b.logger = internalgrpclog.NewPrefixLogger(logger, fmt.Sprintf(logPrefix, b))
	cc.UpdateState(balancer.State{
		ConnectivityState: connectivity.Idle,
		Picker: &idlePicker{exitIdle: sync.OnceFunc(func() {
			// Call ExitIdle in a new goroutine to avoid deadlocks while calling
			// back into the channel synchronously.
			go b.ExitIdle()
		})},
	})
	return b
}

type lazyBalancer struct {
	// The following fields are initialized at build time and read-only after
	// that and therefore do not need to be guarded by a mutex.
	cc           balancer.ClientConn
	buildOptions balancer.BuildOptions

Review comment (suggestion):
This really should be exported, otherwise you expose unexported symbols.  The user would have to look at the source code to learn how to use it.

Also I'm completely unclear what the difference is in making this a type alias vs. just a type definition (w/ vs. w/o the `=`).

Code after:
// later release.
package lazy

import (
	"fmt"
	"sync"

	"google.golang.org/grpc/balancer"
	"google.golang.org/grpc/connectivity"
	"google.golang.org/grpc/grpclog"
	"google.golang.org/grpc/resolver"

	internalgrpclog "google.golang.org/grpc/internal/grpclog"
)

var (
	logger = grpclog.Component("lazy-lb")
)

const (
	logPrefix = "[lazy-lb %p] "
)

// ChildBuilderFunc creates a new balancer with the ClientConn. It has the same
// type as the balancer.Builder.Build method.
type ChildBuilderFunc func(cc balancer.ClientConn, opts balancer.BuildOptions) balancer.Balancer

// NewBalancer is the constructor for the lazy balancer.
func NewBalancer(cc balancer.ClientConn, bOpts balancer.BuildOptions, childBuilder ChildBuilderFunc) balancer.Balancer {
	b := &lazyBalancer{
		cc:           cc,
		buildOptions: bOpts,
		childBuilder: childBuilder,
	}
	b.logger = internalgrpclog.NewPrefixLogger(logger, fmt.Sprintf(logPrefix, b))
	cc.UpdateState(balancer.State{
		ConnectivityState: connectivity.Idle,
		Picker: &idlePicker{exitIdle: sync.OnceFunc(func() {
			// Call ExitIdle in a new goroutine to avoid deadlocks while calling
			// back into the channel synchronously.
			go b.ExitIdle()
		})},
	})
	return b
}

type lazyBalancer struct {
	// The following fields are initialized at build time and read-only after
	// that and therefore do not need to be guarded by a mutex.
	cc           balancer.ClientConn
	buildOptions balancer.BuildOptions