mirror of
https://github.com/hanzoai/tasks.git
synced 2026-11-02 12:22:18 +00:00
## What changed? This PR aims to avoid usage of [`strings.Split`](https://pkg.go.dev/strings#Split) where possible in favor of better string splitting techniques, speficially: [`strings.SplitN`](https://pkg.go.dev/strings#SplitN) and [`strings.SplitSeq`](https://pkg.go.dev/strings#SplitSeq) where appropriate. There was also a [`strings.Fields`](https://pkg.go.dev/strings#Fields) change I made to use [`strings.FieldsSeq`](https://pkg.go.dev/strings#FieldsSeq) instead, and another for S3 to use the [`path`](https://pkg.go.dev/path) package instead of [`strings.Split`](https://pkg.go.dev/strings#Split). ## Why? [`strings.SplitN`](https://pkg.go.dev/strings#SplitN) and [`strings.SplitSeq`](https://pkg.go.dev/strings#SplitSeq) are often better options in many cases, and can be _partially_ detected using [`modernize`](https://pkg.go.dev/golang.org/x/tools/gopls/internal/analysis/modernize): > `stringsseq`: replace Split in "for range strings.Split(...)" by go1.24's more efficient `SplitSeq`, or `Fields` with `FieldSeq`. ## How did you test it? - [X] built - [X] run locally and tested manually - [x] covered by existing tests - [x] added new unit test(s) - [ ] added new functional test(s) ## Potential risks There are lots of potentially subtle behaviors from the `strings.Split` (and `strings.Fields`) usage that should be accounted for. If our existing tests don't cover those subtleties, there's risk for introducing an unintended bug. More intricate handling/parsing previously using the `strings` package should get extra attention from reviewers. I've attempted to break up my changes into logical commit chunks to aid in review / help spot potentially concerning changes.
183 lines
5.0 KiB
Go
183 lines
5.0 KiB
Go
package gocql
|
|
|
|
import (
|
|
"crypto/tls"
|
|
"crypto/x509"
|
|
"encoding/base64"
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/gocql/gocql"
|
|
"go.temporal.io/server/common/auth"
|
|
"go.temporal.io/server/common/config"
|
|
"go.temporal.io/server/common/debug"
|
|
"go.temporal.io/server/common/persistence/nosql/nosqlplugin/cassandra/translator"
|
|
"go.temporal.io/server/common/resolver"
|
|
)
|
|
|
|
func NewCassandraCluster(
|
|
cfg config.Cassandra,
|
|
resolver resolver.ServiceResolver,
|
|
) (*gocql.ClusterConfig, error) {
|
|
var resolvedHosts []string
|
|
for _, host := range parseHosts(cfg.Hosts) {
|
|
resolvedHosts = append(resolvedHosts, resolver.Resolve(host)...)
|
|
}
|
|
|
|
cluster := gocql.NewCluster(resolvedHosts...)
|
|
if err := ConfigureCassandraCluster(cfg, cluster); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return cluster, nil
|
|
}
|
|
|
|
// Modifies the input cluster config in place.
|
|
//
|
|
//nolint:revive // cognitive complexity 61 (> max enabled 25)
|
|
func ConfigureCassandraCluster(cfg config.Cassandra, cluster *gocql.ClusterConfig) error {
|
|
cluster.ProtoVersion = 4
|
|
if cfg.Port > 0 {
|
|
cluster.Port = cfg.Port
|
|
}
|
|
if cfg.User != "" && cfg.Password != "" {
|
|
cluster.Authenticator = gocql.PasswordAuthenticator{
|
|
Username: cfg.User,
|
|
Password: cfg.Password,
|
|
AllowedAuthenticators: cfg.AllowedAuthenticators,
|
|
}
|
|
}
|
|
if cfg.Keyspace != "" {
|
|
cluster.Keyspace = cfg.Keyspace
|
|
}
|
|
if cfg.Datacenter != "" {
|
|
cluster.HostFilter = gocql.DataCentreHostFilter(cfg.Datacenter)
|
|
}
|
|
if cfg.TLS != nil && cfg.TLS.Enabled {
|
|
if cfg.TLS.CertData != "" && cfg.TLS.CertFile != "" {
|
|
return errors.New("only one of certData or certFile properties should be specified")
|
|
}
|
|
|
|
if cfg.TLS.KeyData != "" && cfg.TLS.KeyFile != "" {
|
|
return errors.New("only one of keyData or keyFile properties should be specified")
|
|
}
|
|
|
|
if cfg.TLS.CaData != "" && cfg.TLS.CaFile != "" {
|
|
return errors.New("only one of caData or caFile properties should be specified")
|
|
}
|
|
|
|
cluster.SslOpts = &gocql.SslOptions{
|
|
CaPath: cfg.TLS.CaFile,
|
|
EnableHostVerification: cfg.TLS.EnableHostVerification,
|
|
Config: auth.NewTLSConfigForServer(cfg.TLS.ServerName, cfg.TLS.EnableHostVerification),
|
|
}
|
|
|
|
var certBytes []byte
|
|
var keyBytes []byte
|
|
var err error
|
|
|
|
if cfg.TLS.CertFile != "" {
|
|
certBytes, err = os.ReadFile(cfg.TLS.CertFile)
|
|
if err != nil {
|
|
return fmt.Errorf("error reading client certificate file: %w", err)
|
|
}
|
|
} else if cfg.TLS.CertData != "" {
|
|
certBytes, err = base64.StdEncoding.DecodeString(cfg.TLS.CertData)
|
|
if err != nil {
|
|
return fmt.Errorf("client certificate could not be decoded: %w", err)
|
|
}
|
|
}
|
|
|
|
if cfg.TLS.KeyFile != "" {
|
|
keyBytes, err = os.ReadFile(cfg.TLS.KeyFile)
|
|
if err != nil {
|
|
return fmt.Errorf("error reading client certificate private key file: %w", err)
|
|
}
|
|
} else if cfg.TLS.KeyData != "" {
|
|
keyBytes, err = base64.StdEncoding.DecodeString(cfg.TLS.KeyData)
|
|
if err != nil {
|
|
return fmt.Errorf("client certificate private key could not be decoded: %w", err)
|
|
}
|
|
}
|
|
|
|
if len(certBytes) > 0 {
|
|
clientCert, err := tls.X509KeyPair(certBytes, keyBytes)
|
|
if err != nil {
|
|
return fmt.Errorf("unable to generate x509 key pair: %w", err)
|
|
}
|
|
|
|
cluster.SslOpts.Certificates = []tls.Certificate{clientCert}
|
|
}
|
|
|
|
if cfg.TLS.CaData != "" {
|
|
cluster.SslOpts.RootCAs = x509.NewCertPool()
|
|
pem, err := base64.StdEncoding.DecodeString(cfg.TLS.CaData)
|
|
if err != nil {
|
|
return fmt.Errorf("caData could not be decoded: %w", err)
|
|
}
|
|
if !cluster.SslOpts.RootCAs.AppendCertsFromPEM(pem) {
|
|
return errors.New("failed to load decoded CA Cert as PEM")
|
|
}
|
|
}
|
|
}
|
|
|
|
if cfg.MaxConns > 0 {
|
|
cluster.NumConns = cfg.MaxConns
|
|
}
|
|
|
|
cluster.ConnectTimeout = 10 * time.Second * debug.TimeoutMultiplier
|
|
if cfg.ConnectTimeout > 0 {
|
|
cluster.ConnectTimeout = cfg.ConnectTimeout
|
|
}
|
|
|
|
cluster.Timeout = cluster.ConnectTimeout
|
|
if cfg.Timeout > 0 {
|
|
cluster.Timeout = cfg.Timeout
|
|
}
|
|
|
|
cluster.WriteTimeout = cluster.Timeout
|
|
if cfg.WriteTimeout > 0 {
|
|
cluster.WriteTimeout = cfg.WriteTimeout
|
|
}
|
|
|
|
cluster.ProtoVersion = 4
|
|
cluster.Consistency = cfg.Consistency.GetConsistency()
|
|
cluster.SerialConsistency = cfg.Consistency.GetSerialConsistency()
|
|
cluster.DisableInitialHostLookup = cfg.DisableInitialHostLookup
|
|
|
|
cluster.ReconnectionPolicy = &gocql.ExponentialReconnectionPolicy{
|
|
MaxRetries: 30,
|
|
InitialInterval: time.Second,
|
|
MaxInterval: 10 * time.Second,
|
|
}
|
|
|
|
cluster.PoolConfig.HostSelectionPolicy = gocql.TokenAwareHostPolicy(gocql.RoundRobinHostPolicy())
|
|
|
|
if cfg.AddressTranslator != nil && cfg.AddressTranslator.Translator != "" {
|
|
addressTranslator, err := translator.LookupTranslator(cfg.AddressTranslator.Translator)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
cluster.AddressTranslator, err = addressTranslator.GetTranslator(&cfg)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// parseHosts returns parses a list of hosts separated by comma
|
|
func parseHosts(input string) []string {
|
|
var hosts []string
|
|
for h := range strings.SplitSeq(input, ",") {
|
|
if host := strings.TrimSpace(h); len(host) > 0 {
|
|
hosts = append(hosts, host)
|
|
}
|
|
}
|
|
return hosts
|
|
}
|