Skip to content

Conversation

@miparnisari
Copy link
Contributor

@miparnisari miparnisari commented Aug 26, 2025

Description

Streaming APIs such as Watch can get disconnected. This PR provides a code snippet that can be used to automatically re-connect.

Testing

Locally, with https://github.com/miparnisari/authzed-java-client-test

@miparnisari miparnisari requested a review from a team as a code owner August 26, 2025 21:48
@miparnisari miparnisari force-pushed the add-watch-code-snippet branch 3 times, most recently from 9b3e0b0 to f915ccd Compare August 26, 2025 22:12
Copy link
Contributor

@tstirrat15 tstirrat15 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we either put it in the examples directory or additionally put it in the examples dir? https://github.com/authzed/authzed-java/tree/main/examples/v1

@miparnisari
Copy link
Contributor Author

miparnisari commented Aug 27, 2025

@tstirrat15 why "additionally"? What value does having it in the "examples" directory bring? Personal opinion, but it seems quite hidden

@miparnisari miparnisari force-pushed the add-watch-code-snippet branch from f915ccd to 393cf64 Compare August 27, 2025 00:23
README.md Outdated
}
```

### Calling `Watch`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we perhaps move this to the examples directory so it can be compiled and updated as needed? Then we can link the example from the readme. Same for check permissions.

README.md Outdated
builder.setOptionalStartCursor(lastZedToken);
}

WatchRequest request = builder.build();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest enabling heartbeats. This solves the confusion around idle timeouts and long-lived gRPC streaming APIs.

Copy link
Contributor Author

@miparnisari miparnisari Aug 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need to update the API first.

README.md Outdated

} catch (Exception e) {
if (e instanceof StatusRuntimeException sre && sre.getStatus().getCode().equals(Status.UNAVAILABLE.getCode()) &&
(sre.getMessage().contains("stream timeout") || sre.getMessage().contains("RST_STREAM closed stream"))) {
Copy link
Contributor

@vroldanbet vroldanbet Aug 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn't be checking the message to trigger retries - these could be subject to change. I assume those have INTERNAL grpc code, so those are safe to be retried.

In addition to this manual handling, we could add retry policies, which is built into the Java gRPC SDK. Please note that retry policies are only supported for server-streaming APIs (like the Watch API), but are not supported for client-streaming and bidirectional APIs. I'm also not fully sure server-streaming is retried once data has been streamed back to the client.

@tstirrat15
Copy link
Contributor

tstirrat15 commented Aug 27, 2025

@miparnisari i realize that it isn't often a place you would look, but when it's in a .java file instead of a .md file, you can do things like lint and build as a part of CI and ensure that updates to the library haven't broken the usage that you're saying is correct - or at least that's where i'm coming from with this.

@miparnisari miparnisari force-pushed the add-watch-code-snippet branch 2 times, most recently from a13c387 to fa39172 Compare August 27, 2025 18:23
@miparnisari miparnisari force-pushed the add-watch-code-snippet branch 2 times, most recently from 7add8f9 to 6095dc4 Compare August 27, 2025 23:34
@miparnisari miparnisari force-pushed the add-watch-code-snippet branch from 6095dc4 to f239482 Compare August 27, 2025 23:46

} catch (Exception e) {
if (e instanceof StatusRuntimeException sre && (sre.getStatus().getCode().equals(Status.UNAVAILABLE.getCode()) ||
(sre.getStatus().getCode().equals(Status.INTERNAL.getCode())) && sre.getMessage().contains("stream timeout"))) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO we should just do this on the Internal code alone

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't we also retry on Unavailable?

Copy link

@ecordell ecordell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, with the caveat I haven't run this locally myself. But we can follow up if we find issues.

@vroldanbet vroldanbet added this pull request to the merge queue Aug 29, 2025
Merged via the queue into main with commit 023259f Aug 29, 2025
14 checks passed
@vroldanbet vroldanbet deleted the add-watch-code-snippet branch August 29, 2025 07:28
@github-actions github-actions bot locked and limited conversation to collaborators Aug 29, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants