-
Notifications
You must be signed in to change notification settings - Fork 215
/
DefaultGatewaySignalEnrichmentProvider.java
67 lines (58 loc) · 2.85 KB
/
DefaultGatewaySignalEnrichmentProvider.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
/*
* Copyright (c) 2019 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.utils;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.Executor;
import org.apache.pekko.actor.ActorSystem;
import org.apache.pekko.http.javadsl.model.HttpRequest;
import org.eclipse.ditto.edge.service.dispatching.EdgeCommandForwarderActor;
import org.eclipse.ditto.internal.models.signalenrichment.ByRoundTripSignalEnrichmentFacade;
import org.eclipse.ditto.internal.models.signalenrichment.DefaultSignalEnrichmentProviderConfig;
import org.eclipse.ditto.internal.models.signalenrichment.DittoCachingSignalEnrichmentFacade;
import org.eclipse.ditto.internal.models.signalenrichment.SignalEnrichmentFacade;
import com.typesafe.config.Config;
/**
* Provider for gateway-service of signal-enriching facades that uses an async Caffeine cache in order to load
* extra data to enrich.
*/
public final class DefaultGatewaySignalEnrichmentProvider implements GatewaySignalEnrichmentProvider {
private static final String COMMAND_FORWARDER = "/user/gatewayRoot/" + EdgeCommandForwarderActor.ACTOR_NAME;
private static final String CACHE_LOADER_DISPATCHER = "signal-enrichment-cache-dispatcher";
private final SignalEnrichmentFacade facade;
/**
* Instantiate this provider. Called by reflection.
*
* @param actorSystem The actor system for which this provider is instantiated.
* @param config the config the extension is configured.
*/
public DefaultGatewaySignalEnrichmentProvider(final ActorSystem actorSystem, final Config config) {
final var commandHandler = actorSystem.actorSelection(COMMAND_FORWARDER);
final var providerConfig = DefaultSignalEnrichmentProviderConfig.of(config);
final var delegate = ByRoundTripSignalEnrichmentFacade.of(commandHandler, providerConfig.getAskTimeout());
if (providerConfig.isCachingEnabled()) {
final Executor cacheLoaderExecutor = actorSystem.dispatchers().lookup(CACHE_LOADER_DISPATCHER);
facade = DittoCachingSignalEnrichmentFacade.newInstance(
delegate,
providerConfig.getCacheConfig(),
cacheLoaderExecutor,
"gateway");
} else {
facade = delegate;
}
}
@Override
public CompletionStage<SignalEnrichmentFacade> getFacade(final HttpRequest request) {
return CompletableFuture.completedFuture(facade);
}
}