Files
tasks/common/sdk/factory.go
Jeremy BreidingandGitHub 6a9e262126 Replacing SdkClient injection with SdkClientFactory injection. (#2611)
* Replacing SdkClient injection with SdkClientFactory injection.

* Remove extra DisableHealthCheck usages

* Reconciling review comments

* retry for all clients, but only crash for system clients
2022-03-17 15:05:56 -07:00

108 lines
3.2 KiB
Go

// The MIT License
//
// Copyright (c) 2020 Temporal Technologies Inc. All rights reserved.
//
// Copyright (c) 2020 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//go:generate mockgen -copyright_file ../../LICENSE -package $GOPACKAGE -source $GOFILE -destination factory_mock.go
package sdk
import (
"crypto/tls"
"fmt"
"sync"
sdkclient "go.temporal.io/sdk/client"
"go.temporal.io/server/common"
"go.temporal.io/server/common/backoff"
"go.temporal.io/server/common/log"
"go.temporal.io/server/common/log/tag"
)
type (
ClientFactory interface {
NewClient(namespaceName string, logger log.Logger) (sdkclient.Client, error)
GetSystemClient(logger log.Logger) sdkclient.Client
}
clientFactory struct {
hostPort string
tlsConfig *tls.Config
metricsHandler *MetricsHandler
systemSdkClient sdkclient.Client
once *sync.Once
}
)
var _ ClientFactory = (*clientFactory)(nil)
func NewClientFactory(hostPort string, tlsConfig *tls.Config, metricsHandler *MetricsHandler) *clientFactory {
return &clientFactory{
hostPort: hostPort,
tlsConfig: tlsConfig,
metricsHandler: metricsHandler,
once: &sync.Once{},
}
}
func (f *clientFactory) NewClient(namespaceName string, logger log.Logger) (sdkclient.Client, error) {
var client sdkclient.Client
// Retry for up to 1m, handles frontend service not ready
err := backoff.Retry(func() error {
sdkClient, err := sdkclient.NewClient(sdkclient.Options{
HostPort: f.hostPort,
Namespace: namespaceName,
MetricsHandler: f.metricsHandler,
Logger: log.NewSdkLogger(logger),
ConnectionOptions: sdkclient.ConnectionOptions{
TLS: f.tlsConfig,
},
})
if err != nil {
return fmt.Errorf("unable to create SDK client: %w", err)
}
client = sdkClient
return nil
}, common.CreateSdkClientFactoryRetryPolicy(), common.IsContextDeadlineExceededErr)
return client, err
}
func (f *clientFactory) GetSystemClient(logger log.Logger) sdkclient.Client {
f.once.Do(func() {
client, err := f.NewClient(common.SystemLocalNamespace, logger)
if err != nil {
logger.Fatal(
"error getting system sdk client",
tag.Error(err),
)
}
f.systemSdkClient = client
})
return f.systemSdkClient
}