Skip to content

Commit 00dd363

Browse files
committed
add properties for VIAF to set thread pool size and rate limit
1 parent 8f95521 commit 00dd363

File tree

3 files changed

+59
-1
lines changed

3 files changed

+59
-1
lines changed

conciliator.properties

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,10 @@ cache.size=64MB
5252
# datasource.anothersolr.nametype.name=Person
5353
# datasource.anothersolr.url.query=http://SOME_OTHER_SOLR_INSTANCE:8983/solr/SOME_COLLECTION/select?wt=xml&q={{QUERY}}&rows={{ROWS}}
5454
# etc.
55+
56+
#### VIAF
57+
58+
## override default threadpool size for VIAF
59+
#datasource.viaf.threadpool.size=1
60+
## delay after every request, in milliseconds, to self-rate limit
61+
#datasource.viaf.delay=100

src/main/java/com/codefork/refine/datasource/WebServiceSearchTask.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,17 @@ public class WebServiceSearchTask implements SearchTask {
1919
private WebServiceDataSource dataSource;
2020
private String key;
2121
private SearchQuery searchQuery;
22+
private int delay;
2223

23-
public WebServiceSearchTask(WebServiceDataSource dataSource, String key, SearchQuery searchQuery) {
24+
public WebServiceSearchTask(WebServiceDataSource dataSource, String key, SearchQuery searchQuery, int delay) {
2425
this.key = key;
2526
this.searchQuery = searchQuery;
2627
this.dataSource = dataSource;
28+
this.delay = delay;
29+
}
30+
31+
public WebServiceSearchTask(WebServiceDataSource dataSource, String key, SearchQuery searchQuery) {
32+
this(dataSource, key, searchQuery, 0);
2733
}
2834

2935
@Override
@@ -51,6 +57,17 @@ public SearchResult call() {
5157
return new SearchResult(key, SearchResult.ErrorType.UNKNOWN);
5258
}
5359
results.sort(BY_SCORE_REVERSED);
60+
61+
if(delay > 0) {
62+
// TODO: it would be nice to sleep only we if actually made a request (if the result wasn't cached)
63+
// but we'd need to make some modifications in order to know that
64+
try {
65+
Thread.sleep(delay);
66+
} catch (InterruptedException e) {
67+
dataSource.getLog().error("sleep interrupted in WebServiceSearchTask");
68+
}
69+
}
70+
5471
return new SearchResult(key, results);
5572
}
5673

src/main/java/com/codefork/refine/viaf/VIAF.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import com.codefork.refine.ThreadPool;
77
import com.codefork.refine.ThreadPoolFactory;
88
import com.codefork.refine.datasource.ConnectionFactory;
9+
import com.codefork.refine.datasource.SearchTask;
10+
import com.codefork.refine.datasource.WebServiceSearchTask;
911
import com.codefork.refine.datasource.stats.Stats;
1012
import com.codefork.refine.datasource.WebServiceDataSource;
1113
import com.codefork.refine.resources.NameType;
@@ -47,15 +49,39 @@ public class VIAF extends WebServiceDataSource {
4749
private VIAFSource viafSource = null;
4850
private Map<String, NonVIAFSource> nonViafSources = new HashMap<>();
4951

52+
private int delay = 0;
53+
5054
@Autowired
5155
public VIAF(Config config, CacheManager cacheManager, ThreadPoolFactory threadPoolFactory, ConnectionFactory connectionFactory, Stats stats) {
5256
super(config, cacheManager, threadPoolFactory, connectionFactory, stats);
5357

5458
setCacheEnabled(true);
5559

60+
var dataSourceProperties = config.getDataSourceProperties("viaf");
61+
62+
var threadPoolSize = dataSourceProperties.getProperty("threadpool.size");
63+
if(threadPoolSize != null) {
64+
getLog().info("Setting pool size for VIAF to " + threadPoolSize);
65+
getThreadPool().setPoolSize(Integer.parseInt(threadPoolSize.strip()));
66+
}
67+
68+
var delay = dataSourceProperties.getProperty("delay");
69+
if(delay != null) {
70+
getLog().info("Setting delay to " + delay);
71+
setDelay(Integer.parseInt(delay.strip()));
72+
}
73+
5674
spf = SAXParserFactory.newInstance();
5775
}
5876

77+
public int getDelay() {
78+
return delay;
79+
}
80+
81+
public void setDelay(int delay) {
82+
this.delay = delay;
83+
}
84+
5985
/**
6086
* Factory method for getting a NonVIAFSource object
6187
*/
@@ -115,6 +141,14 @@ public static String createCqlQueryString(SearchQuery searchQuery) {
115141
return cql;
116142
}
117143

144+
/**
145+
* Override so we can add an optional delay to the task
146+
**/
147+
@Override
148+
public SearchTask createSearchTask(String key, SearchQuery searchQuery) {
149+
return new WebServiceSearchTask(this, key, searchQuery, getDelay());
150+
}
151+
118152
/**
119153
* Does actual work of performing a search and parsing the XML.
120154
* @param query

0 commit comments

Comments
 (0)