Skip to content

Commit

Permalink
optimisation
Browse files Browse the repository at this point in the history
  • Loading branch information
Alonexc committed Apr 15, 2024
1 parent ad5a26b commit 59f9fc0
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,17 @@
*/
public class SDKManager {

private static final SDKManager INSTANCE = new SDKManager();
private static volatile SDKManager INSTANCE = null;


public static SDKManager getInstance() {
public static synchronized SDKManager getInstance() {
if (INSTANCE == null) {
synchronized (SDKManager.class) {
if (INSTANCE == null) {
INSTANCE = new SDKManager();
}
}
}
return INSTANCE;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ public class CreateEtcdConfig implements CreateSDKConfig {

private String etcdServerAddress;

private Long connectTime;
@Builder.Default()
private int connectTime = 10;

@Override
public String getUniqueKey() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ public class CreateRedisConfig implements CreateSDKConfig {

private String password;

private Integer timeOut;
@Builder.Default
private int timeOut = 10;

@Override
public String getUniqueKey() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,12 @@

import org.apache.eventmesh.dashboard.core.function.SDK.config.CreateEtcdConfig;

import java.nio.charset.StandardCharsets;
import java.util.AbstractMap.SimpleEntry;
import java.util.List;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import io.etcd.jetcd.ByteSequence;
import io.etcd.jetcd.KV;
import io.etcd.jetcd.KeyValue;
import io.etcd.jetcd.kv.GetResponse;

import lombok.extern.slf4j.Slf4j;

Expand All @@ -38,39 +33,24 @@ public class EtcdSDKCreateOperationTest {

private final EtcdSDKOperation etcdSDKOperation = new EtcdSDKOperation();

private static final String key = "/test/foo";

private static final String value = "test";

private static final String url = "http://localhost:2379";

@Test
void testCreateClient() {
// final CreateEtcdConfig etcdConfig = CreateEtcdConfig.builder()
// .etcdServerAddress(url)
// .connectTime(10L)
// .build();
// // etcdConfig.setEtcdServerAddress(url);
// SimpleEntry<String, KV> simpleEntry = null;
// try {
// simpleEntry = etcdSDKOperation.createClient(etcdConfig);
// Assertions.assertEquals(url, simpleEntry.getKey());
// simpleEntry.getValue().put(bytesOf(key), bytesOf(value));
// final GetResponse response = simpleEntry.getValue().get(bytesOf(key)).get();
// final List<KeyValue> keyValues = response.getKvs();
// log.info("get key = {} , value = {} from etcd success",
// keyValues.get(0).getKey().toString(StandardCharsets.UTF_8),
// keyValues.get(0).getValue().toString(StandardCharsets.UTF_8));
// simpleEntry.getValue().close();
// } catch (Exception e) {
// log.error("create etcd client failed", e);
// if (simpleEntry != null) {
// simpleEntry.getValue().close();
// }
// }
}

private static ByteSequence bytesOf(String val) {
return ByteSequence.from(val, StandardCharsets.UTF_8);
final CreateEtcdConfig etcdConfig = CreateEtcdConfig.builder()
.etcdServerAddress(url)
.connectTime(5)
.build();
SimpleEntry<String, KV> simpleEntry = null;
try {
simpleEntry = etcdSDKOperation.createClient(etcdConfig);
Assertions.assertEquals(url, simpleEntry.getKey());
simpleEntry.getValue().close();
} catch (Exception e) {
log.error("create etcd client failed", e);
if (simpleEntry != null) {
simpleEntry.getValue().close();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
@Slf4j
class RedisSDKCreateOperationTest {

private RedisSDKOperation redisClientCreateOperation = new RedisSDKOperation();
private final RedisSDKOperation redisClientCreateOperation = new RedisSDKOperation();

@Test
void testCreateClient() {
Expand All @@ -41,7 +41,6 @@ void testCreateClient() {
.password("")
.timeOut(5)
.build();
// createClientConfig.setRedisUrl("redis://localhost:6379");
SimpleEntry<String, StatefulRedisConnection<String, String>> simpleEntry = null;
try {
simpleEntry = redisClientCreateOperation.createClient(createClientConfig);
Expand Down

0 comments on commit 59f9fc0

Please sign in to comment.