go.temporal.io/server/client/clientfactory.go
245 LOC · 88 covered · 157 uncovered · 17 ranges · 64 concepts · 3 introducers · 12 tests
File neighbourhood
The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.
Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file
In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.
Graph controls are ready.
Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.
//go:generate mockgen -package $GOPACKAGE -source $GOFILE -destination client_factory_mock.go
package client
import (
"time"
"go.temporal.io/api/workflowservice/v1"
"go.temporal.io/server/api/adminservice/v1"
"go.temporal.io/server/api/historyservice/v1"
"go.temporal.io/server/api/matchingservice/v1"
"go.temporal.io/server/client/admin"
"go.temporal.io/server/client/frontend"
"go.temporal.io/server/client/history"
"go.temporal.io/server/client/matching"
"go.temporal.io/server/common"
"go.temporal.io/server/common/dynamicconfig"
"go.temporal.io/server/common/log"
"go.temporal.io/server/common/membership"
"go.temporal.io/server/common/metrics"
"go.temporal.io/server/common/namespace"
"go.temporal.io/server/common/primitives"
"go.temporal.io/server/common/testing/testhooks"
"google.golang.org/grpc"
)
type (
// Factory can be used to create RPC clients for temporal services
Factory interface {
NewHistoryClientWithTimeout(timeout time.Duration) (historyservice.HistoryServiceClient, error)
NewMatchingClientWithTimeout(namespaceIDToName NamespaceIDToNameFunc, timeout time.Duration, longPollTimeout time.Duration) (matchingservice.MatchingServiceClient, error)
NewRemoteFrontendClientWithTimeout(rpcAddress string, timeout time.Duration, longPollTimeout time.Duration) (grpc.ClientConnInterface, workflowservice.WorkflowServiceClient)
NewLocalFrontendClientWithTimeout(timeout time.Duration, longPollTimeout time.Duration) (grpc.ClientConnInterface, workflowservice.WorkflowServiceClient, error)
NewRemoteAdminClientWithTimeout(rpcAddress string, timeout time.Duration, largeTimeout time.Duration) adminservice.AdminServiceClient
NewLocalAdminClientWithTimeout(timeout time.Duration, largeTimeout time.Duration) (adminservice.AdminServiceClient, error)
}
// FactoryProvider can be used to provide a customized client Factory implementation.
FactoryProvider interface {
NewFactory(
rpcFactory common.RPCFactory,
monitor membership.Monitor,
metricsHandler metrics.Handler,
dc *dynamicconfig.Collection,
testHooks testhooks.TestHooks,
numberOfHistoryShards int32,
logger log.Logger,
throttledLogger log.Logger,
) Factory
}
// NamespaceIDToNameFunc maps a namespaceID to namespace name. Returns error when mapping is not possible.
NamespaceIDToNameFunc func(id namespace.ID) (namespace.Name, error)
rpcClientFactory struct {
rpcFactory common.RPCFactory
monitor membership.Monitor
metricsHandler metrics.Handler
dynConfig *dynamicconfig.Collection
testHooks testhooks.TestHooks
numberOfHistoryShards int32
logger log.Logger
throttledLogger log.Logger
}
factoryProviderImpl struct {
}
serviceKeyResolverImpl struct {
resolver membership.ServiceResolver
}
)
// NewFactoryProvider creates a default implementation of FactoryProvider.
return &factoryProviderImpl{}
}
// NewFactory creates an instance of client factory that knows how to dispatch RPC calls.
func (p *factoryProviderImpl) NewFactory(
rpcFactory common.RPCFactory,
monitor membership.Monitor,
metricsHandler metrics.Handler,
dc *dynamicconfig.Collection,
testHooks testhooks.TestHooks,
numberOfHistoryShards int32,
logger log.Logger,
throttledLogger log.Logger,
return &rpcClientFactory{
rpcFactory: rpcFactory,
monitor: monitor,
metricsHandler: metricsHandler,
dynConfig: dc,
testHooks: testHooks,
numberOfHistoryShards: numberOfHistoryShards,
logger: logger,
throttledLogger: throttledLogger,
}
}
func (cf *rpcClientFactory) NewHistoryClientWithTimeout(timeout time.Duration) (historyservice.HistoryServiceClient, error) {
fx.go ×44
resolver, err := cf.monitor.GetResolver(primitives.HistoryService)
if err != nil {
return nil, err
}
cf.dynConfig,
resolver,
cf.logger,
cf.numberOfHistoryShards,
cf.rpcFactory,
timeout,
)
if cf.metricsHandler != nil {
client = history.NewMetricClient(client, cf.metricsHandler, cf.logger, cf.throttledLogger)
}
return client, nil
}
func (cf *rpcClientFactory) NewMatchingClientWithTimeout(
namespaceIDToName NamespaceIDToNameFunc,
timeout time.Duration,
longPollTimeout time.Duration,
resolver, err := cf.monitor.GetResolver(primitives.MatchingService)
if err != nil {
return nil, err
}
clientProvider := func(clientKey string) (any, func() error, error) {
return matchingservice.NewMatchingServiceClient(connection), connection.Close, nil
}
timeout,
longPollTimeout,
common.NewClientCache(keyResolver, clientProvider, cf.logger),
cf.metricsHandler,
cf.logger,
matching.NewLoadBalancer(namespaceIDToName, cf.dynConfig, cf.testHooks),
dynamicconfig.MatchingSpreadRoutingBatchSize.Get(cf.dynConfig),
resolver,
dynamicconfig.MatchingConnectionCloseDelay.Get(cf.dynConfig),
)
if cf.metricsHandler != nil {
client = matching.NewMetricClient(client, cf.metricsHandler, cf.logger, cf.throttledLogger)
}
return client, nil
}
func (cf *rpcClientFactory) NewRemoteFrontendClientWithTimeout(
rpcAddress string,
timeout time.Duration,
longPollTimeout time.Duration,
) (grpc.ClientConnInterface, workflowservice.WorkflowServiceClient) {
connection := cf.rpcFactory.CreateRemoteFrontendGRPCConnection(rpcAddress)
client := workflowservice.NewWorkflowServiceClient(connection)
return connection, cf.newFrontendClient(client, timeout, longPollTimeout)
}
func (cf *rpcClientFactory) NewLocalFrontendClientWithTimeout(
timeout time.Duration,
longPollTimeout time.Duration,
connection := cf.rpcFactory.CreateLocalFrontendGRPCConnection()
client := workflowservice.NewWorkflowServiceClient(connection)
return connection, cf.newFrontendClient(client, timeout, longPollTimeout), nil
}
func (cf *rpcClientFactory) NewRemoteAdminClientWithTimeout(
rpcAddress string,
timeout time.Duration,
largeTimeout time.Duration,
) adminservice.AdminServiceClient {
connection := cf.rpcFactory.CreateRemoteFrontendGRPCConnection(rpcAddress)
client := adminservice.NewAdminServiceClient(connection)
return cf.newAdminClient(client, timeout, largeTimeout)
}
func (cf *rpcClientFactory) NewLocalAdminClientWithTimeout(
timeout time.Duration,
longPollTimeout time.Duration,
connection := cf.rpcFactory.CreateLocalFrontendGRPCConnection()
client := adminservice.NewAdminServiceClient(connection)
return cf.newAdminClient(client, timeout, longPollTimeout), nil
}
func (cf *rpcClientFactory) newAdminClient(
client adminservice.AdminServiceClient,
timeout time.Duration,
longPollTimeout time.Duration,
client = admin.NewClient(timeout, longPollTimeout, client)
if cf.metricsHandler != nil {
client = admin.NewMetricClient(client, cf.metricsHandler, cf.throttledLogger)
}
return client
}
func (cf *rpcClientFactory) newFrontendClient(
client workflowservice.WorkflowServiceClient,
timeout time.Duration,
longPollTimeout time.Duration,
client = frontend.NewClient(timeout, longPollTimeout, client)
if cf.metricsHandler != nil {
client = frontend.NewMetricClient(client, cf.metricsHandler, cf.throttledLogger)
}
return client
}
func newServiceKeyResolver(resolver membership.ServiceResolver) *serviceKeyResolverImpl {
fx.go ×44
return &serviceKeyResolverImpl{
resolver: resolver,
}
}
// Lookup returns the address for a node within a batch. key contains the key (including batch
// number), and index is the index within the batch. If not using batches, index should be 0.
// Note that Lookup(key) and LookupN(key, n)[0] are equal.
hosts := r.resolver.LookupN(key, index+1)
if len(hosts) == 0 {
}
index %= len(hosts)
}
}
func (r *serviceKeyResolverImpl) GetAllAddresses() ([]string, error) {
var all []string
for _, host := range r.resolver.Members() {
all = append(all, host.GetAddress())
}
return all, nil
}