Skip to content

Commit

Permalink
chore: session keep alive heartbeat
Browse files Browse the repository at this point in the history
Signed-off-by: Jeronimo Irazabal <jeronimo.irazabal@gmail.com>
  • Loading branch information
jeroiraz committed Nov 1, 2022
1 parent de50cda commit 90bac3c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 15 deletions.
36 changes: 28 additions & 8 deletions src/main/java/io/codenotary/immudb4j/ImmuClient.java
Expand Up @@ -37,6 +37,8 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

Expand All @@ -50,16 +52,19 @@ public class ImmuClient {

private final PublicKey serverSigningKey;
private final ImmuStateHolder stateHolder;
private long keepAlivePeriod;

private ManagedChannel channel;

private final ImmuServiceGrpc.ImmuServiceBlockingStub stub;

private Session session;

private Timer sessionHeartBeat;

public ImmuClient(Builder builder) {
this.stateHolder = builder.getStateHolder();
this.serverSigningKey = builder.getServerSigningKey();
this.keepAlivePeriod = builder.getKeepAlivePeriod();
this.stub = createStubFrom(builder);
}

Expand Down Expand Up @@ -117,14 +122,29 @@ public synchronized void openSession(String username, String password, String da

final ImmudbProto.OpenSessionResponse resp = this.stub.openSession(req);

this.session = new Session(resp.getSessionID(), username, database);
this.session = new Session(resp.getSessionID(), database);

this.sessionHeartBeat = new Timer();

this.sessionHeartBeat.schedule(new TimerTask() {
@Override
public void run() {
try {
stub.keepAlive(Empty.getDefaultInstance());
} catch (Exception e) {
e.printStackTrace();
}
}
}, 0, keepAlivePeriod);
}

public synchronized void closeSession() {
if (this.session == null) {
throw new IllegalStateException("no open session");
}

this.sessionHeartBeat.cancel();

try {
this.stub.closeSession(Empty.getDefaultInstance());
} finally {
Expand Down Expand Up @@ -1133,15 +1153,15 @@ public static class Builder {

private PublicKey serverSigningKey;

private boolean withAuth;
private long keepAlivePeriod;

private ImmuStateHolder stateHolder;

private Builder() {
this.serverUrl = "localhost";
this.serverPort = 3322;
this.stateHolder = new SerializableImmuStateHolder();
this.withAuth = true;
this.keepAlivePeriod = 60 * 1000; // 1 minute
}

public ImmuClient build() {
Expand Down Expand Up @@ -1179,12 +1199,12 @@ public Builder withServerSigningKey(String publicKeyFilename) throws Exception {
return this;
}

public boolean isWithAuth() {
return withAuth;
public long getKeepAlivePeriod() {
return keepAlivePeriod;
}

public Builder withAuth(boolean withAuth) {
this.withAuth = withAuth;
public Builder withKeepAlivePeriod(long keepAlivePeriod) {
this.keepAlivePeriod = keepAlivePeriod;
return this;
}

Expand Down
8 changes: 1 addition & 7 deletions src/main/java/io/codenotary/immudb4j/Session.java
Expand Up @@ -17,23 +17,17 @@

public class Session {

private String username;
private String sessionID;
private String database;

public Session(String sessionID, String username, String database) {
public Session(String sessionID, String database) {
this.sessionID = sessionID;
this.username = username;
this.database = database;
}

public String getSessionID() {
return sessionID;
}

public String getUsername() {
return username;
}

public String getDatabase() {
return database;
Expand Down

0 comments on commit 90bac3c

Please sign in to comment.