mirror of
https://github.com/siderolabs/talos.git
synced 2025-10-10 15:11:15 +02:00
Updates: * pkgs v1.3.0-alpha.0-33-g8fe5cbc * tools v1.3.0-alpha.0-20-g3b5f89a * aws-sdk-go v1.44.120 * docker v20.10.20+incompatible * fsnotify v1.6.0 * nftables v0.0.0-20221015190445-4f5cd5826fbd * gen v0.4.0 * grpc-proxy v0.4.0 * spf13/cobra v1.6.0 * u-root v0.10.0 * x/net v0.1.0 * x/sync v0.1.0 * x/sys v0.1.0 * x/term v0.1.0 * x/time v0.1.0 * grpc v1.50.1 * genproto v0.0.0-20221018160656-63c7b68cfc55 * Linux kernel 5.15.74 Signed-off-by: Tim Jones <tim.jones@siderolabs.com>
56 lines
1.3 KiB
Go
56 lines
1.3 KiB
Go
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
package backend
|
|
|
|
import (
|
|
"crypto/tls"
|
|
"sync"
|
|
|
|
"github.com/siderolabs/grpc-proxy/proxy"
|
|
"google.golang.org/grpc/credentials"
|
|
)
|
|
|
|
// APIDFactory caches connection to apid instances by target.
|
|
//
|
|
// TODO: need to clean up idle connections from time to time.
|
|
type APIDFactory struct {
|
|
cache sync.Map
|
|
creds credentials.TransportCredentials
|
|
}
|
|
|
|
// NewAPIDFactory creates new APIDFactory with given tls.Config.
|
|
//
|
|
// Client TLS config is used to connect to other apid instances.
|
|
func NewAPIDFactory(config *tls.Config) *APIDFactory {
|
|
return &APIDFactory{
|
|
creds: credentials.NewTLS(config),
|
|
}
|
|
}
|
|
|
|
// Get backend by target.
|
|
//
|
|
// Get performs caching of backends.
|
|
func (factory *APIDFactory) Get(target string) (proxy.Backend, error) {
|
|
b, ok := factory.cache.Load(target)
|
|
if ok {
|
|
return b.(proxy.Backend), nil
|
|
}
|
|
|
|
backend, err := NewAPID(target, factory.creds)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
existing, loaded := factory.cache.LoadOrStore(target, backend)
|
|
if loaded {
|
|
// race: another Get() call built different backend
|
|
backend.Close()
|
|
|
|
return existing.(proxy.Backend), nil
|
|
}
|
|
|
|
return backend, nil
|
|
}
|