typelevel / feral

Feral cats are homeless, feral functions are serverless
Apache License 2.0
168 stars 41 forks source link

Implement Google Functions Framework #132

Open armanbilge opened 2 years ago

armanbilge commented 2 years ago

https://github.com/GoogleCloudPlatform/functions-framework#functions-framework-contract

A Functions Framework consists of two parts:

  • A package that instantiates a web server and invokes function code in response to an HTTP request. This package may include additional functionality to further minimize boilerplate.
  • A script or tool that converts a source code transform of function code into app code ("the function-to-app converter")

This is lower-level than Cloud Functions, which enables us to implement concurrency, and thus achieve better efficiency than non-concurrent serverless options like Lambda.

For performance, efficiency and correctness reasons, the framework must be able to handle multiple concurrent invocations of the developer's function.

Concurrency is configurable. By default each Cloud Run container instance can receive up to 80 requests at the same time; you can increase this to a maximum of 1000. Note that in comparison, Functions-as-a-Service (FaaS) solutions like Cloud Functions have a fixed concurrency of 1.

Note that these frameworks are containerized and deployed to e.g. Cloud Run. This is different from FaaS, where you typically upload an npm package or a jar.

These tips for running Java in Cloud Run (and only Java!) suggest that Node.js is still an ideal runtime even for the framework setup: https://cloud.google.com/run/docs/tips/java

There's also a testkit: https://github.com/GoogleCloudPlatform/functions-framework-conformance

Finally, none of this is specific to Google Cloud per se: it works with ordinary HTTP events and platform-agnostic CloudEvents and is deployable to kubernetes/knative ... although, not sure how widely adopted these are outside of Google (AWS does not support CloudEvents, https://github.com/cloudevents/spec/issues/435).

This also makes testing locally much easier, since it does not rely on emulators of serverless environments.

tanishiking commented 9 months ago

While CloudEvents handler support may take some effort since we need to have some sort of CloudEvents SDK for Scala, supporting HTTP would be the first step? ref: https://github.com/typelevel/feral/issues/268

armanbilge commented 9 months ago

supporting HTTP would be the first step

@tanishiking I think we already support HTTP. As far as I can tell there is nothing special about it, it is just ordinary HTTP. So you can make an HTTP application with http4s Ember server and deploy it to Google Cloud Run.

tanishiking commented 9 months ago

@armanbilge Great, thanks! I gonna play around with GCP stuffs with Scala Native when I have time, since most of GCP APIs are well defined with gRPC (or OpenAPI) https://github.com/googleapis/googleapis.github.io/ and I remember you mentioned there exists pure Scala gRPC client davenverse/http4s-grpc on Discord 👍

https://discord.com/channels/632150470000902164/635668881951686686/1152248482992762932

tanishiking commented 9 months ago

Oh, it's already generated https://davenverse.github.io/googleapis-http4s/ 😃