From 361b0f4d8004b781373180afbea94ffb2f38bc4b Mon Sep 17 00:00:00 2001 From: elroy Date: Wed, 12 Jul 2017 22:23:05 +0200 Subject: [PATCH] WIP flexible double buy protection --- .../komtek/gpi/application/Application.java | 24 ++++++++++++++----- .../controllers/GunbotProxyController.java | 4 +++- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/main/java/nl/komtek/gpi/application/Application.java b/src/main/java/nl/komtek/gpi/application/Application.java index 830dee3..769654c 100644 --- a/src/main/java/nl/komtek/gpi/application/Application.java +++ b/src/main/java/nl/komtek/gpi/application/Application.java @@ -7,9 +7,7 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory; import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory; -import org.springframework.cache.CacheManager; import org.springframework.cache.annotation.EnableCaching; -import org.springframework.cache.ehcache.EhCacheCacheManager; import org.springframework.cache.ehcache.EhCacheManagerFactoryBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; @@ -36,29 +34,43 @@ public class Application { private int additionalPort; @Value("${server.address:0.0.0.0}") private String serverAddress; + @Value("${doubleBuyProtectionSeconds:0}") + private int doubleBuyProtectionSeconds; + @Value("${doubleBuyProtection:false}") + private boolean doubleBuyProtection; public static void main(String[] args) { SpringApplication.run(Application.class, args); } @Bean - public CacheManager cacheManager() { + public net.sf.ehcache.management.CacheManager cacheManager() { net.sf.ehcache.CacheManager cacheManager = ehCacheCacheManager().getObject(); Cache tickerCache = new Cache("ticker", 500, false, false, 0, 0); Cache tradeHistoryCache = new Cache("tradeHistory", 500, false, false, 0, 0); Cache openOrdersCache = new Cache("openOrders", 500, false, false, 0, 0); Cache completeBalancesCache = new Cache("completeBalances", 5, false, false, 0, 0); Cache chartDataCache = new Cache("chartData", 500, false, false, 30, 30); - Cache buyOrderProtectionCache = new Cache("buyOrderProtection", 100, false, false, 30, 30); Cache balancesCache = new Cache("balances", 5, false, false, 0, 0); + + cacheManager.addCache(tickerCache); cacheManager.addCache(tradeHistoryCache); cacheManager.addCache(openOrdersCache); cacheManager.addCache(completeBalancesCache); cacheManager.addCache(chartDataCache); - cacheManager.addCache(buyOrderProtectionCache); cacheManager.addCache(balancesCache); - return new EhCacheCacheManager(cacheManager); + + if (doubleBuyProtectionSeconds > 0) { + Cache buyOrderProtectionCache = new Cache("buyOrderProtection", 100, false, false, doubleBuyProtectionSeconds, doubleBuyProtectionSeconds); + cacheManager.addCache(buyOrderProtectionCache); + } else if (doubleBuyProtection) { + Cache buyOrderProtectionCache = new Cache("buyOrderProtection", 100, false, false, 30, 30); + cacheManager.addCache(buyOrderProtectionCache); + } + + return new net.sf.ehcache.management.CacheManager(cacheManager); + } @Bean diff --git a/src/main/java/nl/komtek/gpi/controllers/GunbotProxyController.java b/src/main/java/nl/komtek/gpi/controllers/GunbotProxyController.java index 3ed2abc..e2a0390 100644 --- a/src/main/java/nl/komtek/gpi/controllers/GunbotProxyController.java +++ b/src/main/java/nl/komtek/gpi/controllers/GunbotProxyController.java @@ -33,6 +33,8 @@ public class GunbotProxyController { private GunbotProxyService gunbotProxyService; @Value("${doubleBuyProtection:false}") private boolean doubleBuyProtection; + @Value("${doubleBuyProtectionSeconds:0}") + private int doubleBuyProtectionSeconds; private Logger logger = LogManager.getLogger(GunbotProxyController.class); private PoloniexDataMapper mapper = new PoloniexDataMapper(); @@ -141,7 +143,7 @@ public class GunbotProxyController { @RequestParam BigDecimal amount) { String key = request.getHeader("key"); - if (doubleBuyProtection) { + if (doubleBuyProtection || doubleBuyProtectionSeconds > 0) { return gunbotProxyService.buyOrderWithProtection(key, currencyPair, rate, amount); } else { return gunbotProxyService.buyOrder(key, currencyPair, rate, amount);