Skip to content

Commit

Permalink
feat: add support of message level latency
Browse files Browse the repository at this point in the history
BREAKING CHANGE: this version is using the latest dependencies introduced by Gravitee V4.0
  • Loading branch information
guillaumelamirand committed Jun 22, 2023
1 parent 6f29b5f commit 8d25c60
Show file tree
Hide file tree
Showing 17 changed files with 742 additions and 179 deletions.
48 changes: 24 additions & 24 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,32 @@ version: 2.1
setup: true

orbs:
gravitee: gravitee-io/gravitee@2.1
gravitee: gravitee-io/gravitee@4.0.0

# our single workflow, that triggers the setup job defined above, filters on tag and branches are needed otherwise
# some workflow and job will not be triggered for tags (default CircleCI behavior)
workflows:
setup_build:
when:
not: << pipeline.git.tag >>
jobs:
- gravitee/setup_plugin-build-config:
filters:
tags:
ignore:
- /.*/
setup_build:
when:
not: << pipeline.git.tag >>
jobs:
- gravitee/setup_plugin-build-config:
filters:
tags:
ignore:
- /.*/

setup_release:
when:
matches:
pattern: "^[0-9]+\\.[0-9]+\\.[0-9]+$"
value: << pipeline.git.tag >>
jobs:
- gravitee/setup_plugin-release-config:
filters:
branches:
ignore:
- /.*/
tags:
only:
- /^[0-9]+\.[0-9]+\.[0-9]+$/
setup_release:
when:
matches:
pattern: "/^[0-9]+\\.[0-9]+\\.[0-9]+(-(alpha|beta|rc)\\.[0-9]+)?$/"
value: << pipeline.git.tag >>
jobs:
- gravitee/setup_plugin-release-config:
filters:
branches:
ignore:
- /.*/
tags:
only:
- /^[0-9]+\.[0-9]+\.[0-9]+(-(alpha|beta|rc)\.[0-9]+)?$/
43 changes: 31 additions & 12 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,44 @@ image:https://img.shields.io/badge/semantic--release-conventional%20commits-e100
image:https://circleci.com/gh/gravitee-io/gravitee-policy-latency.svg?style=svg["CircleCI", link="https://circleci.com/gh/gravitee-io/gravitee-policy-latency"]
endif::[]

== Phase
== Compatibility with APIM

|===
|Plugin version | APIM version

| 2.x and upper | 4.x and upper
| 1.4.x | 3.10.x and upper
| Up to 1.3.x | Up to 3.9.x
|===

=== V3 engine

[cols="2*", options="header"]
[cols="4*", options="header"]
|===
^|onRequest
^|onResponse
^|onRequestContent
^|onResponseContent

^.^| X
^.^|
^.^|
^.^|
^.^|
|===

=== V4 engine

[cols="4*", options="header"]
|===
^|onRequest
^|onResponse
^|onMessageRequest
^|onMessageResponse

^.^| X
^.^|
^.^| X
^.^| X
|===

== Description
Expand All @@ -28,15 +56,6 @@ This policy is particularly useful in two scenarios:
* Testing: adding latency allows you to test client applications when APIs are slow to respond.
* Monetization: a longer latency can be added to free plans to encourage clients to move to a better (or paid) plan.

== Compatibility with APIM

|===
|Plugin version | APIM version

| 1.4.x and upper | 3.10.x and upper
| Up to 1.3.x | Up to 3.9.x
|===

== Configuration

You can configure the policy with the following options:
Expand Down
129 changes: 113 additions & 16 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,35 @@

<groupId>io.gravitee.policy</groupId>
<artifactId>gravitee-policy-latency</artifactId>
<version>1.4.1</version>
<version>2.0.0</version>
<packaging>jar</packaging>
<name>Gravitee.io APIM - Policy - Latency</name>
<description>Description of the Latency Gravitee Policy</description>

<parent>
<groupId>io.gravitee</groupId>
<artifactId>gravitee-parent</artifactId>
<version>20.2</version>
<version>21.0.1</version>
</parent>

<properties>
<gravitee-apim-gateway-tests-sdk.version>3.18.0-SNAPSHOT</gravitee-apim-gateway-tests-sdk.version>
<gravitee-bom.version>2.5</gravitee-bom.version>
<gravitee-gateway-api.version>1.32.2</gravitee-gateway-api.version>
<gravitee-bom.version>5.0.0</gravitee-bom.version>
<gravitee-gateway-api.version>3.0.0-alpha.6</gravitee-gateway-api.version>
<gravitee-node.version>3.1.0-alpha.10</gravitee-node.version>
<gravitee-policy-api.version>1.11.0</gravitee-policy-api.version>
<gravitee-common.version>2.1.1</gravitee-common.version>
<gravitee-apim.version>4.0.0-SNAPSHOT</gravitee-apim.version>
<gravitee-sse.version>3.1.0-alpha.3</gravitee-sse.version>
<gravitee-http-post.version>1.0.0-alpha.2</gravitee-http-post.version>
<gravitee-reactor-message.version>1.0.0-alpha.7</gravitee-reactor-message.version>

<json-schema-generator-maven-plugin.version>1.3.0</json-schema-generator-maven-plugin.version>
<json-schema-generator-maven-plugin.outputDirectory>${project.build.directory}/schemas</json-schema-generator-maven-plugin.outputDirectory>

<maven-assembly-plugin.version>3.3.0</maven-assembly-plugin.version>
<maven-plugin-assembly.version>3.6.0</maven-plugin-assembly.version>
<maven-plugin-prettier.version>0.19</maven-plugin-prettier.version>
<maven-plugin-prettier.prettierjava.version>1.6.2</maven-plugin-prettier.prettierjava.version>
<maven-plugin-properties.version>1.1.0</maven-plugin-properties.version>
<!-- Property used by the publication job in CI-->
<publish-folder-path>graviteeio-apim/plugins/policies</publish-folder-path>
</properties>
Expand All @@ -56,6 +65,16 @@
<scope>import</scope>
<type>pom</type>
</dependency>
<dependency>
<groupId>io.gravitee.policy</groupId>
<artifactId>gravitee-policy-api</artifactId>
<version>${gravitee-policy-api.version}</version>
</dependency>
<dependency>
<groupId>io.gravitee.gateway</groupId>
<artifactId>gravitee-gateway-api</artifactId>
<version>${gravitee-gateway-api.version}</version>
</dependency>
</dependencies>
</dependencyManagement>

Expand All @@ -81,15 +100,86 @@
<artifactId>vertx-core</artifactId>
<scope>provided</scope>
</dependency>

<!-- Logging -->
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-rx-java3</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<scope>provided</scope>
</dependency>

<!-- Lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>

<!-- Test scope -->
<dependency>
<groupId>io.gravitee.apim.gateway</groupId>
<artifactId>gravitee-apim-gateway-tests-sdk</artifactId>
<version>${gravitee-apim.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.gravitee.apim.plugin.entrypoint</groupId>
<artifactId>gravitee-apim-plugin-entrypoint-http-proxy</artifactId>
<version>${gravitee-apim.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.gravitee.apim.plugin.endpoint</groupId>
<artifactId>gravitee-apim-plugin-endpoint-http-proxy</artifactId>
<version>${gravitee-apim.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.graviteesource.reactor</groupId>
<artifactId>gravitee-reactor-message</artifactId>
<version>${gravitee-reactor-message.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.gravitee.apim.plugin.endpoint</groupId>
<artifactId>gravitee-apim-plugin-endpoint-mock</artifactId>
<version>${gravitee-apim.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.graviteesource.entrypoint</groupId>
<artifactId>gravitee-entrypoint-sse</artifactId>
<version>${gravitee-sse.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.graviteesource.entrypoint</groupId>
<artifactId>gravitee-entrypoint-http-post</artifactId>
<version>${gravitee-http-post.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
Expand All @@ -103,9 +193,14 @@
</dependency>

<dependency>
<groupId>io.gravitee.apim.gateway</groupId>
<artifactId>gravitee-apim-gateway-tests-sdk</artifactId>
<version>${gravitee-apim-gateway-tests-sdk.version}</version>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Expand All @@ -121,7 +216,7 @@
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0.0</version>
<version>${maven-plugin-properties.version}</version>
<executions>
<execution>
<phase>initialize</phase>
Expand Down Expand Up @@ -157,9 +252,11 @@
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>${maven-assembly-plugin.version}</version>
<version>${maven-plugin-assembly.version}</version>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<descriptors>
Expand All @@ -179,10 +276,10 @@
<plugin>
<groupId>com.hubspot.maven.plugins</groupId>
<artifactId>prettier-maven-plugin</artifactId>
<version>0.17</version>
<version>${maven-plugin-prettier.version}</version>
<configuration>
<nodeVersion>12.13.0</nodeVersion>
<prettierJavaVersion>1.6.1</prettierJavaVersion>
<prettierJavaVersion>${maven-plugin-prettier.prettierjava.version}</prettierJavaVersion>
<skip>${skip.validation}</skip>
</configuration>
<executions>
<execution>
Expand Down
51 changes: 27 additions & 24 deletions src/main/java/io/gravitee/policy/latency/LatencyPolicy.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,38 +15,41 @@
*/
package io.gravitee.policy.latency;

import io.gravitee.gateway.api.ExecutionContext;
import io.gravitee.gateway.api.Request;
import io.gravitee.gateway.api.Response;
import io.gravitee.policy.api.PolicyChain;
import io.gravitee.policy.api.annotations.OnRequest;
import io.gravitee.gateway.reactive.api.context.HttpExecutionContext;
import io.gravitee.gateway.reactive.api.context.MessageExecutionContext;
import io.gravitee.gateway.reactive.api.policy.Policy;
import io.gravitee.policy.latency.configuration.LatencyPolicyConfiguration;
import io.vertx.core.Vertx;
import io.gravitee.policy.latency.v3.LatencyPolicyV3;
import io.reactivex.rxjava3.core.Completable;
import io.reactivex.rxjava3.core.Maybe;

/**
* @author Azize ELAMRANI (azize.elamrani at graviteesource.com)
* @author Guillaume Lamirand (guillaume.lamirand at graviteesource.com)
* @author GraviteeSource Team
*/
public class LatencyPolicy {

private final LatencyPolicyConfiguration latencyPolicyConfiguration;
public class LatencyPolicy extends LatencyPolicyV3 implements Policy {

public LatencyPolicy(final LatencyPolicyConfiguration latencyPolicyConfiguration) {
this.latencyPolicyConfiguration = latencyPolicyConfiguration;
super(latencyPolicyConfiguration);
}

@Override
public String id() {
return "latency";
}

@Override
public Completable onRequest(final HttpExecutionContext ctx) {
return Completable.complete().delay(configuration.getTime(), configuration.getTimeUnit());
}

@Override
public Completable onMessageRequest(final MessageExecutionContext ctx) {
return ctx.request().onMessage(message -> Maybe.just(message).delay(configuration.getTime(), configuration.getTimeUnit()));
}

@OnRequest
public void onRequest(
final Request request,
final Response response,
final ExecutionContext executionContext,
final PolicyChain policyChain
) {
executionContext
.getComponent(Vertx.class)
.setTimer(
latencyPolicyConfiguration.getTimeUnit().toMillis(latencyPolicyConfiguration.getTime()),
timerId -> policyChain.doNext(request, response)
);
@Override
public Completable onMessageResponse(final MessageExecutionContext ctx) {
return ctx.response().onMessage(message -> Maybe.just(message).delay(configuration.getTime(), configuration.getTimeUnit()));
}
}
Loading

0 comments on commit 8d25c60

Please sign in to comment.