update at 2021-07-16 10:48:40 by ehlxr

This commit is contained in:
2021-07-16 10:48:40 +08:00
parent 163882bb4d
commit b3dbc9f668
7 changed files with 31 additions and 80 deletions

26
pom.xml
View File

@@ -5,18 +5,18 @@
<artifactId>budd</artifactId> <artifactId>budd</artifactId>
<packaging>war</packaging> <packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version> <version>0.0.1-SNAPSHOT</version>
<build> <!--<build>-->
<plugins> <!-- <plugins>-->
<plugin> <!-- <plugin>-->
<groupId>org.apache.maven.plugins</groupId> <!-- <groupId>org.apache.maven.plugins</groupId>-->
<artifactId>maven-compiler-plugin</artifactId> <!-- <artifactId>maven-compiler-plugin</artifactId>-->
<configuration> <!-- <configuration>-->
<source>11</source> <!-- <source>11</source>-->
<target>11</target> <!-- <target>11</target>-->
</configuration> <!-- </configuration>-->
</plugin> <!-- </plugin>-->
</plugins> <!-- </plugins>-->
</build> <!--</build>-->
<name>budd</name> <name>budd</name>
<url>http://maven.apache.org</url> <url>http://maven.apache.org</url>
@@ -173,7 +173,7 @@
<dependency> <dependency>
<groupId>redis.clients</groupId> <groupId>redis.clients</groupId>
<artifactId>jedis</artifactId> <artifactId>jedis</artifactId>
<version>3.4.0</version> <version>3.4.1</version>
</dependency> </dependency>
<dependency> <dependency>

View File

@@ -22,7 +22,7 @@
* THE SOFTWARE. * THE SOFTWARE.
*/ */
package io.github.ehlxr.redis; package io.github.ehlxr;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;

View File

@@ -24,17 +24,15 @@
package io.github.ehlxr; package io.github.ehlxr;
import com.google.common.collect.Maps;
/** /**
* Created by ehlxr on 2016/12/23. * Created by ehlxr on 2016/12/23.
*/ */
public class dfd { public class dfd {
public static void main(String[] args) { public static void main(String[] args) {
var map = Maps.newHashMap(); // var map = Maps.newHashMap();
map.put("d", 1); // map.put("d", 1);
System.out.println(map); // System.out.println(map);
} }
public void printCircle(int[][] matrix, int startX, int startY, int endX, int endY) { public void printCircle(int[][] matrix, int startX, int startY, int endX, int endY) {

View File

@@ -24,18 +24,12 @@
package io.github.ehlxr.http; package io.github.ehlxr.http;
import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.net.Authenticator;
import java.net.InetSocketAddress;
import java.net.ProxySelector;
import java.net.URI; import java.net.URI;
import java.net.http.HttpClient; import java.net.http.HttpClient;
import java.net.http.HttpRequest; import java.net.http.HttpRequest;
import java.net.http.HttpResponse; import java.net.http.HttpResponse;
import java.nio.file.Paths;
import java.time.Duration; import java.time.Duration;
import java.util.concurrent.CompletableFuture;
/** /**
* @author ehlxr * @author ehlxr

View File

@@ -28,6 +28,7 @@ import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Random; import java.util.Random;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
/** /**
* 滑动时间窗口限流工具 * 滑动时间窗口限流工具
@@ -93,25 +94,19 @@ public class SlideWindow {
/** /**
* 等待直到获得允许 * 等待直到获得允许
*/ */
public synchronized void tryAcquire() throws InterruptedException { public synchronized void tryAcquire(long timeout, TimeUnit unit) throws Exception {
long nowTime = System.currentTimeMillis(); if (!acquire()) {
if (list.size() < count) { long microsToWait = TimeUnit.MILLISECONDS.toMicros(System.currentTimeMillis() - list.get(count - 1));
list.add(0, nowTime); if (unit.toMicros(timeout) < microsToWait) {
return; throw new TimeoutException();
} }
long l = nowTime - list.get(count - 1);
if (l <= timeWindow) {
// 等待 // 等待
TimeUnit.MILLISECONDS.sleep(timeWindow - l); TimeUnit.MICROSECONDS.sleep(microsToWait);
tryAcquire(); acquire();
} else {
list.remove(count - 1);
list.add(0, nowTime);
} }
} }
public static void main(String[] args) throws InterruptedException { public static void main(String[] args) throws Exception {
SlideWindow slideWindow = new SlideWindow(5, 1000); SlideWindow slideWindow = new SlideWindow(5, 1000);
while (true) { while (true) {
@@ -119,7 +114,7 @@ public class SlideWindow {
// if (slideWindow.acquire()) { // if (slideWindow.acquire()) {
// System.out.println(System.currentTimeMillis()); // System.out.println(System.currentTimeMillis());
// } // }
slideWindow.tryAcquire(); slideWindow.tryAcquire(2, TimeUnit.SECONDS);
System.out.println(System.currentTimeMillis()); System.out.println(System.currentTimeMillis());

View File

@@ -24,6 +24,7 @@
package io.github.ehlxr.redis; package io.github.ehlxr.redis;
import io.github.ehlxr.SerializeUtil;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import redis.clients.jedis.Jedis; import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPool;
@@ -83,8 +84,8 @@ public class JedisUtil {
* 回收jedis * 回收jedis
*/ */
public static void returnJedis(Jedis jedis) { public static void returnJedis(Jedis jedis) {
if (jedis != null) // if (jedis != null)
jedisPool.returnResource(jedis); // jedisPool.returnResource(jedis);
} }
/** /**

View File

@@ -1,37 +0,0 @@
/*
* The MIT License (MIT)
*
* Copyright © 2020 xrv <xrg@live.com>
*
* 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"));
}
}