-
Notifications
You must be signed in to change notification settings - Fork 8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* VersionUtil to get version from jar * NetUtil to get available port to bind conveniently Fix: * LogBase repackage and support variable arguments * SentinelConfig remove duplicated JVM properties overriding
- Loading branch information
1 parent
94dc796
commit 0cd7c34
Showing
13 changed files
with
250 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
sentinel-core/src/main/java/com/alibaba/csp/sentinel/util/VersionUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.alibaba.csp.sentinel.util; | ||
|
||
import com.alibaba.csp.sentinel.log.RecordLog; | ||
|
||
/** | ||
* Get Sentinel version from MANIFEST.MF | ||
* | ||
* @author jason<jason@dayima.com> @ Sep 28, 2018 | ||
* | ||
*/ | ||
public class VersionUtil { | ||
public static String getVersion(String defaultVersion) { | ||
try { | ||
String version = VersionUtil.class.getPackage().getImplementationVersion(); | ||
return version == null || version.length() == 0 ? defaultVersion : version; | ||
} catch (Throwable e) { | ||
RecordLog.warn("return default version, ignore exception", e); | ||
return defaultVersion; | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
sentinel-core/src/test/java/com/alibaba/csp/sentinel/util/VersionUtilTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.alibaba.csp.sentinel.util; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
public class VersionUtilTest { | ||
@Test | ||
public void versionTest() { | ||
String version = VersionUtil.getVersion("1.0"); | ||
/** | ||
* manifest cannot be load before package | ||
*/ | ||
Assert.assertEquals("1.0", version); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
...tinel-transport-common/src/main/java/com/alibaba/csp/sentinel/transport/util/NetUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package com.alibaba.csp.sentinel.transport.util; | ||
|
||
import java.io.IOException; | ||
import java.net.ServerSocket; | ||
|
||
/** | ||
* net utils for transporting | ||
* | ||
* @author jason<jason@dayima.com> @ Sep 28, 2018 | ||
* | ||
*/ | ||
public class NetUtil { | ||
|
||
/** | ||
* Get a server socket from an avaliable port from a base port.<br> | ||
* Increasement on port number will happen when the port has already been | ||
* used.<br> | ||
* <p> | ||
* To avoid infinite loop 200 attemps will be made in most.<br> | ||
* Default backlog is 100. | ||
* | ||
* @param port | ||
* @return null for error | ||
*/ | ||
public static ServerSocket getServerSocketFromBasePort(int port) { | ||
return getServerSocketFromBasePort(port, 100, 200); | ||
} | ||
|
||
/** | ||
* Get a server socket from an avaliable port from a base port.<br> | ||
* Increasement on port number will happen when the port has already been | ||
* used.<br> | ||
* <p> | ||
* To avoid infinite loop 200 attemps will be made in most. | ||
* | ||
* @param port | ||
* @param backlog | ||
* @return null for error | ||
*/ | ||
public static ServerSocket getServerSocketFromBasePort(int port, int backlog) { | ||
return getServerSocketFromBasePort(port, backlog, 200); | ||
} | ||
|
||
/** | ||
* Get a server socket from an avaliable port from a base port.<br> | ||
* Increasement on port number will happen when the port has already been | ||
* used.<br> | ||
* | ||
* @param port | ||
* @param backlog | ||
* @param tries | ||
* total count of increasement | ||
* @return | ||
*/ | ||
public static ServerSocket getServerSocketFromBasePort(int port, int backlog, int tries) { | ||
while (tries-- > 0) { | ||
try { | ||
return new ServerSocket(port++, backlog); | ||
} catch (IOException e) { | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
/** | ||
* Get an available port to bind with maximum 200 tries | ||
* | ||
* @param port | ||
* @return 0 for failure | ||
*/ | ||
public static int getAvailablePortFromBase(int port) { | ||
return getAvailablePortFromBase(port, 200); | ||
} | ||
|
||
/** | ||
* Get an available port to bind | ||
* | ||
* @param port | ||
* @param maxTries | ||
* @return 0 for failure | ||
*/ | ||
public static int getAvailablePortFromBase(int port, int maxTries) { | ||
ServerSocket serverSocket = getServerSocketFromBasePort(port, 50, maxTries); | ||
if (serverSocket != null) { | ||
int finalPort = serverSocket.getLocalPort(); | ||
try { | ||
serverSocket.close(); | ||
} catch (IOException e) { | ||
} | ||
return finalPort; | ||
} | ||
return 0; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
...l-transport-common/src/test/java/com/alibaba/csp/sentinel/transport/util/NetUtilTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.alibaba.csp.sentinel.transport.util; | ||
|
||
import java.io.IOException; | ||
import java.net.ServerSocket; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
public class NetUtilTest { | ||
@Test | ||
public void availablePortTest() { | ||
// get a port first | ||
int port = NetUtil.getAvailablePortFromBase(3000, 5000); | ||
Assert.assertTrue(port > 0); | ||
// try to create a socket based on it (should be just use it) | ||
ServerSocket serverSocket = NetUtil.getServerSocketFromBasePort(port); | ||
Assert.assertNotNull(serverSocket); | ||
Assert.assertEquals(port, serverSocket.getLocalPort()); | ||
// get a port based on it again while the socket working, | ||
// a new greater port will be returned | ||
int portNew = NetUtil.getAvailablePortFromBase(port); | ||
Assert.assertTrue(portNew > port); | ||
// close the socket and fetch again, same port will be returned | ||
try { | ||
serverSocket.close(); | ||
} catch (IOException e) { | ||
} | ||
portNew = NetUtil.getAvailablePortFromBase(port); | ||
Assert.assertEquals(portNew, port); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.