update at 2021-07-16 10:48:40 by ehlxr
parent
163882bb4d
commit
b3dbc9f668
26
pom.xml
26
pom.xml
|
@ -5,18 +5,18 @@
|
|||
<artifactId>budd</artifactId>
|
||||
<packaging>war</packaging>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>11</source>
|
||||
<target>11</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
<!--<build>-->
|
||||
<!-- <plugins>-->
|
||||
<!-- <plugin>-->
|
||||
<!-- <groupId>org.apache.maven.plugins</groupId>-->
|
||||
<!-- <artifactId>maven-compiler-plugin</artifactId>-->
|
||||
<!-- <configuration>-->
|
||||
<!-- <source>11</source>-->
|
||||
<!-- <target>11</target>-->
|
||||
<!-- </configuration>-->
|
||||
<!-- </plugin>-->
|
||||
<!-- </plugins>-->
|
||||
<!--</build>-->
|
||||
|
||||
<name>budd</name>
|
||||
<url>http://maven.apache.org</url>
|
||||
|
@ -173,7 +173,7 @@
|
|||
<dependency>
|
||||
<groupId>redis.clients</groupId>
|
||||
<artifactId>jedis</artifactId>
|
||||
<version>3.4.0</version>
|
||||
<version>3.4.1</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
|
|
|
@ -22,7 +22,7 @@
|
|||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package io.github.ehlxr.redis;
|
||||
package io.github.ehlxr;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
|
@ -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) {
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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());
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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"));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue