Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wrap SpringLBClientFactory in a CachingLBClientFactory so LBClients a…
…re reused. fixes gh-240
- Loading branch information
1 parent
4bd7c3b
commit 384fad6
Showing
3 changed files
with
132 additions
and
3 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
.../src/main/java/org/springframework/cloud/netflix/feign/ribbon/CachingLBClientFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright 2013-2015 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.cloud.netflix.feign.ribbon; | ||
|
||
import feign.ribbon.LBClient; | ||
import feign.ribbon.LBClientFactory; | ||
import org.springframework.util.ConcurrentReferenceHashMap; | ||
|
||
import java.util.Map; | ||
|
||
/** | ||
* LBClientFactory that caches entries created. | ||
* @author Spencer Gibb | ||
*/ | ||
public class CachingLBClientFactory implements LBClientFactory { | ||
|
||
private volatile Map<String, LBClient> cache = new ConcurrentReferenceHashMap<>(); | ||
private final LBClientFactory delegate; | ||
|
||
public CachingLBClientFactory(LBClientFactory delegate) { | ||
this.delegate = delegate; | ||
} | ||
|
||
@Override | ||
public LBClient create(String clientName) { | ||
if (cache.containsKey(clientName)) { | ||
return cache.get(clientName); | ||
} | ||
LBClient client = delegate.create(clientName); | ||
cache.put(clientName, client); | ||
return client; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
...test/java/org/springframework/cloud/netflix/feign/ribbon/CachingLBClientFactoryTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* Copyright 2013-2015 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.cloud.netflix.feign.ribbon; | ||
|
||
import static org.junit.Assert.*; | ||
import static org.mockito.Mockito.*; | ||
|
||
import com.netflix.client.config.CommonClientConfigKey; | ||
import com.netflix.client.config.DefaultClientConfigImpl; | ||
import com.netflix.client.config.IClientConfig; | ||
import feign.ribbon.LBClient; | ||
import feign.ribbon.LBClientFactory; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.mockito.Mock; | ||
import org.mockito.MockitoAnnotations; | ||
|
||
/** | ||
* @author Spencer Gibb | ||
*/ | ||
public class CachingLBClientFactoryTests { | ||
|
||
@Mock | ||
private LBClientFactory delegate; | ||
|
||
private CachingLBClientFactory factory; | ||
|
||
@Before | ||
public void init() { | ||
MockitoAnnotations.initMocks(this); | ||
|
||
IClientConfig config = new DefaultClientConfigImpl(); | ||
config.set(CommonClientConfigKey.ConnectTimeout, 1000); | ||
config.set(CommonClientConfigKey.ReadTimeout, 500); | ||
|
||
LBClient client1 = LBClient.create(null, config); | ||
LBClient client2 = LBClient.create(null, config); | ||
|
||
when(delegate.create("client1")).thenReturn(client1); | ||
when(delegate.create("client2")).thenReturn(client2); | ||
|
||
factory = new CachingLBClientFactory(delegate); | ||
} | ||
|
||
@Test | ||
public void delegateCreatesWhenMissing() { | ||
LBClient client = factory.create("client1"); | ||
assertNotNull("client was null", client); | ||
|
||
verify(delegate, times(1)).create("client1"); | ||
} | ||
|
||
@Test | ||
public void cacheWorks() { | ||
LBClient client = factory.create("client2"); | ||
assertNotNull("client was null", client); | ||
|
||
client = factory.create("client2"); | ||
assertNotNull("client was null", client); | ||
|
||
verify(delegate, times(1)).create("client2"); | ||
} | ||
|
||
} |