Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mock server #126

Merged
merged 11 commits into from
Mar 15, 2024
Merged

Mock server #126

merged 11 commits into from
Mar 15, 2024

Conversation

chriso
Copy link
Contributor

@chriso chriso commented Mar 14, 2024

It's difficult to try out Dispatch today. Users either have to use ngrok to expose their test functions to the production Dispatch instance, or must manually fit the pieces together from the dispatch.test package (#86) to setup a local server that they can connect to.

This PR introduces a front-end to the components in the dispatch.test package, via a python -m dispatch.test command. Users can use this to quickly spin up a mock Dispatch server locally.

$ python -m dispatch.test --help
Mock Dispatch server for use in test environments.

Usage:
  dispatch.test <endpoint> [--api-key=<key>] [--hostname=<name>] [--port=<port>] [-v | --verbose]
  dispatch.test -h | --help

Options:
     --api-key=<key>      API key to require when clients connect to the server [default: test].

     --hostname=<name>    Hostname to listen on [default: 127.0.0.1].
     --port=<port>        Port to listen on [default: 4450].

  -v --verbose            Show verbose details in the log.
  -h --help               Show this help information.

The mock server is a very basic in-memory implementation of the Dispatch interface. It will dispatch function calls from a FIFO queue, and supports both polling and tail calls, but it doesn't have any scheduling capabilities implemented beyond that (e.g. adaptive concurrency control).

Example

When you start the server, it prints configuration for a local endpoint or stand-alone client:

$ python -m dispatch.test http://127.0.0.1:8000
Spawned a mock Dispatch server on 127.0.0.1:4450 to dispatch
function calls to the endpoint at http://127.0.0.1:8000.

The Dispatch SDK can be configured with:

  export DISPATCH_API_URL="http://127.0.0.1:4450"
  export DISPATCH_API_KEY="test"
  export DISPATCH_ENDPOINT_URL="http://127.0.0.1:8000"
  export DISPATCH_VERIFICATION_KEY="Z+nTe2VRcw8t8Ihx++D+nXtbO28nwjWIOTLRgzrelYs="

By default, a static API key and verification key is used, so that users don't have to reconfigure their endpoints each time.

Here's an example endpoint with one function:

test.py

from fastapi import FastAPI
from dispatch.fastapi import Dispatch


app = FastAPI()
dispatch = Dispatch(app)


@dispatch.function
def myfunction():
    print("Running my function")


print("Dispatching call to my function")
myfunction.dispatch()

If I start the endpoint on the URL expected by the mock Dispatch server, I see that the function was called correctly:

$ uvicorn test:app --port 8000
Dispatching call to my function
...
Running my function

@chriso chriso marked this pull request as ready for review March 14, 2024 02:58
@chriso chriso self-assigned this Mar 14, 2024
@chriso chriso merged commit e0a1b1d into main Mar 15, 2024
3 checks passed
@chriso chriso deleted the test-server branch March 15, 2024 00:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants