Skip to content

Commit

Permalink
Merge pull request #402 from project-tsurugi/wip/i_601_apply_comments
Browse files Browse the repository at this point in the history
apply commnets
  • Loading branch information
t-horikawa authored Jan 25, 2024
2 parents 9404624 + 6fffff5 commit ea2ba04
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ public abstract class Link implements ServerResource {
private long receivedMessageNumber = 0;

protected ResponseBox responseBox = new ResponseBox(this);
protected TimeUnit timeUnit;
protected long timeout = 0;
protected TimeUnit closeTimeUnit;
protected long closeTimeout = 0;

/**
* Getter of the receivedMessageNumber.
Expand Down Expand Up @@ -114,8 +114,8 @@ public void pullMessage(long checkedMessageNumber, long t, TimeUnit u) throws Ti
* @param t the timeout
*/
public void setCloseTimeout(Timeout t) {
timeout = t.value();
timeUnit = t.unit();
closeTimeout = t.value();
closeTimeUnit = t.unit();
}

/**
Expand All @@ -140,9 +140,9 @@ ResponseBox getResponseBox() {

// to suppress spotbug error
long value() {
return this.timeout;
return this.closeTimeout;
}
TimeUnit unit() {
return this.timeUnit;
return this.closeTimeUnit;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -143,15 +143,21 @@ public void setCloseTimeout(Timeout timeout) {
*/
@Override
public void close() throws IOException {
try {
if (!closed.get()) {
if (!closed.getAndSet(true)) {
try {
link.close();
closed.set(true);
} catch (ServerException | InterruptedException e) {
throw new IOException(e);
}
} catch (ServerException | InterruptedException e) {
throw new IOException(e);
}
}

/**
* Set wire to close state without link close.
* The link must be closed for this method to be called.
*/
public void closeWithoutGet() {
closed.set(true);
}

private static EndpointRequest.Request.Builder newRequest() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,24 @@ private boolean doPull(long timeout, TimeUnit unit, boolean throwException) thro
}
}

private int closeTimeoutMillis() {
if (closeTimeout == 0) {
return 0;
}
if (closeTimeUnit.toMillis(closeTimeout) > Integer.MAX_VALUE) {
return Integer.MAX_VALUE;
}
return (int) closeTimeUnit.toMillis(closeTimeout);
}

private void closeBoxes(boolean intentionalClose) throws IOException {
responseBox.doClose(intentionalClose);
resultSetBox.doClose(intentionalClose);
if (!socket.isClosed()) {
socket.setSoTimeout(closeTimeoutMillis());
socket.close();
}
socketClosed.set(true);
socket.close();
}

public ResultSetBox getResultSetBox() {
Expand Down Expand Up @@ -281,7 +294,7 @@ public void close() throws IOException, ServerException {
try (var c1 = socket; var c2 = inStream; var c3 = outStream) {
send(REQUEST_SESSION_BYE, TERMINATION_REQUEST);
while (!socketClosed.get()) {
doPull(timeout, timeUnit);
doPull(closeTimeout, closeTimeUnit);
}
} catch (TimeoutException e) {
socketError.set(true);
Expand All @@ -295,10 +308,12 @@ public void close() throws IOException, ServerException {
}
}

public void closeWithoutGet() throws IOException, ServerException {
/**
* Close the socket without sending REQUEST_SESSION_BYE.
* This method is intended to use before session open.
*/
public void closeWithoutGet() throws IOException {
closed.set(true);
socketClosed.set(true);
socket.close();
closeBoxes(false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@ public Wire get() throws IOException, ServerException, InterruptedException {
wireImpl.setSessionID(futureSessionID.get());
return wireImpl;
} catch (IOException | ServerException | InterruptedException e) {
streamLink.closeWithoutGet();
try {
streamLink.closeWithoutGet();
wireImpl.closeWithoutGet();
} catch (IOException exceptionOnClose) {
e.addSuppressed(exceptionOnClose);
}
throw e;
}
}
Expand All @@ -48,7 +53,12 @@ public Wire get(long timeout, TimeUnit unit) throws IOException, ServerException
wireImpl.setSessionID(futureSessionID.get(timeout, unit));
return wireImpl;
} catch (IOException | ServerException | InterruptedException | TimeoutException e) {
streamLink.closeWithoutGet();
try {
streamLink.closeWithoutGet();
wireImpl.closeWithoutGet();
} catch (IOException exceptionOnClose) {
e.addSuppressed(exceptionOnClose);
}
throw e;
}
}
Expand All @@ -66,7 +76,7 @@ public void close() throws IOException, ServerException, InterruptedException {
if (!gotton.getAndSet(true)) {
futureSessionID.get(); // ensure notify client of session limit error
streamLink.closeWithoutGet();
wireImpl.close();
wireImpl.closeWithoutGet();
}
}
}

0 comments on commit ea2ba04

Please sign in to comment.