Skip to content

Commit

Permalink
Merge pull request #2092 from thisaltennakoon/master
Browse files Browse the repository at this point in the history
Fix concurrency issues while deploying APIs during high traffic
  • Loading branch information
DinithHerath authored Aug 31, 2023
2 parents 9975c5e + 83265a8 commit 1994f78
Showing 1 changed file with 60 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@ public class SynapseConfiguration implements ManagedLifecycle, SynapseArtifact {
*/
private Map<String, MessageProcessor> messageProcessors = new ConcurrentHashMap<String, MessageProcessor>();

private Map<String, Object> locks = new ConcurrentHashMap<String, Object>();

/**
* Endpoint templates to create actual endpoints
*/
Expand Down Expand Up @@ -1147,6 +1149,20 @@ public Map<String, Endpoint> getDefinedEndpoints() {
return definedEndpoints;
}

public Object acquireLock(String key) {
Object lock = locks.get(key);
if (lock == null) {
synchronized (this) {
lock = locks.get(key);
if (lock == null) {
lock = new Object();
locks.put(key, lock);
}
}
}
return lock;
}

/**
* Get the definition of the endpoint with the given key
*
Expand All @@ -1155,58 +1171,60 @@ public Map<String, Endpoint> getDefinedEndpoints() {
*/
public Endpoint getEndpoint(String key) {

Object o = getEntry(key);
if (o != null && o instanceof Endpoint) {
return (Endpoint) o;
}
synchronized (acquireLock(key)) {
Object o = getEntry(key);
if (o != null && o instanceof Endpoint) {
return (Endpoint) o;
}

Entry entry = null;
if (o == null) {
entry = new Entry(key);
entry.setType(Entry.REMOTE_ENTRY);
} else {
Object object = localRegistry.get(key);
if (object instanceof Entry) {
entry = (Entry) object;
Entry entry = null;
if (o == null) {
entry = new Entry(key);
entry.setType(Entry.REMOTE_ENTRY);
} else {
Object object = localRegistry.get(key);
if (object instanceof Entry) {
entry = (Entry) object;
}
}
}

assertEntryNull(entry, key);
assertEntryNull(entry, key);

//noinspection ConstantConditions
if (entry.getMapper() == null) {
entry.setMapper(XMLToEndpointMapper.getInstance());
}
//noinspection ConstantConditions
if (entry.getMapper() == null) {
entry.setMapper(XMLToEndpointMapper.getInstance());
}

if (entry.getType() == Entry.REMOTE_ENTRY) {
if (registry != null) {
o = registry.getResource(entry, getProperties());
if (o != null && o instanceof Endpoint) {
localRegistry.put(key, entry);
return (Endpoint) o;
} else if (o instanceof OMNode) {
properties.put(SynapseConstants.SYNAPSE_CONFIGURATION, this);
Endpoint e = (Endpoint) XMLToEndpointMapper.getInstance().
getObjectFromOMNode((OMNode) o, properties);
if (e != null) {
entry.setValue(e);
return e;
if (entry.getType() == Entry.REMOTE_ENTRY) {
if (registry != null) {
o = registry.getResource(entry, getProperties());
if (o != null && o instanceof Endpoint) {
localRegistry.put(key, entry);
return (Endpoint) o;
} else if (o instanceof OMNode) {
properties.put(SynapseConstants.SYNAPSE_CONFIGURATION, this);
Endpoint e = (Endpoint) XMLToEndpointMapper.getInstance().
getObjectFromOMNode((OMNode) o, properties);
if (e != null) {
entry.setValue(e);
return e;
}
}
}
}
} else {
Object value = entry.getValue();
if (value instanceof OMNode) {
properties.put(SynapseConstants.SYNAPSE_CONFIGURATION, this);
Object object = entry.getMapper().getObjectFromOMNode(
(OMNode) value, getProperties());
if (object instanceof Endpoint) {
entry.setValue(object);
return (Endpoint) object;
} else {
Object value = entry.getValue();
if (value instanceof OMNode) {
properties.put(SynapseConstants.SYNAPSE_CONFIGURATION, this);
Object object = entry.getMapper().getObjectFromOMNode(
(OMNode) value, getProperties());
if (object instanceof Endpoint) {
entry.setValue(object);
return (Endpoint) object;
}
}
}
return null;
}
return null;
}

/**
Expand Down

0 comments on commit 1994f78

Please sign in to comment.