openfaas / faas

OpenFaaS - Serverless Functions Made Simple
https://www.openfaas.com
MIT License
25.15k stars 1.94k forks source link

Async: Custom headers and QueryString should be serialized and passed onto function. #369

Closed alexellis closed 6 years ago

alexellis commented 6 years ago

Custom headers and QueryString should be serialized and passed onto function.

Expected Behaviour

Custom headers such as X-Forwarded-By or query string values such as ?id=alex should be passed onto the function when it is invoked by a queue worker.

Current Behaviour

Ignored by async, passed by synchronous invocations.

Possible Solution

Alter the en-queuing code to serialize this additional data

Steps to Reproduce (for bugs)

curl -H "X-Forwarded-By: OpenFaaS-Cloud" localhost:8080/async-function/env?id=alex

Should result in two headers being exposed as environment:

Http_X_Forwarded_By
Http_Querystring

Context

Additional meta-data is key when the body of a request is RAW binary.

Your Environment

Tested on Swarm but counts for any provider.

alexellis commented 6 years ago

The header may already be serialized - https://github.com/openfaas/faas/blob/master/gateway/queue/types.go#L10

ericstoekl commented 6 years ago

There may be many concurrent requests to the same function, so will each function call be guaranteed to have the environment variables set to its proper value at the time of function execution?

For example, I send data blob A to the function with environment variable setting a parameter to b. At nearly the exact same time, another user sends data blob C to the same function with parameter set to d. Will it be guaranteed that when the function operates on A, the environment variable will still be b, and not d?

Let me know if this makes sense. Thanks

alexellis commented 6 years ago

We fork a process for each request in the watchdog and in afterburn we are passing data in the same way to a single process. This work is complete for synchronous calling. The ticket is for implementing in the queue code.

SeanDongX commented 6 years ago

I can take a look

SeanDongX commented 6 years ago

Fix: https://github.com/openfaas/nats-queue-worker/pull/7

@alexellis needless to say, when the PR is approved, you have to push a new docker image at functions/queue-worker:#tag

and then change the following lines in docker-compose.extended.yml

queue-worker:
        image: functions/queue-worker:#tag

And then the issue can be finally closed!

alexellis commented 6 years ago

Thanks for working on this.

alexellis commented 6 years ago

Merged, thanks @wonderxboy