Skip to content

feat: auto-detect ASGI mode for @aio decorated functions #387

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

Merged
merged 12 commits into from
Jul 23, 2025

Conversation

taeold
Copy link
Contributor

@taeold taeold commented Jul 22, 2025

Summary

  • Implement automatic ASGI mode detection for functions decorated with @aio.http or @aio.cloud_event
  • The approach creates a Flask app first, loads the module within its context, then checks if ASGI is needed
  • This results in an unused Flask app for ASGI functions, but we accept this memory overhead as a trade-off
  • The --asgi CLI flag still works and skips the Flask app creation for optimization

Implementation Details

  • Added ASGI_FUNCTIONS set to _function_registry.py to track functions that require ASGI
  • Updated @aio.http and @aio.cloud_event decorators to register functions in ASGI_FUNCTIONS
  • Modified create_app() to auto-detect ASGI requirements after module loading:
    1. Always creates a Flask app first
    2. Loads the user module within Flask app context
    3. Checks if target function is in ASGI_FUNCTIONS registry
    4. If ASGI is needed, delegates to create_asgi_app_from_module()
  • The --asgi CLI flag continues to work, bypassing Flask app creation entirely for performance

Trade-offs

  • Memory overhead: ASGI functions will have an unused Flask app instance created during auto-detection
  • Accepted trade-off: This avoids loading modules twice which could cause side effects
  • Optimization available: Users can still use --asgi flag to skip Flask app creation entirely

Test plan

  • Added tests to verify decorators register functions in ASGI_FUNCTIONS
  • Added CLI tests to verify auto-detection works for @aio decorated functions
  • Added CLI tests to verify regular functions still use Flask/WSGI mode
  • Added proper test isolation with registry cleanup fixtures
  • All existing tests pass
  • Linting passes

taeold added 3 commits July 21, 2025 15:38
Functions decorated with @aio.http or @aio.cloud_event now automatically
run in ASGI mode without requiring the --asgi flag. This improves the
developer experience by removing the need to remember to pass the flag
when using async decorators.
@taeold taeold requested a review from maemayve July 22, 2025 15:21
@taeold taeold merged commit ef48e70 into GoogleCloudPlatform:main Jul 23, 2025
51 of 56 checks passed
@taeold taeold deleted the auto-detect-asgi-decorators branch July 23, 2025 00:50
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.

2 participants