higherkindness / mu-haskell

Mu (μ) is a purely functional framework for building micro services.
http://higherkindness.io/mu-haskell/
Apache License 2.0
333 stars 19 forks source link

Bi-Directional streaming consumes tons of CPU #300

Closed y-taka-23 closed 3 years ago

y-taka-23 commented 3 years ago

I found that a server consumes around 100% of CPU load during bi-directional RPC.

I can repro the phenomenon even in a very simple call like the followings. On the other hand, libraries in other languages (e.g. grpc-go) don't that. So I'm afraid it is from an immanent problem in Mu-Haskell implementation. How can I tame it?

import           Data.Conduit
import qualified Data.Conduit.Combinators as C

sayManyHellos
  :: (MonadServer m)
  => ConduitT () HelloRequest m () -> ConduitT HelloResponse Void m () -> m ()
sayManyHellos src sink = do
    let reply (HelloRequest n) = HelloResponse ("Hello, " <> n)
    runConduit $ src .| C.map reply .| sink
syntax = "proto3";

package quickstart;

service Greeter {
  rpc SayManyHellos (stream HelloRequest) returns (stream HelloResponse) {}
}

message HelloRequest { string name = 1; }
message HelloResponse { string message = 1; }
grpc "TheSchema" id "greeter.proto"

data HelloRequest = HelloRequest {
      name :: T.Text
    } deriving (
      Eq, Show, Generic
    , ToSchema   TheSchema "HelloRequest"
    , FromSchema TheSchema "HelloRequest"
    )

data HelloResponse = HelloResponse {
      message :: T.Text
    } deriving (
      Eq, Show, Generic
    , ToSchema   TheSchema "HelloResponse"
    , FromSchema TheSchema "HelloResponse"
    )