Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Choreo] Upstream connection handling support considering TCP and HTTP #3625

Open
wants to merge 3 commits into
base: choreo
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions adapter/cmd/adapter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func startMicroGateway() {
logger.Fatal("Error starting the adapter", err)
}
conf, errReadConfig := config.ReadConfigs()
config.GetTcpKeepaliveEnabledOrgs()
if errReadConfig != nil {
logger.Fatal("Error loading configuration. ", errReadConfig)
}
Expand Down
9 changes: 9 additions & 0 deletions adapter/config/default_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,15 @@ var defaultConfig = &Config{
MaxRetries: 50,
},
},
TcpConfigurations: upstreamTcpConfigs{
KeepaliveTimeInMillis: 120000,
KeepaliveProbes: 9,
KeepaliveIntervalInMillis: 75,
},
HttpConfigurations: upstreamHttpConfigs{
IdleTimeoutInMillis: 120000,
MaxConnectionDurationInMillis: 120000,
},
},
Connection: connection{
Timeouts: connectionTimeouts{
Expand Down
20 changes: 15 additions & 5 deletions adapter/config/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,12 @@ import (
)

var (
onceConfigRead sync.Once
onceGetDefaultVhost sync.Once
adapterConfig *Config
defaultVhost map[string]string
e error
onceConfigRead sync.Once
onceGetDefaultVhost sync.Once
adapterConfig *Config
defaultVhost map[string]string
e error
UpstreamConnectionConfEnabledOrgList []string
)

// DefaultGatewayName represents the name of the default gateway
Expand Down Expand Up @@ -255,3 +256,12 @@ func (config *Config) validateConfig() error {
func printDeprecatedWarningLog(deprecatedTerm, currentTerm string) {
logger.Warnf("%s is deprecated. Use %s instead", deprecatedTerm, currentTerm)
}

// GetTcpKeepaliveEnabledOrgs returns the list of orgs that need to handle connection timeouts
func GetTcpKeepaliveEnabledOrgs() {
orgs := os.Getenv("TCP_KEEPALIVE_ENABLED_ORGS")
UpstreamConnectionConfEnabledOrgList = strings.Split(orgs, ",")
if len(UpstreamConnectionConfEnabledOrgList) == 0 {
UpstreamConnectionConfEnabledOrgList[0] = ""
}
}
24 changes: 18 additions & 6 deletions adapter/config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,12 +235,14 @@ type globalCors struct {
// Envoy Upstream Related Configurations
type envoyUpstream struct {
// UpstreamTLS related Configuration
TLS upstreamTLS
Timeouts upstreamTimeout
Health upstreamHealth
DNS upstreamDNS
Retry upstreamRetry
CircuitBreakers []upstreamCircuitBreaker
TLS upstreamTLS
Timeouts upstreamTimeout
Health upstreamHealth
DNS upstreamDNS
Retry upstreamRetry
CircuitBreakers []upstreamCircuitBreaker
TcpConfigurations upstreamTcpConfigs
HttpConfigurations upstreamHttpConfigs
}

type upstreamTLS struct {
Expand Down Expand Up @@ -276,6 +278,16 @@ type dnsResolverConfig struct {
CAres cAres
}

type upstreamTcpConfigs struct {
KeepaliveTimeInMillis uint32
KeepaliveProbes uint32
KeepaliveIntervalInMillis uint32
}
type upstreamHttpConfigs struct {
IdleTimeoutInMillis uint32
MaxConnectionDurationInMillis uint32
}

type dnsResolverType string

const (
Expand Down
6 changes: 5 additions & 1 deletion adapter/internal/api/apis_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,11 @@ func ProcessMountedAPIProjects() (err error) {

func validateAndUpdateXds(apiProject mgw.ProjectAPI, override *bool) (err error) {
apiYaml := apiProject.APIYaml.Data
apiProject.OrganizationID = config.GetControlPlaneConnectedTenantDomain()
if (apiProject.APIYaml.Data.OrganizationID != "") {
apiProject.OrganizationID = apiProject.APIYaml.Data.OrganizationID
} else {
apiProject.OrganizationID = config.GetControlPlaneConnectedTenantDomain()
}

// handle panic
defer func() {
Expand Down
33 changes: 33 additions & 0 deletions adapter/internal/oasparser/envoyconf/routes_with_clusters.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ func CreateRoutesWithClusters(mgwSwagger model.MgwSwagger, upstreamCerts map[str
if !strings.Contains(apiLevelEndpointProd.EndpointPrefix, xWso2EPClustersConfigNamePrefix) {
cluster, address, err := processEndpoints(apiLevelClusterNameProd, apiLevelEndpointProd,
upstreamCerts, timeout, apiLevelbasePath)
// assigns specified values for TCP keep-alive and HTTP timeout considering specific organizations
ok := slices.Contains(config.UpstreamConnectionConfEnabledOrgList, organizationID)
if ok {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • support

getKeepAliveConfigs(cluster, conf)
}
if err != nil {
apiLevelClusterNameProd = ""
logger.LoggerOasparser.Errorf("Error while adding api level production endpoints for %s. %v , skipping api...", apiTitle, err.Error())
Expand Down Expand Up @@ -343,6 +348,34 @@ func getClusterName(epPrefix string, organizationID string, vHost string, swagge
swaggerVersion)
}

func getKeepAliveConfigs(cluster *clusterv3.Cluster, conf *config.Config) {
cluster.UpstreamConnectionOptions = &clusterv3.UpstreamConnectionOptions{
TcpKeepalive: &corev3.TcpKeepalive{
KeepaliveProbes: wrapperspb.UInt32(conf.Envoy.Upstream.TcpConfigurations.KeepaliveProbes),
KeepaliveInterval: wrapperspb.UInt32(conf.Envoy.Upstream.TcpConfigurations.KeepaliveIntervalInMillis),
KeepaliveTime: wrapperspb.UInt32(conf.Envoy.Upstream.TcpConfigurations.KeepaliveTimeInMillis / 1000),
},
}

config := &upstreams.HttpProtocolOptions{
CommonHttpProtocolOptions: &corev3.HttpProtocolOptions{
IdleTimeout: durationpb.New(time.Duration(conf.Envoy.Upstream.HttpConfigurations.IdleTimeoutInMillis) * time.Millisecond),
MaxConnectionDuration: durationpb.New(time.Duration(conf.Envoy.Upstream.HttpConfigurations.MaxConnectionDurationInMillis) * time.Millisecond),
},
UpstreamProtocolOptions: &upstreams.HttpProtocolOptions_UseDownstreamProtocolConfig{},
}
MarshalledHTTPProtocolOptions, err := proto.Marshal(config)
if err != nil {
logger.LoggerOasparser.Error("Error while marshalling the upstream TCP keep alive config")
}
cluster.TypedExtensionProtocolOptions = map[string]*anypb.Any{
"envoy.extensions.upstreams.http.v3.HttpProtocolOptions": {
TypeUrl: httpProtocolOptionsName,
Value: MarshalledHTTPProtocolOptions,
},
}
}

// CreateLuaCluster creates lua cluster configuration.
func CreateLuaCluster(interceptorCerts map[string][]byte, endpoint model.InterceptEndpoint) (*clusterv3.Cluster, []*corev3.Address, error) {
logger.LoggerOasparser.Debug("creating a lua cluster ", endpoint.ClusterName)
Expand Down
16 changes: 16 additions & 0 deletions resources/conf/config.toml.template
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,22 @@ retainKeys = ["self_validate_jwt", "issuer", "claim_mappings", "consumer_key_cla
# Max interval for the Envoy's exponential retry back off algorithm
maxInterval = "500ms"

# TCP configurations applicable with the upstream clusters
[router.upstream.tcpConfigurations]
# The number of milliseconds a connection needs to be idle before keep-alive probes start being sent
keepaliveTimeInMillis = 120000
# Maximum number of keep-alive probes to send without response before deciding the connection is dead
keepaliveProbes = 9

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Too high again?

Suggested change
keepaliveProbes = 9
keepaliveProbes = 3

# The number of milliseconds between keep-alive probes
keepaliveIntervalInMillis = 75
slahirucd7 marked this conversation as resolved.
Show resolved Hide resolved

# HTTP configurations applicable with the upstream clusters
[router.upstream.httpConfigurations]
# Idle timeout in milliseconds for connections
idleTimeoutInMillis = 120000

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is too low for default. We have to set a value which is more than max response timeout (300s)

Suggested change
idleTimeoutInMillis = 120000
idleTimeoutInMillis = 360000

# The maximum duration of a connection in milliseconds
maxConnectionDurationInMillis = 120000
slahirucd7 marked this conversation as resolved.
Show resolved Hide resolved

# Timeouts managed by the connection manager
[router.connectionTimeout]
# The amount of time that Envoy will wait for the entire request to be received. Time from client to upstream.
Expand Down
Loading