Skip to content

Commit

Permalink
feat(appmesh): add Connection Pools for VirtualNode and VirtualGateway (
Browse files Browse the repository at this point in the history
#13917)

Adds connection pools to VirtualNodes and VirtualGateways. VirtualNodes support listeners for HTTP, HTTP2, gRPC and TPC. VirtualGateways support only HTTP, HTTP2 and gRPC (no TCP). There are connection pool configurations for each of these 7 node/protocol combinations.

* HTTP connection pools need these properties: maxConnections, maxPendingRequests
* HTTP2 connection pools need these properties: maxRequests
* gRPC connection pools need these properties: maxRequests
* TCP connection pools need these properties: maxConnections

Since connection pool properties differ by protocol so it's important to enforce the correct fields in CDK. To accomplish this  I've added interfaces per protocol type and squashed API config `ConnectionPool > [Protocol] > properties` to the equivalent `ConnectionPool > properties` CDK config. This is ok because the user will never mismatch protocol types. From [docs (see connection pool section)](https://docs.aws.amazon.com/app-mesh/latest/userguide/virtual_gateways.html):
> The connectionPool and portMapping protocols must be the same. If your listener protocol is grpc or http2, specify maxRequests only. If your listener protocol is http, you can specify both maxConnections and maxPendingRequests. 

```ts
new appmesh.VirtualNode(stack, 'test-node', {
  mesh,
  listeners: [
    appmesh.VirtualNodeListener.tcp({
      port: 80,
      connectionPool: {
        maxConnections: 100,
      },
    }),
  ],
});
```

Pre-addressing the `ConnectionPoolConfig` placeholder interface & lint ignore statement: I’ve added this so I can treat the connection pool types as sub-classes while taking advantage of the interface object literal pattern. Cleaner UX compared to using classes.

BREAKING CHANGE: HTTP2 `VirtualNodeListener`s must be now created with `Http2VirtualNodeListenerOptions`
* **appmesh**: HTTP2 `VirtualGatewayListener`s must be now created with `Http2VirtualGatewayListenerOptions`

Closes #11647

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
alexbrjo committed Apr 16, 2021
1 parent 61ad0aa commit 8a949dc
Show file tree
Hide file tree
Showing 7 changed files with 512 additions and 33 deletions.
32 changes: 32 additions & 0 deletions packages/@aws-cdk/aws-appmesh/README.md
Expand Up @@ -306,6 +306,38 @@ const node = mesh.addVirtualNode('virtual-node', {
});
```

## Adding a connection pool to a listener

The `connectionPool` property can be added to a Virtual Node listener or Virtual Gateway listener to add a request connection pool. There are different
connection pool properties per listener protocol types.

```typescript
// A Virtual Node with a gRPC listener with a connection pool set
const node = new appmesh.VirtualNode(stack, 'node', {
mesh,
dnsHostName: 'node',
listeners: [appmesh.VirtualNodeListener.http({
port: 80,
connectionPool: {
maxConnections: 100,
maxPendingRequests: 10,
},
})],
});

// A Virtual Gateway with a gRPC listener with a connection pool set
const gateway = new appmesh.VirtualGateway(this, 'gateway', {
mesh: mesh,
listeners: [appmesh.VirtualGatewayListener.grpc({
port: 8080,
connectionPool: {
maxRequests: 10,
},
})],
virtualGatewayName: 'gateway',
});
```

## Adding a Route

A `route` is associated with a virtual router, and it's used to match requests for a virtual router and distribute traffic accordingly to its associated virtual nodes.
Expand Down
28 changes: 27 additions & 1 deletion packages/@aws-cdk/aws-appmesh/lib/private/utils.ts
Expand Up @@ -38,4 +38,30 @@ const HEALTH_CHECK_PROPERTY_THRESHOLDS: {[key in (keyof AppMeshHealthCheck)]?: [
port: [1, 65535],
timeoutMillis: [2000, 60000],
unhealthyThreshold: [2, 10],
};
};

/**
* Generated Connection pool config
*/
export interface ConnectionPoolConfig {
/**
* The maximum connections in the pool
*
* @default - none
*/
readonly maxConnections?: number;

/**
* The maximum pending requests in the pool
*
* @default - none
*/
readonly maxPendingRequests?: number;

/**
* The maximum requests in the pool
*
* @default - none
*/
readonly maxRequests?: number;
}
55 changes: 55 additions & 0 deletions packages/@aws-cdk/aws-appmesh/lib/shared-interfaces.ts
Expand Up @@ -299,3 +299,58 @@ class VirtualServiceBackend extends Backend {
};
}
}

/**
* Connection pool properties for HTTP listeners
*/
export interface HttpConnectionPool {
/**
* The maximum connections in the pool
*
* @default - none
*/
readonly maxConnections: number;

/**
* The maximum pending requests in the pool
*
* @default - none
*/
readonly maxPendingRequests: number;
}

/**
* Connection pool properties for TCP listeners
*/
export interface TcpConnectionPool {
/**
* The maximum connections in the pool
*
* @default - none
*/
readonly maxConnections: number;
}

/**
* Connection pool properties for gRPC listeners
*/
export interface GrpcConnectionPool {
/**
* The maximum requests in the pool
*
* @default - none
*/
readonly maxRequests: number;
}

/**
* Connection pool properties for HTTP2 listeners
*/
export interface Http2ConnectionPool {
/**
* The maximum requests in the pool
*
* @default - none
*/
readonly maxRequests: number;
}
69 changes: 49 additions & 20 deletions packages/@aws-cdk/aws-appmesh/lib/virtual-gateway-listener.ts
@@ -1,17 +1,23 @@
import * as cdk from '@aws-cdk/core';
import { CfnVirtualGateway } from './appmesh.generated';
import { validateHealthChecks } from './private/utils';
import { HealthCheck, Protocol } from './shared-interfaces';
import { validateHealthChecks, ConnectionPoolConfig } from './private/utils';
import {
GrpcConnectionPool,
HealthCheck,
Http2ConnectionPool,
HttpConnectionPool,
Protocol,
} from './shared-interfaces';
import { TlsCertificate, TlsCertificateConfig } from './tls-certificate';

// keep this import separate from other imports to reduce chance for merge conflicts with v2-main
// eslint-disable-next-line no-duplicate-imports, import/order
import { Construct } from '@aws-cdk/core';

/**
* Represents the properties needed to define HTTP Listeners for a VirtualGateway
* Represents the properties needed to define a Listeners for a VirtualGateway
*/
export interface HttpGatewayListenerOptions {
interface VirtualGatewayListenerCommonOptions {
/**
* Port to listen for connections on
*
Expand All @@ -35,29 +41,39 @@ export interface HttpGatewayListenerOptions {
}

/**
* Represents the properties needed to define GRPC Listeners for a VirtualGateway
* Represents the properties needed to define HTTP Listeners for a VirtualGateway
*/
export interface GrpcGatewayListenerOptions {
export interface HttpGatewayListenerOptions extends VirtualGatewayListenerCommonOptions {
/**
* Port to listen for connections on
* Connection pool for http listeners
*
* @default - 8080
* @default - None
*/
readonly port?: number
readonly connectionPool?: HttpConnectionPool;
}

/**
* Represents the properties needed to define HTTP2 Listeners for a VirtualGateway
*/
export interface Http2GatewayListenerOptions extends VirtualGatewayListenerCommonOptions {
/**
* The health check information for the listener
* Connection pool for http listeners
*
* @default - no healthcheck
* @default - None
*/
readonly healthCheck?: HealthCheck;
readonly connectionPool?: Http2ConnectionPool;
}

/**
* Represents the properties needed to define GRPC Listeners for a VirtualGateway
*/
export interface GrpcGatewayListenerOptions extends VirtualGatewayListenerCommonOptions {
/**
* Represents the listener certificate
* Connection pool for http listeners
*
* @default - none
* @default - None
*/
readonly tlsCertificate?: TlsCertificate;
readonly connectionPool?: GrpcConnectionPool;
}

/**
Expand All @@ -78,21 +94,21 @@ export abstract class VirtualGatewayListener {
* Returns an HTTP Listener for a VirtualGateway
*/
public static http(options: HttpGatewayListenerOptions = {}): VirtualGatewayListener {
return new VirtualGatewayListenerImpl(Protocol.HTTP, options.healthCheck, options.port, options.tlsCertificate);
return new VirtualGatewayListenerImpl(Protocol.HTTP, options.healthCheck, options.port, options.tlsCertificate, options.connectionPool);
}

/**
* Returns an HTTP2 Listener for a VirtualGateway
*/
public static http2(options: HttpGatewayListenerOptions = {}): VirtualGatewayListener {
return new VirtualGatewayListenerImpl(Protocol.HTTP2, options.healthCheck, options.port, options.tlsCertificate);
public static http2(options: Http2GatewayListenerOptions = {}): VirtualGatewayListener {
return new VirtualGatewayListenerImpl(Protocol.HTTP2, options.healthCheck, options.port, options.tlsCertificate, options.connectionPool);
}

/**
* Returns a GRPC Listener for a VirtualGateway
*/
public static grpc(options: GrpcGatewayListenerOptions = {}): VirtualGatewayListener {
return new VirtualGatewayListenerImpl(Protocol.GRPC, options.healthCheck, options.port, options.tlsCertificate);
return new VirtualGatewayListenerImpl(Protocol.GRPC, options.healthCheck, options.port, options.tlsCertificate, options.connectionPool);
}

/**
Expand All @@ -110,7 +126,8 @@ class VirtualGatewayListenerImpl extends VirtualGatewayListener {
constructor(private readonly protocol: Protocol,
private readonly healthCheck: HealthCheck | undefined,
private readonly port: number = 8080,
private readonly tlsCertificate: TlsCertificate | undefined) {
private readonly tlsCertificate: TlsCertificate | undefined,
private readonly connectionPool: ConnectionPoolConfig | undefined) {
super();
}

Expand All @@ -128,6 +145,7 @@ class VirtualGatewayListenerImpl extends VirtualGatewayListener {
},
healthCheck: this.healthCheck ? renderHealthCheck(this.healthCheck, this.protocol, this.port): undefined,
tls: tlsConfig ? renderTls(tlsConfig) : undefined,
connectionPool: this.connectionPool ? renderConnectionPool(this.connectionPool, this.protocol) : undefined,
},
};
}
Expand Down Expand Up @@ -171,3 +189,14 @@ function renderHealthCheck(hc: HealthCheck, listenerProtocol: Protocol,

return healthCheck;
}

function renderConnectionPool(connectionPool: ConnectionPoolConfig, listenerProtocol: Protocol):
CfnVirtualGateway.VirtualGatewayConnectionPoolProperty {
return ({
[listenerProtocol]: {
maxRequests: connectionPool?.maxRequests !== undefined ? connectionPool.maxRequests : undefined,
maxConnections: connectionPool?.maxConnections !== undefined ? connectionPool.maxConnections : undefined,
maxPendingRequests: connectionPool?.maxPendingRequests !== undefined ? connectionPool.maxPendingRequests : undefined,
},
});
}

0 comments on commit 8a949dc

Please sign in to comment.