announce: unofficial gRPC scopes and websocket scopes

175 views
Skip to first unread message

Piotr Morgwai Kotarbinski

unread,
Apr 17, 2021, 5:51:22 AM4/17/21
to google-guice
Hello all,
Recently I've finally found some time to polish and generalize a set of classes providing Guice scopes for gRPC and websockets, that I was copy-pasting-tweaking for every java project for last few years: maybe someone else will find it useful :)

Provides `rpcScope` and `listenerCallScope`. Oversimplifying, in case of a streaming client, `listenerCallScope` spans over processing of a single message from client's stream, while `rpcScope` spans over a whole given RPC. Oversimplifying again, in case of a unary client, these 2 scopes have roughly the same span.

Provides
`requestScope` spans over HTTP servlet requests and websocket events, such as connection open/closed or message received (ie, over each of endpoints' annotated methods or overriding those of Endpoint)
`websocketConnectionScope` spans over a whole endpoint connection (javax.websocket.Session)
`httpSessionScope` available both to servlets and websocket endpoints

both libs are built on top of https://github.com/morgwai/guice-context-scopes which automates transferring scopes when dispatching work to new threads (useful for async processing).

answering in advance why websocket scopes are not built on top of the official servlet scopes:
- this implementation is thread-safe: a single request can be handled by multiple threads (as long as accessed scoped objects are thread-safe)
- guice-context-scopes lib was first developed for gRPC scopes. After that, it felt more natural to use it also for websocket scopes, rather than pretend that everything is an HttpServletRequest. I may be biased here however ;-)


Feedback is welcome :)

Cheers!

Deba

unread,
Oct 4, 2021, 5:43:17 AM10/4/21
to google-guice
Interesting :) . Would you please elaborate a sample use case that was your motivation? 

Debasish

Piotr Morgwai Kotarbinski

unread,
Nov 8, 2021, 7:26:51 AM11/8/21
to google-guice
Hi,
Sorry for the late reply: apparently google decided that I'm not worthy of receiving notifications and I've noticed your message just now by chance.
(I've even checked if maybe gmail decided that messages from google are spam and verified that I'm subscribed to this thread, yet no notification email anywhere...)

there were 2 main points that motivated me:
- to have functionality similar to servlet's REQUEST_SCOPE and SESSION_SCOPE available when developing websockets and gRPC services, so that I could scope objects related to given websocket/gRPC message properly (or to a whole websocket connection / gRPC stream).
- make it easy (ie: automate as much as possible) to keep scopes when switching threads, which is very convenient when using asynchronous approach in server design.

You can have a look at samples mentioned in the README of each repo. In both cases there's an example showing how to scope EntityManager to a given message, so that all component performing JPA operations within processing of the same message, will automatically do their stuff within the same common transaction. Here are the links to the "core" files of each example:

Using this opportunity to share some news, I've recently added jakarta name-space support to websocket scopes: jar is now available with 2 classifiers: servlet-scopes-5.0-javax and servlet-scopes-5.0-jakarta-experimental.
Jakarta flavor is based on repackaged version of Guice from GuicedEE project: https://guicedee.com/ . There are some small issues with it (see notes: https://github.com/morgwai/servlet-scopes#notes-on-jakarta-support ), but seems to work of so far.

Cheers!

Piotr Morgwai Kotarbinski

unread,
Nov 8, 2021, 7:38:37 AM11/8/21
to google-guice
[reposting linkified version: i hate google-groups]

Hi,
Sorry for the late reply: apparently google decided that I'm not worthy of receiving notifications and I've noticed your message just now by chance.
(I've even checked if maybe gmail decided that messages from google are spam and verified that I'm subscribed to this thread, yet no notification email anywhere...)

there were 2 main points that motivated me:
- to have functionality similar to servlet's REQUEST_SCOPE and SESSION_SCOPE available when developing websockets and gRPC services, so that I could scope objects related to given websocket/gRPC message properly (or to a whole websocket connection / gRPC stream).
- make it easy (ie: automate as much as possible) to keep scopes when switching threads, which is very convenient when using asynchronous approach in server design.

You can have a look at samples mentioned in the README of each repo. In both cases there's an example showing how to scope EntityManager to a given message, so that all component performing JPA operations within processing of the same message, will automatically do their stuff within the same common transaction. Here are the links to the "core" files of each example:

Using this opportunity to share some news, I've recently added jakarta name-space support to websocket scopes: jar is now available with 2 classifiers: servlet-scopes-5.0-javax and servlet-scopes-5.0-jakarta-experimental.
Jakarta flavor is based on repackaged version of Guice from GuicedEE project: https://guicedee.com/ . There are some small issues with it (see notes: https://github.com/morgwai/servlet-scopes#notes-on-jakarta-support ), but seems to work ok so far.

Cheers!


On Monday, October 4, 2021 at 4:43:17 PM UTC+7 padhy.d...@gmail.com wrote:
Reply all
Reply to author
Forward
0 new messages