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

feat(appmesh): add timeout support to Routes #11973

Merged
merged 18 commits into from
Dec 15, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
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
6 changes: 5 additions & 1 deletion packages/@aws-cdk/aws-appmesh/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ The _RouteSpec_ class provides an easy interface for defining new protocol speci
The `tcp()`, `http()` and `http2()` methods provide the spec necessary to define a protocol specific spec.

For HTTP based routes, the match field can be used to match on a route prefix.
By default, an HTTP based route will match on `/`. All matches must start with a leading `/`.
By default, an HTTP based route will match on `/`. All matches must start with a leading `/`. The timeout field can also be specified for `idle` and `perRequest` timeouts.

```ts
router.addRoute('route-http', {
Expand All @@ -295,6 +295,10 @@ router.addRoute('route-http', {
match: {
serviceName: 'my-service.default.svc.cluster.local',
},
timeout: {
idle : Duration.seconds(2),
perRequest: Duration.seconds(1),
},
}),
});
```
Expand Down
76 changes: 75 additions & 1 deletion packages/@aws-cdk/aws-appmesh/lib/route-spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as cdk from '@aws-cdk/core';
import { CfnRoute } from './appmesh.generated';
import { Protocol } from './shared-interfaces';
import { Protocol, HttpTimeout, GrpcTimeout, TcpTimeout } from './shared-interfaces';
import { IVirtualNode } from './virtual-node';

/**
Expand Down Expand Up @@ -58,6 +58,13 @@ export interface HttpRouteSpecOptions {
* List of targets that traffic is routed to when a request matches the route
*/
readonly weightedTargets: WeightedTarget[];

/**
* An object that represents a http timeout
*
* @default - None
*/
readonly timeout?: HttpTimeout;
}

/**
Expand All @@ -68,6 +75,13 @@ export interface TcpRouteSpecOptions {
* List of targets that traffic is routed to when a request matches the route
*/
readonly weightedTargets: WeightedTarget[];

/**
* An object that represents a tcp timeout
*
* @default - None
*/
readonly timeout?: TcpTimeout;
}

/**
Expand All @@ -79,6 +93,13 @@ export interface GrpcRouteSpecOptions {
*/
readonly match: GrpcRouteMatch;

/**
* An object that represents a grpc timeout
*
* @default - None
*/
readonly timeout?: GrpcTimeout;

/**
* List of targets that traffic is routed to when a request matches the route
*/
Expand Down Expand Up @@ -166,9 +187,15 @@ class HttpRouteSpec extends RouteSpec {

/**
* The criteria for determining a request match
*
skiyani marked this conversation as resolved.
Show resolved Hide resolved
*/
public readonly match?: HttpRouteMatch;

/**
* The criteria for determining a timeout configuration
*/
public readonly timeout?: HttpTimeout;

/**
* List of targets that traffic is routed to when a request matches the route
*/
Expand All @@ -179,6 +206,7 @@ class HttpRouteSpec extends RouteSpec {
this.protocol = protocol;
this.match = props.match;
this.weightedTargets = props.weightedTargets;
this.timeout = props.timeout;
}

public bind(_scope: cdk.Construct): RouteSpecConfig {
Expand All @@ -193,12 +221,26 @@ class HttpRouteSpec extends RouteSpec {
match: {
prefix: prefixPath,
},
timeout: this.timeout ? this.renderTimeout(this.timeout): undefined,
};
return {
httpRouteSpec: this.protocol === Protocol.HTTP ? httpConfig : undefined,
http2RouteSpec: this.protocol === Protocol.HTTP2 ? httpConfig : undefined,
};
}

private renderTimeout(timeout: HttpTimeout): CfnRoute.HttpTimeoutProperty {
skiyani marked this conversation as resolved.
Show resolved Hide resolved
return {
idle: timeout?.idle !== undefined ? {
unit: 'ms',
value: timeout?.idle.toMilliseconds(),
} : undefined,
perRequest: timeout?.perRequest !== undefined ? {
unit: 'ms',
value: timeout?.perRequest.toMilliseconds(),
} : undefined,
};
}
}

class TcpRouteSpec extends RouteSpec {
Expand All @@ -207,9 +249,15 @@ class TcpRouteSpec extends RouteSpec {
*/
public readonly weightedTargets: WeightedTarget[];

/**
* The criteria for determining a timeout configuration
*/
public readonly timeout?: TcpTimeout;

constructor(props: TcpRouteSpecOptions) {
super();
this.weightedTargets = props.weightedTargets;
this.timeout = props.timeout;
}

public bind(_scope: cdk.Construct): RouteSpecConfig {
Expand All @@ -218,19 +266,31 @@ class TcpRouteSpec extends RouteSpec {
action: {
weightedTargets: renderWeightedTargets(this.weightedTargets),
},
timeout: this.timeout ? this.renderTimeout(this.timeout): undefined,
},
};
}

private renderTimeout(timeout: TcpTimeout): CfnRoute.TcpTimeoutProperty {
return ({
idle: timeout?.idle !== undefined ? {
unit: 'ms',
value: timeout?.idle.toMilliseconds(),
} : undefined,
});
}
}

class GrpcRouteSpec extends RouteSpec {
public readonly weightedTargets: WeightedTarget[];
public readonly match: GrpcRouteMatch;
public readonly timeout?: GrpcTimeout;

constructor(props: GrpcRouteSpecOptions) {
super();
this.weightedTargets = props.weightedTargets;
this.match = props.match;
this.timeout = props.timeout;
}

public bind(_scope: cdk.Construct): RouteSpecConfig {
Expand All @@ -242,9 +302,23 @@ class GrpcRouteSpec extends RouteSpec {
match: {
serviceName: this.match.serviceName,
},
timeout: this.timeout ? this.renderTimeout(this.timeout): undefined,
},
};
}

private renderTimeout(timeout: GrpcTimeout): CfnRoute.GrpcTimeoutProperty {
return ({
idle: timeout?.idle !== undefined ? {
unit: 'ms',
value: timeout?.idle.toMilliseconds(),
} : undefined,
perRequest: timeout?.perRequest !== undefined ? {
unit: 'ms',
value: timeout?.perRequest.toMilliseconds(),
} : undefined,
});
}
}

/**
Expand Down
50 changes: 50 additions & 0 deletions packages/@aws-cdk/aws-appmesh/lib/shared-interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,56 @@
import * as cdk from '@aws-cdk/core';
import { CfnVirtualGateway, CfnVirtualNode } from './appmesh.generated';

/**
* Represents timeouts for HTTP protocols.
*/
export interface HttpTimeout {
/**
* Represents an idle timeout. The amount of time that a connection may be idle.
*
* @default - none
*/
readonly idle?: cdk.Duration;

/**
* Represents per request timeout.
*
* @default - 15 s
*/
readonly perRequest?: cdk.Duration;
}

/**
* Represents timeouts for GRPC protocols.
*/
export interface GrpcTimeout {
/**
* Represents an idle timeout. The amount of time that a connection may be idle.
*
* @default - none
*/
readonly idle?: cdk.Duration;

/**
* Represents per request timeout.
*
* @default - 15 s
*/
readonly perRequest?: cdk.Duration;
}

/**
* Represents timeouts for TCP protocols.
*/
export interface TcpTimeout {
/**
* Represents an idle timeout. The amount of time that a connection may be idle.
*
* @default - none
*/
readonly idle?: cdk.Duration;
}

/**
* Enum of supported AppMesh protocols
*/
Expand Down
52 changes: 1 addition & 51 deletions packages/@aws-cdk/aws-appmesh/lib/virtual-node-listener.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as cdk from '@aws-cdk/core';
import { CfnVirtualNode } from './appmesh.generated';
import { validateHealthChecks } from './private/utils';
import { HealthCheck, Protocol } from './shared-interfaces';
import { HealthCheck, Protocol, HttpTimeout, GrpcTimeout, TcpTimeout } from './shared-interfaces';

/**
* Properties for a VirtualNode listener
Expand Down Expand Up @@ -68,56 +68,6 @@ export interface TcpVirtualNodeListenerOptions extends VirtualNodeListenerCommon
readonly timeout?: TcpTimeout;
}

/**
* Represents timeouts for HTTP protocols.
*/
export interface HttpTimeout {
/**
* Represents an idle timeout. The amount of time that a connection may be idle.
*
* @default - none
*/
readonly idle?: cdk.Duration;

/**
* Represents per request timeout.
*
* @default - 15 s
*/
readonly perRequest?: cdk.Duration;
}

/**
* Represents timeouts for GRPC protocols.
*/
export interface GrpcTimeout {
/**
* Represents an idle timeout. The amount of time that a connection may be idle.
*
* @default - none
*/
readonly idle?: cdk.Duration;

/**
* Represents per request timeout.
*
* @default - 15 s
*/
readonly perRequest?: cdk.Duration;
}

/**
* Represents timeouts for TCP protocols.
*/
export interface TcpTimeout {
/**
* Represents an idle timeout. The amount of time that a connection may be idle.
*
* @default - none
*/
readonly idle?: cdk.Duration;
}

/**
* Defines listener for a VirtualNode
*/
Expand Down
5 changes: 4 additions & 1 deletion packages/@aws-cdk/aws-appmesh/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,10 @@
"duration-prop-type:@aws-cdk/aws-appmesh.GrpcVirtualNodeListenerOptions.timeout",
"duration-prop-type:@aws-cdk/aws-appmesh.HttpVirtualNodeListenerOptions.timeout",
"duration-prop-type:@aws-cdk/aws-appmesh.Http2VirtualNodeListenerOptions.timeout",
"duration-prop-type:@aws-cdk/aws-appmesh.TcpVirtualNodeListenerOptions.timeout"
"duration-prop-type:@aws-cdk/aws-appmesh.TcpVirtualNodeListenerOptions.timeout",
"duration-prop-type:@aws-cdk/aws-appmesh.GrpcRouteSpecOptions.timeout",
"duration-prop-type:@aws-cdk/aws-appmesh.HttpRouteSpecOptions.timeout",
"duration-prop-type:@aws-cdk/aws-appmesh.TcpRouteSpecOptions.timeout"
]
},
"stability": "experimental",
Expand Down
26 changes: 26 additions & 0 deletions packages/@aws-cdk/aws-appmesh/test/integ.mesh.expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,16 @@
},
"Match": {
"Prefix": "/"
},
"Timeout": {
"Idle": {
"Unit": "ms",
"Value": 10000
},
"PerRequest": {
"Unit": "ms",
"Value": 10000
}
}
}
},
Expand Down Expand Up @@ -553,6 +563,16 @@
},
"Match": {
"Prefix": "/path2"
},
"Timeout": {
"Idle": {
"Unit": "ms",
"Value": 11000
},
"PerRequest": {
"Unit": "ms",
"Value": 11000
}
}
}
},
Expand Down Expand Up @@ -588,6 +608,12 @@
"Weight": 20
}
]
},
"Timeout": {
"Idle": {
"Unit": "ms",
"Value": 12000
}
}
}
},
Expand Down
Loading