-
Notifications
You must be signed in to change notification settings - Fork 214
/
CachingHealthRoute.java
executable file
·115 lines (99 loc) · 4.33 KB
/
CachingHealthRoute.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
* Copyright (c) 2017 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.gateway.service.endpoints.routes.health;
import static org.apache.pekko.http.javadsl.server.Directives.completeWithFuture;
import static org.apache.pekko.http.javadsl.server.Directives.get;
import static org.apache.pekko.http.javadsl.server.Directives.path;
import java.time.Duration;
import java.time.Instant;
import java.util.concurrent.CompletionStage;
import org.eclipse.ditto.gateway.service.health.StatusAndHealthProvider;
import org.eclipse.ditto.gateway.service.util.config.endpoints.PublicHealthConfig;
import org.eclipse.ditto.json.JsonFactory;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.internal.utils.health.StatusDetailMessage;
import org.eclipse.ditto.internal.utils.health.StatusInfo;
import org.apache.pekko.http.javadsl.model.ContentTypes;
import org.apache.pekko.http.javadsl.model.HttpResponse;
import org.apache.pekko.http.javadsl.model.StatusCodes;
import org.apache.pekko.http.javadsl.server.Route;
/**
* Builder for creating Pekko HTTP route for {@code /health}.
*/
public final class CachingHealthRoute {
/**
* Public endpoint of health.
*/
public static final String PATH_HEALTH = "health";
private final StatusAndHealthProvider statusHealthHelper;
private final Duration refreshInterval;
private CompletionStage<StatusInfo> cachedHealth;
private Instant lastCheckInstant;
/**
* Constructs the {@code /health} route builder.
*
* @param statusHealthHelper the helper for retrieving health status of the cluster.
* @param refreshInterval the interval for refreshing the health status.
*/
public CachingHealthRoute(final StatusAndHealthProvider statusHealthHelper, final Duration refreshInterval) {
this.statusHealthHelper = statusHealthHelper;
this.refreshInterval = refreshInterval;
}
/**
* Constructs a new {@code CachingHealthRoute} object.
*
* @param statusAndHealthProvider is used to retrieve the health status of the cluster.
* @param publicHealthConfig the configuration settings of the public health endpoint.
*/
public CachingHealthRoute(final StatusAndHealthProvider statusAndHealthProvider,
final PublicHealthConfig publicHealthConfig) {
statusHealthHelper = statusAndHealthProvider;
refreshInterval = publicHealthConfig.getCacheTimeout();
}
/**
* Builds the {@code /health} route.
*
* @return the {@code /health} route.
*/
public Route buildHealthRoute() {
return path(PATH_HEALTH, () -> // /health
get(() -> // GET
completeWithFuture(createOverallHealthResponse())
)
);
}
private boolean isCacheTimedOut() {
return lastCheckInstant.plusSeconds(refreshInterval.getSeconds()).isBefore(Instant.now());
}
private CompletionStage<HttpResponse> createOverallHealthResponse() {
if (cachedHealth == null || isCacheTimedOut()) {
cachedHealth = statusHealthHelper.retrieveHealth()
.exceptionally(t -> StatusInfo.fromDetail(StatusDetailMessage.of(StatusDetailMessage.Level.ERROR,
"Exception: " + t.getMessage())));
lastCheckInstant = Instant.now();
}
return cachedHealth.thenApply(overallHealth -> {
final JsonObject statusObject = JsonFactory.newObject()
.set(StatusInfo.JSON_KEY_STATUS, overallHealth.getStatus().toString());
if (overallHealth.isHealthy()) {
return HttpResponse.create()
.withStatus(StatusCodes.OK)
.withEntity(ContentTypes.APPLICATION_JSON, statusObject.toString());
} else {
return HttpResponse.create()
.withStatus(StatusCodes.SERVICE_UNAVAILABLE)
.withEntity(ContentTypes.APPLICATION_JSON, statusObject.toString());
}
});
}
}