grpc / grpc-dotnet

gRPC for .NET
Apache License 2.0
4.16k stars 765 forks source link

[question] least connections GRPC load balancing #2389

Open vrnmthr opened 6 months ago

vrnmthr commented 6 months ago

I understand that we can create a custom load balancer as per https://learn.microsoft.com/en-us/aspnet/core/grpc/loadbalancing?view=aspnetcore-5.0#create-a-custom-load-balancer.

I want to create a load balancer that sends requests to the endpoint with the least number of currently active connections. With the SubchannelsLoadBalancer, I can always know when I am "picking" a given subchannel (and thus I can increment its connection count). Is there any hook that I can call when a given request is completed (so I can decrement connection count)?

I am sure that this type of load balancing will be useful for many people. Perhaps it is worth adding into the library as an out of the box configurable option.

vrnmthr commented 6 months ago

I found https://github.com/grpc/grpc-dotnet/blob/master/test/FunctionalTests/Balancer/LeastUsedBalancer.cs. @JamesNK is there a reason this is not exposed publicly?

JamesNK commented 6 months ago

Because it was just an experiment. It also isn't perfect. It doesn't properly track long running streams. It only tracks calls until they return the first message.

AntonSmolkov commented 2 months ago

@JamesNK Are there any plans to make public implementation?

It would be helpful, for instance, in kubenetes envinronment when cpu requests != limits (QoS=Burstable) to distribute load according to real resouses avalability of each pod in the k8s deployment. With round-robin strategy pod that actually has lower resources handles requests slower and thus has more connections stick to it in particular moment of time.

JamesNK commented 2 months ago

No plans.