grpc / grpc/grpc-java

Overload ServerBuilder.addService to take a Supplier of the service definition

Offen
#1,576 1 Kommentar 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
Vorherrschende Sprache
Java
Sterne
12.1k
Forks
4k
Ø Merge
2 T. 17 Std.
Gemergte PRs (30 T.)
37

Beschreibung

In general, servers want to instantiate a new method handler for each call to avoid polluted state. We'd like this to be handled by the framework, rather than asking application developers to remember it. The current API essentially pushes the responsibility to the application:
- The `Server` is a singleton since it has a long-lived lifecycle
- `ServerBuilder.addService(ServerServiceDefinition service)` requires that the `service` be instantiated at startup
- `FooServiceGrpc.bindService(FooService serviceImpl)` also requires that the `serviceImpl` be instantiated. Thus, the `FooService` has to be a singleton with respect to the server lifecycle. That is, your implementation of the generated service stub has to be a singleton. So the app developer has to worry about the scope of the dependency chain from there down.

I propose overloading the generated code to take a `Supplier` of some sort: `FooServiceGrpc.bindService(Supplier serviceImplSupplier)`. This would let me pass a Guice `Provider::get` to the `bindService` method and get whatever Guice scoping I want on method calls. This lets me handle proper scoping in my own framework code rather than in every implementation of a stub.

Almost as good would be pushing the thunk-ness back a layer to `ServerBuilder.addService(Supplier serviceSupplier)`, but I suspect that is harder to implement.

Beitragsleitfaden

Beitragsleitfaden öffnen

Rechercherichtung

Beginnen Sie mit dem Lesen der vorhandenen APIs ServerBuilder.addService(ServerServiceDefinition) und FooServiceGrpc.bindService(FooService), um zu verstehen, wo Dienstinstanzen erstellt werden. Legen Sie fest, welche API einen supplier akzeptieren soll und wie die Erstellung eines Dienstes pro Aufruf mit dem Server-Lebenszyklus und dem generierten Code interagiert. Als erledigt gilt die Aufgabe, wenn die ausgewählte Überladung das angeforderte Scoping-Verhalten unterstützt, ohne Singleton-Dienstimplementierungen vorauszusetzen.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
java
Bereich
backend-api-design
Issue-Typ
Feature
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
25/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.