Skip to content

Commit 6d7ec09

Browse files
authored
Merge pull request #139 from networknt/issue138
fixes #138 refactor the executor for sync call handlers
2 parents 9326a0d + ae2a232 commit 6d7ec09

File tree

16 files changed

+100
-366
lines changed

16 files changed

+100
-366
lines changed

src/main/java/com/networknt/aws/lambda/LightLambdaExchange.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;
66
import com.networknt.aws.lambda.exception.LambdaExchangeStateException;
77
import com.networknt.aws.lambda.handler.MiddlewareHandler;
8-
import com.networknt.aws.lambda.handler.chain.PooledChainLinkExecutor;
8+
import com.networknt.aws.lambda.handler.chain.ChainExecutor;
99
import com.networknt.aws.lambda.handler.chain.Chain;
1010
import com.networknt.aws.lambda.handler.middleware.ExceptionUtil;
1111
import com.networknt.aws.lambda.listener.LambdaExchangeFailureListener;
@@ -15,8 +15,6 @@
1515
import org.slf4j.Logger;
1616
import org.slf4j.LoggerFactory;
1717

18-
import java.io.InputStream;
19-
import java.io.OutputStream;
2018
import java.util.*;
2119

2220

@@ -29,7 +27,7 @@ public final class LightLambdaExchange {
2927
private APIGatewayProxyResponseEvent response;
3028
private final Context context;
3129
private final Map<Attachable<?>, Object> attachments = Collections.synchronizedMap(new HashMap<>());
32-
private final PooledChainLinkExecutor executor;
30+
private final ChainExecutor executor;
3331
private final List<LambdaResponseCompleteListener> responseCompleteListeners = Collections.synchronizedList(new ArrayList<>());
3432
private final List<LambdaRequestCompleteListener> requestCompleteListeners = Collections.synchronizedList(new ArrayList<>());
3533
private final List<LambdaExchangeFailureListener> exchangeFailedListeners = Collections.synchronizedList(new ArrayList<>());
@@ -67,7 +65,7 @@ public final class LightLambdaExchange {
6765
public LightLambdaExchange(Context context, Chain chain) {
6866
this.context = context;
6967
this.chain = chain;
70-
this.executor = new PooledChainLinkExecutor();
68+
this.executor = new ChainExecutor();
7169
}
7270

7371
public void executeChain() {

src/main/java/com/networknt/aws/lambda/handler/Handler.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,15 @@ static void initChains() {
6161
// add the chains to the handler list by id list.
6262
for (var chainName : config.getChains().keySet()) {
6363
var chain = config.getChains().get(chainName);
64-
Chain handlerChain = new Chain(false);
64+
Chain handlerChain = new Chain();
6565
for (var chainItemName : chain) {
6666
var chainItem = handlers.get(chainItemName);
6767
if (chainItem == null) {
6868
throw new RuntimeException("Unknown handler in chain: " + chainItemName);
6969
}
7070
handlerChain.addChainable(chainItem);
7171
}
72+
handlerChain.setFinalized(true);
7273
handlerListById.put(chainName, handlerChain);
7374
}
7475
}
@@ -162,7 +163,7 @@ private static void addPathChain(PathChain pathChain) {
162163
* @return A list containing references to the instantiated handlers
163164
*/
164165
private static Chain getHandlersFromExecList(List<String> execs) {
165-
var handlersFromExecList = new Chain(false);
166+
var handlersFromExecList = new Chain();
166167

167168
if (execs != null) {
168169

@@ -185,7 +186,7 @@ private static Chain getHandlersFromExecList(List<String> execs) {
185186
}
186187
}
187188
}
188-
handlersFromExecList.setupGroupedChain();
189+
handlersFromExecList.setFinalized(true);
189190
return handlersFromExecList;
190191
}
191192

src/main/java/com/networknt/aws/lambda/handler/chain/Chain.java

Lines changed: 3 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,14 @@
1212
public class Chain {
1313
private static final Logger LOG = LoggerFactory.getLogger(Chain.class);
1414
private final LinkedList<LambdaHandler> chain = new LinkedList<>();
15-
private final LinkedList<ArrayList<LambdaHandler>> groupedChain = new LinkedList<>();
16-
17-
private final boolean forceSynchronousExecution;
1815

1916
private static final String CONFIG_NAME = "pooled-chain-executor";
2017
private static final PooledChainConfig CONFIG = (PooledChainConfig) Config.getInstance().getJsonObjectConfig(CONFIG_NAME, PooledChainConfig.class);
2118

2219
private boolean isFinalized;
2320

24-
public Chain(boolean forceSynchronousExecution) {
21+
public Chain() {
2522
this.isFinalized = false;
26-
this.forceSynchronousExecution = forceSynchronousExecution;
2723
}
2824

2925
public void addChainable(LambdaHandler chainable) {
@@ -38,43 +34,14 @@ public boolean isFinalized() {
3834
return isFinalized;
3935
}
4036

41-
public LinkedList<ArrayList<LambdaHandler>> getGroupedChain() {
42-
return groupedChain;
37+
public void setFinalized(boolean finalized) {
38+
this.isFinalized = finalized;
4339
}
4440

4541
public int getChainSize() {
4642
return this.chain.size();
4743
}
4844

49-
public void setupGroupedChain() {
50-
51-
if (this.isFinalized)
52-
return;
53-
54-
var group = new ArrayList<LambdaHandler>();
55-
for (var chainable : this.chain) {
56-
57-
if (this.forceSynchronousExecution || (!chainable.isAsynchronous() && group.isEmpty())) {
58-
this.cutGroup(group, chainable);
59-
group = new ArrayList<>();
60-
61-
} else if (chainable.isAsynchronous()) {
62-
group.add(chainable);
63-
64-
} else if (!chainable.isAsynchronous() && !group.isEmpty()) {
65-
this.groupedChain.add(group);
66-
group = new ArrayList<>();
67-
this.cutGroup(group, chainable);
68-
group = new ArrayList<>();
69-
}
70-
}
71-
72-
if (!group.isEmpty()) {
73-
this.groupedChain.add(group);
74-
}
75-
76-
this.isFinalized = true;
77-
}
7845

7946
/**
8047
* Add to chain from string parameter
@@ -135,11 +102,6 @@ public Chain add(Class<? extends LambdaHandler> middleware) {
135102
return this;
136103
}
137104

138-
private void cutGroup(ArrayList<LambdaHandler> group, LambdaHandler chainable) {
139-
group.add(chainable);
140-
this.groupedChain.add(group);
141-
}
142-
143105
public LinkedList<LambdaHandler> getChain() {
144106
return chain;
145107
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.networknt.aws.lambda.handler.chain;
2+
3+
import com.networknt.aws.lambda.LightLambdaExchange;
4+
import com.networknt.status.Status;
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
7+
8+
import java.util.LinkedList;
9+
import java.util.List;
10+
11+
public class ChainExecutor {
12+
private static final Logger LOG = LoggerFactory.getLogger(ChainExecutor.class);
13+
private final LinkedList<Status> chainResults = new LinkedList<>();
14+
private static final String MIDDLEWARE_UNHANDLED_EXCEPTION = "ERR14004";
15+
16+
public void executeChain(final LightLambdaExchange exchange, final Chain chain) {
17+
18+
if (!chain.isFinalized()) {
19+
LOG.error("Execution attempt on a chain that is not finalized! Call 'finalizeChain' before 'executeChain'");
20+
return;
21+
}
22+
23+
for(var handler : chain.getChain()) {
24+
if (!handler.isEnabled()) {
25+
LOG.debug("Skipping disabled handler: {}", handler.getClass().getName());
26+
continue;
27+
}
28+
Status status = null;
29+
try {
30+
status = handler.execute(exchange);
31+
exchange.updateExchangeStatus(status);
32+
addChainableResult(status);
33+
} catch (Exception e) {
34+
LOG.error("Exception in handler: {}", handler.getClass().getName(), e);
35+
addChainableResult(new Status(MIDDLEWARE_UNHANDLED_EXCEPTION));
36+
}
37+
if (exchange.hasFailedState()) {
38+
break;
39+
}
40+
}
41+
}
42+
43+
protected void addChainableResult(Status result) {
44+
this.chainResults.add(result);
45+
}
46+
47+
public List<Status> getChainResults() {
48+
return chainResults;
49+
}
50+
51+
}

src/main/java/com/networknt/aws/lambda/handler/chain/ChainLinkCallback.java

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/main/java/com/networknt/aws/lambda/handler/chain/ChainLinkWorker.java

Lines changed: 0 additions & 30 deletions
This file was deleted.

0 commit comments

Comments
 (0)