From b3dbc9f668adc392e4ed4bf908f7f8d8528ad983 Mon Sep 17 00:00:00 2001 From: ehlxr Date: Fri, 16 Jul 2021 10:48:40 +0800 Subject: [PATCH] update at 2021-07-16 10:48:40 by ehlxr --- pom.xml | 26 ++++++------- .../ehlxr/{redis => }/SerializeUtil.java | 2 +- src/main/java/io/github/ehlxr/dfd.java | 8 ++-- src/main/java/io/github/ehlxr/http/Main.java | 6 --- .../io/github/ehlxr/rate/SlideWindow.java | 27 ++++++-------- .../java/io/github/ehlxr/redis/JedisUtil.java | 5 ++- src/main/java/io/github/ehlxr/test/Main.java | 37 ------------------- 7 files changed, 31 insertions(+), 80 deletions(-) rename src/main/java/io/github/ehlxr/{redis => }/SerializeUtil.java (98%) delete mode 100644 src/main/java/io/github/ehlxr/test/Main.java diff --git a/pom.xml b/pom.xml index 69c9a07..4acacd1 100644 --- a/pom.xml +++ b/pom.xml @@ -5,18 +5,18 @@ budd war 0.0.1-SNAPSHOT - - - - org.apache.maven.plugins - maven-compiler-plugin - - 11 - 11 - - - - + + + + + + + + + + + + budd http://maven.apache.org @@ -173,7 +173,7 @@ redis.clients jedis - 3.4.0 + 3.4.1 diff --git a/src/main/java/io/github/ehlxr/redis/SerializeUtil.java b/src/main/java/io/github/ehlxr/SerializeUtil.java similarity index 98% rename from src/main/java/io/github/ehlxr/redis/SerializeUtil.java rename to src/main/java/io/github/ehlxr/SerializeUtil.java index 67265ec..769fa4a 100644 --- a/src/main/java/io/github/ehlxr/redis/SerializeUtil.java +++ b/src/main/java/io/github/ehlxr/SerializeUtil.java @@ -22,7 +22,7 @@ * THE SOFTWARE. */ -package io.github.ehlxr.redis; +package io.github.ehlxr; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; diff --git a/src/main/java/io/github/ehlxr/dfd.java b/src/main/java/io/github/ehlxr/dfd.java index 932ff37..134a594 100644 --- a/src/main/java/io/github/ehlxr/dfd.java +++ b/src/main/java/io/github/ehlxr/dfd.java @@ -24,17 +24,15 @@ package io.github.ehlxr; -import com.google.common.collect.Maps; - /** * Created by ehlxr on 2016/12/23. */ public class dfd { public static void main(String[] args) { - var map = Maps.newHashMap(); - map.put("d", 1); - System.out.println(map); + // var map = Maps.newHashMap(); + // map.put("d", 1); + // System.out.println(map); } public void printCircle(int[][] matrix, int startX, int startY, int endX, int endY) { diff --git a/src/main/java/io/github/ehlxr/http/Main.java b/src/main/java/io/github/ehlxr/http/Main.java index cd259df..571e709 100644 --- a/src/main/java/io/github/ehlxr/http/Main.java +++ b/src/main/java/io/github/ehlxr/http/Main.java @@ -24,18 +24,12 @@ package io.github.ehlxr.http; -import java.io.FileNotFoundException; import java.io.IOException; -import java.net.Authenticator; -import java.net.InetSocketAddress; -import java.net.ProxySelector; import java.net.URI; import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; -import java.nio.file.Paths; import java.time.Duration; -import java.util.concurrent.CompletableFuture; /** * @author ehlxr diff --git a/src/main/java/io/github/ehlxr/rate/SlideWindow.java b/src/main/java/io/github/ehlxr/rate/SlideWindow.java index ea2e7bd..72e7ae9 100644 --- a/src/main/java/io/github/ehlxr/rate/SlideWindow.java +++ b/src/main/java/io/github/ehlxr/rate/SlideWindow.java @@ -28,6 +28,7 @@ import java.util.LinkedList; import java.util.List; import java.util.Random; import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; /** * 滑动时间窗口限流工具 @@ -93,25 +94,19 @@ public class SlideWindow { /** * 等待直到获得允许 */ - public synchronized void tryAcquire() throws InterruptedException { - long nowTime = System.currentTimeMillis(); - if (list.size() < count) { - list.add(0, nowTime); - return; - } - - long l = nowTime - list.get(count - 1); - if (l <= timeWindow) { + public synchronized void tryAcquire(long timeout, TimeUnit unit) throws Exception { + if (!acquire()) { + long microsToWait = TimeUnit.MILLISECONDS.toMicros(System.currentTimeMillis() - list.get(count - 1)); + if (unit.toMicros(timeout) < microsToWait) { + throw new TimeoutException(); + } // 等待 - TimeUnit.MILLISECONDS.sleep(timeWindow - l); - tryAcquire(); - } else { - list.remove(count - 1); - list.add(0, nowTime); + TimeUnit.MICROSECONDS.sleep(microsToWait); + acquire(); } } - public static void main(String[] args) throws InterruptedException { + public static void main(String[] args) throws Exception { SlideWindow slideWindow = new SlideWindow(5, 1000); while (true) { @@ -119,7 +114,7 @@ public class SlideWindow { // if (slideWindow.acquire()) { // System.out.println(System.currentTimeMillis()); // } - slideWindow.tryAcquire(); + slideWindow.tryAcquire(2, TimeUnit.SECONDS); System.out.println(System.currentTimeMillis()); diff --git a/src/main/java/io/github/ehlxr/redis/JedisUtil.java b/src/main/java/io/github/ehlxr/redis/JedisUtil.java index d4652a0..c72e886 100644 --- a/src/main/java/io/github/ehlxr/redis/JedisUtil.java +++ b/src/main/java/io/github/ehlxr/redis/JedisUtil.java @@ -24,6 +24,7 @@ package io.github.ehlxr.redis; +import io.github.ehlxr.SerializeUtil; import org.springframework.util.StringUtils; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; @@ -83,8 +84,8 @@ public class JedisUtil { * 回收jedis */ public static void returnJedis(Jedis jedis) { - if (jedis != null) - jedisPool.returnResource(jedis); + // if (jedis != null) + // jedisPool.returnResource(jedis); } /** diff --git a/src/main/java/io/github/ehlxr/test/Main.java b/src/main/java/io/github/ehlxr/test/Main.java deleted file mode 100644 index 0f6a53f..0000000 --- a/src/main/java/io/github/ehlxr/test/Main.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright © 2020 xrv - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -package io.github.ehlxr.test; - -import io.github.ehlxr.redis.JedisUtil; - -/** - * Created by ehlxr on 2016/6/14. - */ -public class Main { - public static void main(String[] args) { - JedisUtil.set("test_20160614", "20160614"); - System.out.println(JedisUtil.getStr("test_20160614")); - } -}