Porównaj commity

...

112 Commity
v0.0.9 ... main

Autor SHA1 Wiadomość Data
Carlos Sousa 2ed7ec1403
add all examples to docs (#222) 2025-05-28 16:21:14 -07:00
nazo b370876074
fix #218 (#219) 2025-05-22 16:11:14 -07:00
Ciro Spaciari 464804edef
Update README.md (#205) 2024-11-05 12:26:02 -08:00
Ciro Spaciari d2f26c682c fix wsgi 2024-10-29 12:13:23 -07:00
Kian-Meng Ang 328e249c2c
fix typos again (#203)
Found via `codespell -H`
2024-10-27 12:27:07 -07:00
Ciro Spaciari d73ed582d5 bump + fix asgi 2024-10-23 11:22:29 -07:00
Carlos Sousa 226ae66f6d
fix high cpu consumption in idle mode, update idle_relaxation_time back to 0.01 (#198) 2024-10-11 12:51:52 -07:00
Ciro Spaciari 63b1546bbb
opsie 2024-07-23 08:53:24 -07:00
Ciro Spaciari 90b08b55d3
bump + add get_cookies 2024-07-17 06:29:41 -07:00
Ciro Spaciari 2f9c267e01
tls only with cert_file_name for now 2024-07-13 18:40:57 -07:00
Ciro Spaciari 30cec004e0
fix WSGI query starting with ? 2024-07-13 15:42:07 -07:00
Ciro Spaciari 488f7bd6ac
bump 2024-07-13 13:36:12 -07:00
Ciro Spaciari 0a3b3fa32f
add set_event_loop 2024-07-13 13:32:46 -07:00
Ciro Spaciari 70199e8d7d
add idle config 2024-07-13 12:59:31 -07:00
Ciro Spaciari 833f07b711
new relaxing mode (#192)
Fix: https://github.com/cirospaciari/socketify.py/issues/152
2024-07-13 12:38:20 -07:00
Ciro Spaciari 7ef429ef9f
fix on_start and on_stop (#191)
Fix: https://github.com/cirospaciari/socketify.py/issues/153
Fix: https://github.com/cirospaciari/socketify.py/issues/140
2024-07-13 11:45:29 -07:00
Ciro Spaciari c692e29ba2
fix #187 (#190) 2024-07-13 11:19:33 -07:00
Ciro Spaciari 8ca74b05d8
fix https://github.com/cirospaciari/socketify.py/issues/154 (#189) 2024-07-13 11:14:18 -07:00
Benjamin df212efc27
bugfix: swap options variable name in listen (#180) 2024-07-11 11:55:35 -07:00
leunga1000 2a398a3543
Adding traceback info to the default error log (#176)
* Update socketify.py

* Update socketify.py
2024-06-03 17:04:00 -03:00
leunga1000 293b2a97c2
Update getting-started.md (#175)
Add __name__ == "__main__" clauses and a make_app function so that the app can be run with the CLI too.
2024-05-03 12:32:02 -03:00
Dominik Moritz 2aba41ff7b
Fix typo in routes docs (#165) 2024-02-08 19:29:07 -03:00
Dominik Moritz 2c51d97097
Remove unused import in example (#157) 2024-01-31 15:20:26 -03:00
Mike Piecuch 26709f4086
Fix Response.send_chunk() and helpers.sendfile() (#150) 2023-12-26 14:23:08 -03:00
Will Rieger e6710b6a9e
Update WS Documentation (#145)
* update examples in docs to show upgrade, provide on_ methods

* add clarification
2023-11-29 08:30:51 -03:00
Cody Fincher e8b4c21515
feat: adds RHEL/OEL based Linux install + markdown linting (#138) 2023-09-06 22:33:05 -03:00
cirospaciari 1edf1f7e78 actually fix tasks now 2023-08-22 00:25:04 -03:00
cirospaciari fb1561dd4e add current_task support for tasks.py 2023-08-17 14:30:00 -03:00
cirospaciari 3b28e0edfe bump version 2023-07-25 08:50:17 -03:00
cirospaciari 263ff096c6 fix signal handler 2023-07-25 08:48:58 -03:00
cirospaciari dd9bb7512f fix toml 2023-07-12 18:07:03 -03:00
cirospaciari 037a29f3e2 bump 2023-07-12 17:55:03 -03:00
Ciro Spaciari 4a6629c7fe [GitHub Actions] Updated windows-amd64 binaries 2023-07-12 18:46:11 +00:00
Ciro Spaciari 87db175640
mor libs on windows.yml 2023-07-12 15:35:41 -03:00
Ciro Spaciari 299e36500a
Update windows.yml 2023-07-12 15:19:27 -03:00
Ciro Spaciari dc41c89a39
Update windows.yml 2023-07-12 15:02:20 -03:00
Ciro Spaciari fcea3cf783
debug windows.yml 2023-07-12 14:58:44 -03:00
Ciro Spaciari a259258cd7
Update windows.yml 2023-07-12 14:54:40 -03:00
Ciro Spaciari ce1ee6fa94
Update windows.yml 2023-07-12 14:34:30 -03:00
Ciro Spaciari bd753ae0e2 [GitHub Actions] Updated windows-amd64 binaries 2023-07-12 16:59:10 +00:00
Ciro Spaciari 9060913d49 [GitHub Actions] Updated darwin-arm64 binaries 2023-07-12 16:47:45 +00:00
Ciro Spaciari f2c8ef04b2 [GitHub Actions] Updated darwin-amd64 2023-07-12 16:35:36 +00:00
Ciro Spaciari 005b150d4a [GitHub Actions] Updated linux-amd64 binaries 2023-07-12 16:20:53 +00:00
cirospaciari 68beb7b40f update uWS 2023-07-12 13:05:45 -03:00
Ciro Spaciari ce55fd194b
Update README.md (#130) 2023-07-11 17:56:44 -03:00
Ciro Spaciari 6439307307
Update README.md (#129) 2023-07-11 13:48:02 -03:00
cirospaciari 21c1f0f5fb fix colors 2023-07-07 19:21:04 -03:00
cirospaciari c96c243fd2 fix async wrappers 2023-07-05 11:49:37 -03:00
cirospaciari b3e7b494ab drop 3.7 support because its EOL today 2023-06-27 18:09:13 -03:00
cirospaciari 34f3d04036 expose block option and also close function on WSGI and ASGI, also avoids exceptions on __del__ 2023-06-27 13:55:10 -03:00
cirospaciari 375b5e0d14 support uvloop 2023-06-24 17:12:08 -03:00
cirospaciari a63b75e30a CPU relax strategy 2023-06-23 10:57:00 -03:00
cirospaciari 99e716e657 add empty content body for wsgi.input, fix get_data 2023-06-23 09:13:28 -03:00
Ciro Spaciari 2cda5f7243
Update api.md (#122) 2023-05-12 20:32:22 -03:00
Ziv Lazarov 1d57eff163
Removed html from examples.md and added references to api, _sidebar and README .md files. (#121)
* Created examples.md file in docs and added 4 examples.
added backpressure, middleware, https and broadcast.

* Removed html from examples.md and added referrences.
added examples link to api.md, README.md and _sidebar.md.

* Added next section.
created link to examples.md file from api.md at the bottom.
2023-05-12 20:31:37 -03:00
Ciro 40404519e5 bump version 2023-03-21 13:33:30 -03:00
Ciro 98dad2117a be agressive on loop for now 2023-03-21 13:32:23 -03:00
Ciro Spaciari 41af724854 [GitHub Actions] Updated windows-amd64 binaries 2023-03-19 17:12:18 +00:00
Ciro Spaciari d954bad875 [GitHub Actions] Updated darwin-arm64 binaries 2023-03-19 16:47:49 +00:00
Ciro Spaciari 664b2d9ff1 [GitHub Actions] Updated darwin-amd64 2023-03-19 16:39:32 +00:00
Ciro Spaciari 0de0e99b02 [GitHub Actions] Updated windows-amd64 binaries 2023-03-19 16:34:27 +00:00
Ciro Spaciari 6bf18b7a79 [GitHub Actions] Updated linux-amd64 binaries 2023-03-19 16:21:39 +00:00
Ciro 3afb7ec2f2 fix several bugs, segfault and memory leak 2023-03-19 13:18:29 -03:00
Ciro e0e28263f5 more WSGI stuff 2023-03-14 15:31:40 -03:00
Ciro 18892b729d less pressure on WSGI try_end 2023-03-12 20:47:33 -03:00
Ciro 27cfb3482b fix WSGI try_end 2023-03-12 20:42:37 -03:00
Ciro 3f7c3d4670 Merge branch 'main' of https://github.com/cirospaciari/socketify.py 2023-03-12 19:53:58 -03:00
Ciro Spaciari 819b1f62d6
WSGI Content-Length and Transfer-Encoding (#75)
* first steps to fix wsgi spec

* wip async on_writable send

* Update src/socketify/wsgi.py

Co-authored-by: Eric Carmichael <eric@ckcollab.com>

* fix decorator router

* fix WSGI spec

---------

Co-authored-by: Eric Carmichael <eric@ckcollab.com>
2023-03-12 19:52:56 -03:00
Ciro 043762d141 fix decorator router 2023-03-12 16:52:10 -03:00
Ciro 4744855d64 bomp version 2023-03-12 16:23:50 -03:00
Ciro 8badbc3eb5 fix https://github.com/cirospaciari/socketify.py/issues/105 2023-03-12 16:19:07 -03:00
Ciro 037673c68d fix https://github.com/cirospaciari/socketify.py/issues/105 2023-03-12 16:16:37 -03:00
Ciro 23f6e26d26 fix forks 2023-03-12 16:08:54 -03:00
Ciro 620b853607 bump version 2023-03-10 13:15:57 -03:00
Ciro 7adfb90ccf fix libuv blocking without need 2023-03-10 13:08:34 -03:00
Ciro f006601588 update graphql helper 2023-03-10 12:36:29 -03:00
Ciro a340dda35b fix formdata helper example 2023-03-09 07:17:18 -03:00
Ciro cc10a3e4ba Merge branch 'main' of https://github.com/cirospaciari/socketify.py 2023-03-09 07:15:34 -03:00
Ciro ed8b0eec3a add form_data example 2023-03-09 07:15:28 -03:00
C2D 288c1d7e61
Allow for alternative compiler (#107) 2023-02-27 12:15:27 -03:00
Ciro 81f5b1e4a1 bump version 2023-02-16 14:18:27 -03:00
Ciro 2ae9059e05 preliminar support for Quart, fix ASGI receive behavior, fix custom Task behavior 2023-02-16 14:17:58 -03:00
Ciro f562556844 bump version 2023-02-09 14:00:09 -03:00
Ciro a447904ec7 more 10% to 20% in PyPy async 2023-02-08 20:54:14 -03:00
Ciro f9a76df2f1 async 10 to 20% faster 2023-02-08 15:53:52 -03:00
Ciro ea202f8e4d fix version 2023-02-06 17:15:02 -03:00
Ciro 741cff380e bump version 2023-02-06 17:14:15 -03:00
Ciro 9fdf8daaf5 Merge branch 'main' of https://github.com/cirospaciari/socketify.py 2023-02-06 17:12:48 -03:00
Ciro d2ecb6ccbf update docs/api.md 2023-02-06 17:12:42 -03:00
Ciro ba6d8861b4 add res.close 2023-02-06 17:11:40 -03:00
Ciro Spaciari e904e8aed6 [GitHub Actions] Updated darwin-arm64 binaries 2023-02-06 20:08:21 +00:00
Ciro Spaciari 4794a1a5d6 [GitHub Actions] Updated windows-amd64 binaries 2023-02-06 20:01:29 +00:00
Ciro Spaciari 9b8660db01 [GitHub Actions] Updated darwin-amd64 2023-02-06 19:52:15 +00:00
Ciro Spaciari c72704a420 [GitHub Actions] Updated linux-amd64 binaries 2023-02-06 19:46:52 +00:00
Ciro a246161a54 update uWS, fix ws upgrade with SSL, fix override write, fix typo in CAPI, add res_close 2023-02-06 16:43:02 -03:00
Ciro ebd10964ba update docs README.md 2023-02-06 12:51:31 -03:00
Ciro 6c94ad5c8c update README.md 2023-02-06 12:50:44 -03:00
Ciro Spaciari 0a78f58783 [GitHub Actions] Updated darwin-arm64 binaries 2023-02-06 15:47:55 +00:00
Ciro Spaciari 2cb295c161 [GitHub Actions] Updated darwin-amd64 2023-02-06 15:38:57 +00:00
Ciro Spaciari d32f948e22 [GitHub Actions] Updated windows-amd64 binaries 2023-02-06 15:25:32 +00:00
Ciro Spaciari bdddaedda8 [GitHub Actions] Updated linux-amd64 binaries 2023-02-06 15:16:24 +00:00
Ciro a84dba6eab optimize send a little bit 2023-02-06 12:11:05 -03:00
Ciro f04a5fd642 Merge branch 'main' of https://github.com/cirospaciari/socketify.py 2023-02-02 10:10:23 -03:00
Ciro Spaciari 2f05a44196 [GitHub Actions] Updated darwin-amd64 2023-02-02 13:09:17 +00:00
Ciro Spaciari f1375cf589 [GitHub Actions] Updated darwin-arm64 binaries 2023-02-02 13:03:15 +00:00
Ciro 6abb96824d Merge branch 'main' of https://github.com/cirospaciari/socketify.py 2023-02-02 10:00:29 -03:00
Ciro Spaciari 65e54834e4 [GitHub Actions] Updated windows-amd64 binaries 2023-02-02 12:52:58 +00:00
Ciro cf2e4b04a1 pyproject.toml 2023-02-02 09:45:31 -03:00
Ciro Spaciari 230bb841ff [GitHub Actions] Updated linux-amd64 binaries 2023-02-02 12:43:14 +00:00
Ciro 45ff9caf50 fix HTTP 1.0 in nginx 2023-02-02 09:39:40 -03:00
Ciro 543e83b561 fix ws.clone 2023-01-22 17:03:38 -03:00
Ciro 3fd00f3c4a fix HTTPS 2023-01-21 13:43:48 -03:00
48 zmienionych plików z 2351 dodań i 1113 usunięć

Wyświetl plik

@ -25,7 +25,7 @@ jobs:
cmake -DCMAKE_BUILD_TYPE=Release -GNinja .. && ninja crypto ssl
cd ..\..\..\..\
cl /MD /W3 /D /EHsc /Zc:__cplusplus /Ox /DLL /D_WINDLL /LD /D "NOMINMAX" /D "WIN32_LEAN_AND_MEAN" /D "UWS_NO_ZLIB" /D "UWS_WITH_PROXY" /D "LIBUS_USE_LIBUV" /I native/src/ /I uWebSockets/src /I uWebSockets/capi /I uWebSockets/uSockets/boringssl/include /D "LIBUS_USE_OPENSSL" /std:c++20 /I C:\vcpkg\packages\libuv_x64-windows-static-md\include /I uWebSockets/uSockets/src /Felibsocketify_windows_amd64.dll ./native/src/libsocketify.cpp uWebSockets/uSockets/src/*.c uWebSockets/uSockets/src/crypto/*.cpp uWebSockets/uSockets/src/eventing/*.c uWebSockets/uSockets/src/crypto/*.c advapi32.lib uWebSockets/uSockets/boringssl/amd64/ssl/ssl.lib uWebSockets/uSockets/boringssl/amd64/crypto/crypto.lib C:\vcpkg\installed\x64-windows-static-md\lib\uv_a.lib iphlpapi.lib userenv.lib psapi.lib user32.lib
cl /MD /W3 /D /EHsc /Zc:__cplusplus /Ox /DLL /D_WINDLL /LD /D "NOMINMAX" /D "WIN32_LEAN_AND_MEAN" /D "UWS_NO_ZLIB" /D "UWS_WITH_PROXY" /D "LIBUS_USE_LIBUV" /I native/src/ /I uWebSockets/src /I uWebSockets/capi /I uWebSockets/uSockets/boringssl/include /D "LIBUS_USE_OPENSSL" /std:c++20 /I C:\vcpkg\packages\libuv_x64-windows-static-md\include /I uWebSockets/uSockets/src /Felibsocketify_windows_amd64.dll ./native/src/libsocketify.cpp uWebSockets/uSockets/src/*.c uWebSockets/uSockets/src/crypto/*.cpp uWebSockets/uSockets/src/eventing/*.c uWebSockets/uSockets/src/crypto/*.c advapi32.lib uWebSockets/uSockets/boringssl/amd64/ssl/ssl.lib uWebSockets/uSockets/boringssl/amd64/crypto/crypto.lib C:\vcpkg\installed\x64-windows-static-md\lib\libuv.lib iphlpapi.lib userenv.lib psapi.lib user32.lib shell32.lib dbghelp.lib ole32.lib uuid.lib ws2_32.lib
git add libsocketify_windows_amd64.dll
git config --global user.email "ciro.spaciari@gmail.com"

3
.gitignore vendored
Wyświetl plik

@ -5,4 +5,5 @@ __pycache__
*.o
node_modules/
yarn.lock
.vscode
.vscode
/venv

Wyświetl plik

@ -1,26 +1,26 @@
# socketify.py
<p align="center">
<a href="https://github.com/cirospaciari/socketify.py"><img src="https://raw.githubusercontent.com/cirospaciari/socketify.py/main/misc/logo.png" alt="Logo" height=170></a>
<br />
<br />
<a href="https://github.com/cirospaciari/socketify.py/actions/workflows/linux.yml" target="_blank"><img src="https://github.com/cirospaciari/socketify.py/actions/workflows/linux.yml/badge.svg" /></a>
<a href="https://github.com/cirospaciari/socketify.py/actions/workflows/windows.yml" target="_blank"><img src="https://github.com/cirospaciari/socketify.py/actions/workflows/windows.yml/badge.svg" /></a>
<a href="https://github.com/cirospaciari/socketify.py/actions/workflows/macos.yml" target="_blank"><img src="https://github.com/cirospaciari/socketify.py/actions/workflows/macos.yml/badge.svg" /></a>
<a href="https://github.com/cirospaciari/socketify.py/actions/workflows/macos_arm64.yml" target="_blank"><img src="https://github.com/cirospaciari/socketify.py/actions/workflows/macos_arm64.yml/badge.svg" /></a>
<br/>
<a href='https://github.com/cirospaciari/socketify.py'><img alt='GitHub Clones' src='https://img.shields.io/badge/dynamic/json?color=success&label=Clones&query=count&url=https://gist.githubusercontent.com/cirospaciari/2243d59951f4abe4fd2000f1e20bc561/raw/clone.json&logo=github'></a>
<a href='https://pypi.org/project/socketify/' target="_blank"><img alt='PyPI Downloads' src='https://static.pepy.tech/personalized-badge/socketify?period=total&units=international_system&left_color=grey&right_color=brightgreen&left_text=Downloads'></a>
<a href="https://github.com/sponsors/cirospaciari/" target="_blank"><img src="https://img.shields.io/static/v1?label=Sponsor&message=%E2%9D%A4&logo=GitHub&link=https://github.com/sponsors/cirospaciari"/></a>
<a href='https://discord.socketify.dev/' target="_blank"><img alt='Discord' src='https://img.shields.io/discord/1042529276219641906?label=Discord'></a>
</p>
<div align="center">
<a href="https://docs.socketify.dev">Documentation</a>
<span>&nbsp;&nbsp;&nbsp;&nbsp;</span>
<a href="https://discord.socketify.dev/">Discord</a>
<span>&nbsp;&nbsp;&nbsp;&nbsp;</span>
<a href="https://github.com/cirospaciari/socketify.py/issues">Issues</a>
<span>&nbsp;&nbsp;&nbsp;&nbsp;</span>
<a href="https://github.com/cirospaciari/socketify.py/tree/main/examples">Examples</a>
<br />
</div>
## :bookmark_tabs: Documentation
See the full docs in [docs.socketify.dev](https://docs.socketify.dev) or in [/docs/README.md](docs/README.md)
Also take a look in the examples in [/examples](https://github.com/cirospaciari/socketify.py/tree/main/examples)
## 💡 Features
- WebSocket with pub/sub support
@ -44,6 +44,7 @@ Also take a look in the examples in [/examples](https://github.com/cirospaciari/
- [`Plugins/Extensions`](https://docs.socketify.dev/extensions.html)
## :mag_right: Upcoming Features
- In-Memory Cache Tools
- Fetch like API powered by libuv
- Async file IO powered by libuv
@ -59,6 +60,7 @@ We created and adapted the full C API from [uNetworking/uWebSockets](https://git
Join Github [`Discussions`](https://github.com/cirospaciari/socketify.py/discussions) or [`Discord`](https://discord.socketify.dev/) for help and have a look at the development progress.
## :zap: Benchmarks
Socketify WebFramework HTTP requests per second (Linux x64)
![image](https://raw.githubusercontent.com/cirospaciari/socketify.py/main/misc/bench-bar-graph-general.png)
@ -75,14 +77,14 @@ WebSocket messages per second (Linux x64)
![image](https://raw.githubusercontent.com/cirospaciari/socketify.py/main/misc/bench-bar-graph-websockets.png)
Http tested with TFB tool plaintext benchmark<br/>
WebSocket tested with [Bun.sh](https://bun.sh) bench chat-client <br/>
Source code in [TechEmPower](https://github.com/TechEmpower/FrameworkBenchmarks) and for websockets in [bench](https://github.com/cirospaciari/socketify.py/tree/main/bench)<br/>
Machine OS: Debian GNU/Linux bookworm/sid x86_64 Kernel: 6.0.0-2-amd64 CPU: Intel i7-7700HQ (8) @ 3.800GHz Memory: 32066MiB
Machine OS: Debian GNU/Linux bookworm/sid x86_64 Kernel: 6.0.0-2-amd64 CPU: Intel i7-7700HQ (8) @ 3.800GHz Memory: 32066MiB
## 📦 Installation
For macOS x64 & Silicon, Linux x64, Windows
```bash
@ -94,9 +96,11 @@ pypy3 -m pip install -e socketify
```
Using install via requirements.txt
```text
socketify
```
```bash
pip install -r ./requirements.txt
#or specify PyPy3
@ -106,19 +110,28 @@ pypy3 -m pip install -r ./requirements.txt
If you are using linux or macOS, you may need to install libuv and zlib in your system
macOS
```bash
brew install libuv
brew install zlib
```
Linux
Linux (Ubuntu/Debian)
```bash
apt install libuv1 zlib1g
```
Linux (RHEL/OEL)
```bash
yum install cmake zlib-devel libuv-devel
```
## 🤔 Usage
Hello world app
```python
from socketify import App
@ -129,6 +142,7 @@ app.run()
```
SSL version sample
``` python
from socketify import App, AppOptions
@ -139,8 +153,9 @@ app.run()
```
WebSockets
```python
from socketify import App, AppOptions, OpCode, CompressOptions
from socketify import App, OpCode, CompressOptions
def ws_open(ws):
print('A WebSocket got connected!')
@ -159,7 +174,7 @@ app.ws("/*", {
'message': ws_message,
'drain': lambda ws: print(f'WebSocket backpressure: {ws.get_buffered_amount()}'),
'close': lambda ws, code, message: print('WebSocket closed'),
'subscription': lambda ws, topic, subscriptions, subscriptions_before: print(f'subscription/unsubscription on topic {topic} {subscriptions} {subscriptions_before}'),
'subscription': lambda ws, topic, subscriptions, subscriptions_before: print(f'subscribe/unsubscribe on topic {topic} {subscriptions} {subscriptions_before}'),
})
app.any("/", lambda res,req: res.end("Nothing to see here!'"))
app.listen(3000, lambda config: print("Listening on port http://localhost:%d now\n" % (config.port)))
@ -169,6 +184,7 @@ app.run()
We have more than 20 examples [click here](https://github.com/cirospaciari/socketify.py/tree/main/examples) for more
## :hammer: Building from source
```bash
#clone and update submodules
git clone https://github.com/cirospaciari/socketify.py.git
@ -185,6 +201,7 @@ pypy3 -m pip uninstall socketify
```
## :briefcase: Commercially supported
I'm a Brazilian consulting & contracting company dealing with anything related with [socketify.py](https://github.com/cirospaciari/socketify.py) and [socketify.rb](https://github.com/cirospaciari/socketify.rb)
Don't hesitate sending a mail if you are in need of advice, support, or having other business inquiries in mind. We'll figure out what's best for both parties.
@ -192,6 +209,7 @@ Don't hesitate sending a mail if you are in need of advice, support, or having o
Special thank's to [uNetworking AB](https://github.com/uNetworking) to develop [uWebSockets](https://github.com/uNetworking/uWebSockets), [uSockets](https://github.com/uNetworking/uSockets) and allow us to bring this features and performance to Python and PyPy
## :heart: Sponsors
If you like to see this project thrive, you can sponsor us on GitHub too. We need all the help we can get.
Thank you [`Otavio Augusto`](https://github.com/middlebaws) to be the first sponsor of this project!
@ -199,21 +217,17 @@ Thank you [`Otavio Augusto`](https://github.com/middlebaws) to be the first spon
<a href="https://github.com/sponsors/cirospaciari/" target="_blank"><img src="https://img.shields.io/static/v1?label=Sponsor&message=%E2%9D%A4&logo=GitHub&link=https://github.com/sponsors/cirospaciari"/></a>
## :star: Stargazers
[![Stargazers repo roster for @cirospaciari/socketify.py](https://reporoster.com/stars/dark/cirospaciari/socketify.py)](https://github.com/cirospaciari/socketify.py/stargazers)
## :wrench: Forkers
[![Forkers repo roster for @cirospaciari/socketify.py](https://reporoster.com/forks/dark/cirospaciari/socketify.py)](https://github.com/cirospaciari/socketify.py/network/members)
## :question: socketify.py vs japronto
People really want to compare with japronto, but this projects are not really comparable. Socketify is an active project and will be maintained over time with security updates and new features, japronto don't get any github updates since 2020 and don't get any src update since 2018, japronto don't support SSL, WebSockets, [`PyPy3`](https://www.pypy.org/), Windows or macOS Silicon, socketify will support Http3 and a lot more features.
And yes, we can be faster than japronto when all our features and goals are achieved, and we are probably faster than any current maintained solution out there.
## :grey_question: uvloop
We don't use uvloop, because uvloop don't support Windows and PyPy3 at this moment, this can change in the future, but right now we want to implement our own libuv + asyncio solution, and a lot more.
## :dizzy: CFFI vs Cython vs HPy
Cython performs really well on Python3 but really bad on PyPy3, CFFI are chosen for better support PyPy3 until we got our hands on an stable [`HPy`](https://hpyproject.org/) integration.
Cython performs really well on Python3 but really bad on PyPy3, CFFI are chosen for better support PyPy3 until we got our hands on a stable [`HPy`](https://hpyproject.org/) integration.

Wyświetl plik

@ -15,7 +15,7 @@ class SSGIHttpResponse:
# send chunk of data, can be used to perform with less backpressure than using send
# total_size is the sum of all lengths in bytes of all chunks to be sended
# connection will end when total_size is met
# returns tuple(bool, bool) first bool represents if the chunk is succefully sended, the second if the connection has ended
# returns tuple(bool, bool) first bool represents if the chunk is successfully sended, the second if the connection has ended
def send_chunk(self, chunk: Union[str, bytes, bytearray, memoryview], total_size: int = False) -> Awaitable:
pass

Wyświetl plik

@ -8,11 +8,10 @@ class Home:
resp.content_type = falcon.MEDIA_TEXT # Default is JSON, so override
resp.text = "Hello, World!"
def on_post(self, req, resp):
raw_data = req.stream.getvalue()
print("data", raw_data)
raw_data = req.stream.read()
resp.status = falcon.HTTP_200 # This is the default status
resp.content_type = falcon.MEDIA_TEXT # Default is JSON, so override
resp.text = raw_data
resp.text = 'Ok'
@ -23,4 +22,4 @@ home = Home()
app.add_route("/", home)
if __name__ == "__main__":
WSGI(app).listen(8000, lambda config: print(f"Listening on port http://localhost:{config.port} now\n")).run(workers=8)
WSGI(app).listen(8000, lambda config: print(f"Listening on port http://localhost:{config.port} now\n")).run(workers=1)

Wyświetl plik

@ -1,6 +1,5 @@
from socketify import ASGI
async def app(scope, receive, send):
assert scope['type'] == 'http'
@ -20,4 +19,4 @@ async def app(scope, receive, send):
if __name__ == "__main__":
ASGI(app).listen(8000, lambda config: print(f"Listening on port http://localhost:{config.port} now\n")).run(8)
ASGI(app, lifespan=False).listen(8000, lambda config: print(f"Listening on port http://localhost:{config.port} now\n")).run(1)

Wyświetl plik

@ -1,8 +1,70 @@
from socketify import WSGI
payload = None
with open("xml.zip", "rb") as file:
payload = file.read()
chunk_size = 64 * 1024
content_length = len(payload)
def app_chunked(environ, start_response):
start_response('200 OK', [('Content-Type', 'application/zip'), ('Transfer-Encoding', 'chunked')])
sended = 0
while content_length > sended:
end = sended + chunk_size
yield payload[sended:end]
sended = end
def app(environ, start_response):
start_response('200 OK', [('Content-Type', 'application/zip'), ('Content-Length', str(content_length))])
sended = 0
while content_length > sended:
end = sended + chunk_size
yield payload[sended:end]
sended = end
# import gc
# gc.collect()
# gc.set_threshold(50, 3, 3)
# import tracemalloc
# tracemalloc.start()
def app_hello(environ, start_response):
# start_response('200 OK', [('Content-Type', 'text/plain'), ('Content-Length', '13')])
start_response('200 OK', [('Content-Type', 'text/plain')])
yield b'Hello, World!\n'
return [ b'Hello, World!']
if __name__ == "__main__":
WSGI(app).listen(8000, lambda config: print(f"Listening on port http://localhost:{config.port} now\n")).run(8)
# import fastwsgi
# fastwsgi.run(wsgi_app=app_hello, host='127.0.0.1', port=8000, loglevel=0)
# from meinheld import server
# server.listen(("0.0.0.0", 8000))
# server.run(app_hello)
from socketify import WSGI
WSGI(app_hello).listen(8000, lambda config: print(f"Listening on port http://localhost:{config.port} now\n")).run(1)
# def run_app():
# import fastwsgi
# fastwsgi.run(wsgi_app=app_hello, host='127.0.0.1', port=8000)
# import os
# pid_list = []
# # fork limiting the cpu count - 1
# for _ in range(1, 8):
# pid = os.fork()
# # n greater than 0 means parent process
# if not pid > 0:
# run_app()
# break
# pid_list.append(pid)
# run_app() # run app on the main process too :)
# # sigint everything to graceful shutdown
# import signal
# for pid in pid_list:
# os.kill(pid, signal.SIGINT)

Wyświetl plik

@ -0,0 +1,4 @@
while true
do
wrk -t1 -c200 -d1 -H 'Connection: keep-alive' http://127.0.0.1:8000 > /dev/null
done

Wyświetl plik

@ -0,0 +1,83 @@
import sys
import io
import time
import datetime
import socket
import optparse
parser = optparse.OptionParser("usage: %prog [options]", add_help_option=False)
parser.add_option("-h", "--host", dest="host", default='127.0.0.1', type="string")
parser.add_option("-p", "--port", dest="port", default=3000, type="int")
(opt, args) = parser.parse_args()
def get_request(path = r'/', host = '127.0.0.1', port = 3000):
req = f'GET {path}' + r' HTTP/1.1' + '\r\n'
req += f'Host: {host}:{port}\r\n'
req += r'User-Agent: curl/7.66.0' + '\r\n'
req += r'Accept: */*' + '\r\n'
req += '\r\n'
return req
payload_tiny = get_request(host = opt.host, port = opt.port)
payload_tiny = payload_tiny.encode('utf-8')
def create_sock(timeout = 0.001):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(timeout)
sock.connect((opt.host, opt.port))
return sock
sock = create_sock()
sock.sendall(payload_tiny)
time.sleep(0.020)
resp = sock.recv(4096)
print('====== response ========')
print(resp.decode('utf-8'))
print('========================')
sock.close()
start_time = datetime.datetime.now()
test1_limit = start_time + datetime.timedelta(seconds = 1)
test2_limit = test1_limit + datetime.timedelta(seconds = 10)
sock = create_sock()
while True:
if datetime.datetime.now() >= test1_limit:
break
sock.sendall(payload_tiny)
try:
resp = sock.recv(4096)
except socket.timeout:
pass
print(f'Test 1 completed!')
sock.close()
req_num = 1000*1000
payload_huge = payload_tiny * req_num
#print(len(payload_huge))
print(f'Run test 2 ...')
totalsent = 0
totalresp = b''
sock = create_sock()
while True:
if datetime.datetime.now() >= test2_limit:
print(f'Test 2: Timeout exceeded!')
break
try:
rc = sock.send(payload_huge[totalsent:])
if rc == 0:
#raise RuntimeError("socket connection broken")
pass
totalsent += rc
resp = sock.recv(65*1024)
totalresp += resp
except socket.timeout:
pass
except ConnectionResetError:
print(f'totalsent = {totalsent}, totalrecv = {len(totalresp)}')
print(f'LastResp: {totalresp[-256:]}')
raise
sock.close()
print("==== Test Finish =====")

Wyświetl plik

@ -0,0 +1,12 @@
#!/usr/bin/env python3
from quart import Quart
app = Quart(__name__)
@app.get("/")
async def plaintext():
return "Hello, World!", {"Content-Type": "text/plain"}
# Quart perform really baddly for sure needs more optimizations, but socketify ASGI + PyPy performs better than uvicorn+httptools+gunicorn

Wyświetl plik

@ -2,12 +2,15 @@ from socketify import App
import os
import multiprocessing
import asyncio
def run_app():
app = App(request_response_factory_max_items=200_000)
router = app.router()
@router.get("/")
async def home(res, req):
res.end("Hello, World!")
app.get("/", home)
res.send(b"Hello, World!")
app.listen(
8000,
lambda config: print(
@ -26,7 +29,7 @@ def create_fork():
# fork limiting the cpu count - 1
for i in range(1, multiprocessing.cpu_count()):
create_fork()
# for i in range(1, multiprocessing.cpu_count()):
# create_fork()
run_app() # run app on the main process too :)

Wyświetl plik

@ -11,12 +11,22 @@
<a href="https://github.com/cirospaciari/socketify.py/actions/workflows/macos_arm64.yml" target="_blank"><img src="https://github.com/cirospaciari/socketify.py/actions/workflows/macos_arm64.yml/badge.svg" /></a>
<br/>
<a href='https://github.com/cirospaciari/socketify.py'><img alt='GitHub Clones' src='https://img.shields.io/badge/dynamic/json?color=success&label=Clones&query=count&url=https://gist.githubusercontent.com/cirospaciari/2243d59951f4abe4fd2000f1e20bc561/raw/clone.json&logo=github'></a>
<a href='https://pypi.org/project/socketify/' target="_blank"><img alt='PyPI Downloads' src='https://static.pepy.tech/personalized-badge/socketify?period=total&units=international_system&left_color=grey&right_color=brightgreen&left_text=Downloads'></a>
<a href="https://github.com/sponsors/cirospaciari/" target="_blank"><img src="https://img.shields.io/static/v1?label=Sponsor&message=%E2%9D%A4&logo=GitHub&link=https://github.com/sponsors/cirospaciari"/></a>
<a href='https://discord.socketify.dev/' target="_blank"><img alt='Discord' src='https://img.shields.io/discord/1042529276219641906?label=Discord'></a>
</p>
<br/>
<div align="center">
<a href="https://github.com/cirospaciari/socketify.py">Github</a>
<span>&nbsp;&nbsp;&nbsp;&nbsp;</span>
<a href="https://discord.socketify.dev/">Discord</a>
<span>&nbsp;&nbsp;&nbsp;&nbsp;</span>
<a href="https://github.com/cirospaciari/socketify.py/issues">Issues</a>
<span>&nbsp;&nbsp;&nbsp;&nbsp;</span>
<a href="https://github.com/cirospaciari/socketify.py/tree/main/examples">Examples</a>
<br />
</div>
<br/>
Socketify.py is a reliable, high-performance Python web framework for building large-scale app backends and microservices.
With no precedents websocket performance and a really fast HTTP server that can delivery encrypted TLS 1.3 quicker than most alternative servers can do even unencrypted, cleartext messaging.
@ -38,3 +48,4 @@ With no precedents websocket performance and a really fast HTTP server that can
- [SSL](ssl.md)
- [CLI, ASGI and WSGI](cli.md)
- [API Reference](api.md)
- [Examples](examples.md)

Wyświetl plik

@ -16,3 +16,4 @@
- [SSL](ssl.md)
- [CLI, ASGI and WSGI](cli.md)
- [API Reference](api.md)
- [Examples](examples.md)

Wyświetl plik

@ -38,11 +38,12 @@ class App:
```
## AppResponse
## Response
```python
class AppResponse:
class Response:
def __init__(self, response, app):
def cork(self, callback):
def close(self):
def set_cookie(self, name, value, options={}):
def run_async(self, task):
async def get_form_urlencoded(self, encoding="utf-8"):
@ -61,7 +62,7 @@ class AppResponse:
def get_proxied_remote_address_bytes(self):
def get_proxied_remote_address(self):
def cork_send(self, message: any, content_type: str = b'text/plain', status : str | bytes | int = b'200 OK', headers=None, end_connection=False):
def send(self, message: any, content_type: str = b'text/plain', status : str | bytes | int = b'200 OK', headers=None, end_connection=False):
def send(self, message: any = b"", content_type: str = b'text/plain', status : str | bytes | int = b'200 OK', headers=None, end_connection=False):
def end(self, message, end_connection=False):
def pause(self):
def resume(self):
@ -87,9 +88,9 @@ class AppResponse:
def __del__(self):
```
## AppRequest
## Request
```python
class AppRequest:
class Request:
def __init__(self, request, app):
def get_cookie(self, name):
def get_url(self):
@ -228,4 +229,6 @@ class MiddlewareRouter:
def connect(self, path, handler):
def trace(self, path, handler):
def any(self, path, handler):
```
```
### Next [Examples](examples.md)

Wyświetl plik

@ -30,7 +30,7 @@ Options:
--ws-close-on-backpressure-limit BOOLEAN Close connections that hits maximum backpressure [default: False]
--lifespan [auto|on|off] Lifespan implementation. [default: auto]
--interface [auto|asgi|asgi3|wsgi|ssgi|socketify] Select ASGI (same as ASGI3), ASGI3, WSGI or SSGI as the application interface. [default: auto]
--disable-listen-log BOOLEAN Disable log when start listenning [default: False]
--disable-listen-log BOOLEAN Disable log when start listening [default: False]
--version or -v Display the socketify.py version and exit.
--ssl-keyfile TEXT SSL key file
--ssl-certfile TEXT SSL certificate file

81
docs/examples.md 100644
Wyświetl plik

@ -0,0 +1,81 @@
## 📚 Examples
All examples are located in the [`examples`](https://github.com/cirospaciari/socketify.py/tree/main/examples) directory.
### 🚀 Getting Started
- [`hello_world.py`](https://github.com/cirospaciari/socketify.py/tree/main/examples/hello_world.py) - Basic HTTP server setup
- [`hello_world_cli.py`](https://github.com/cirospaciari/socketify.py/tree/main/examples/hello_world_cli.py) - Command-line interface example
- [`hello_world_cli_ws.py`](https://github.com/cirospaciari/socketify.py/tree/main/examples/hello_world_cli_ws.py) - CLI with WebSocket support
- [`hello_world_unix_domain.py`](https://github.com/cirospaciari/socketify.py/tree/main/examples/hello_world_unix_domain.py) - Unix domain socket example
### 🔒 Security & HTTPS
- [`https.py`](https://github.com/cirospaciari/socketify.py/tree/main/examples/https.py) - HTTPS server with SSL/TLS configuration
### 🌐 WebSocket Examples
- [`websockets.py`](https://github.com/cirospaciari/socketify.py/tree/main/examples/websockets.py) - Basic WebSocket implementation
- [`ws_close_connection.py`](https://github.com/cirospaciari/socketify.py/tree/main/examples/ws_close_connection.py) - WebSocket connection management
- [`chat/`](https://github.com/cirospaciari/socketify.py/tree/main/examples/chat) - Real-time chat application
- [`broadcast.py`](https://github.com/cirospaciari/socketify.py/tree/main/examples/broadcast.py) - Broadcasting messages to multiple clients
- [`backpressure.py`](https://github.com/cirospaciari/socketify.py/tree/main/examples/backpressure.py) - Handling WebSocket backpressure
### ⚙️ Middleware & Routing
- [`middleware.py`](https://github.com/cirospaciari/socketify.py/tree/main/examples/middleware.py) - Basic middleware implementation
- [`middleware_async.py`](https://github.com/cirospaciari/socketify.py/tree/main/examples/middleware_async.py) - Asynchronous middleware
- [`middleware_sync.py`](https://github.com/cirospaciari/socketify.py/tree/main/examples/middleware_sync.py) - Synchronous middleware
- [`middleware_router.py`](https://github.com/cirospaciari/socketify.py/tree/main/examples/middleware_router.py) - Router-based middleware
- [`router_and_basics.py`](https://github.com/cirospaciari/socketify.py/tree/main/examples/router_and_basics.py) - Routing fundamentals
### 🔄 Async/Sync Programming
- [`async.py`](https://github.com/cirospaciari/socketify.py/tree/main/examples/async.py) - Asynchronous request handling
- [`upgrade.py`](https://github.com/cirospaciari/socketify.py/tree/main/examples/upgrade.py) - Protocol upgrade examples
- [`upgrade_async.py`](https://github.com/cirospaciari/socketify.py/tree/main/examples/upgrade_async.py) - Asynchronous protocol upgrades
### 📁 File Handling & Static Content
- [`static_files.py`](https://github.com/cirospaciari/socketify.py/tree/main/examples/static_files.py) - Serving static files
- [`file_stream.py`](https://github.com/cirospaciari/socketify.py/tree/main/examples/file_stream.py) - File streaming capabilities
- [`upload_or_post.py`](https://github.com/cirospaciari/socketify.py/tree/main/examples/upload_or_post.py) - File uploads and POST data handling
### 🎨 Template Engines
- [`template_jinja2.py`](https://github.com/cirospaciari/socketify.py/tree/main/examples/template_jinja2.py) - Jinja2 template integration
- [`template_mako.py`](https://github.com/cirospaciari/socketify.py/tree/main/examples/template_mako.py) - Mako template integration
- [`templates/`](https://github.com/cirospaciari/socketify.py/tree/main/examples/templates) - Template examples and resources
### 🛠️ Advanced Features
- [`custom_json_serializer.py`](https://github.com/cirospaciari/socketify.py/tree/main/examples/custom_json_serializer.py) - Custom JSON serialization
- [`http_request_cache.py`](https://github.com/cirospaciari/socketify.py/tree/main/examples/http_request_cache.py) - HTTP request caching
- [`proxy.py`](https://github.com/cirospaciari/socketify.py/tree/main/examples/proxy.py) - Proxy server implementation
- [`automatic_port_selection.py`](https://github.com/cirospaciari/socketify.py/tree/main/examples/automatic_port_selection.py) - Dynamic port selection
### 🔧 Server Configuration
- [`listen_options.py`](https://github.com/cirospaciari/socketify.py/tree/main/examples/listen_options.py) - Server listening options
- [`graceful_shutdown.py`](https://github.com/cirospaciari/socketify.py/tree/main/examples/graceful_shutdown.py) - Graceful server shutdown
- [`forks.py`](https://github.com/cirospaciari/socketify.py/tree/main/examples/forks.py) - Multi-process server setup
### 📊 GraphQL Integration
- [`graphiql.py`](https://github.com/cirospaciari/socketify.py/tree/main/examples/graphiql.py) - GraphiQL interface setup
- [`graphiql_raw.py`](https://github.com/cirospaciari/socketify.py/tree/main/examples/graphiql_raw.py) - Raw GraphQL implementation
### 🐳 Development & Deployment
- [`docker/`](https://github.com/cirospaciari/socketify.py/tree/main/examples/docker) - Docker containerization examples
- [`requirements.txt`](https://github.com/cirospaciari/socketify.py/tree/main/examples/requirements.txt) - Example dependencies
### 🛡️ Error Handling & Logging
- [`error_handler.py`](https://github.com/cirospaciari/socketify.py/tree/main/examples/error_handler.py) - Error handling strategies
- [`better_logging.py`](https://github.com/cirospaciari/socketify.py/tree/main/examples/better_logging.py) - Advanced logging setup
- [`not_found.py`](https://github.com/cirospaciari/socketify.py/tree/main/examples/not_found.py) - Custom 404 error pages
### 🔨 Utilities & Helpers
- [`helpers/`](https://github.com/cirospaciari/socketify.py/tree/main/examples/helpers) - Utility functions and helper modules

Wyświetl plik

@ -8,10 +8,14 @@ Hello world app
```python
from socketify import App
app = App()
app.get("/", lambda res, req: res.end("Hello World socketify from Python!"))
app.listen(3000, lambda config: print("Listening on port http://localhost:%d now\n" % config.port))
app.run()
def make_app(app: App):
app.get("/", lambda res, req: res.end("Hello World socketify from Python!"))
if __name__ == "__main__":
app = App()
make_app(app)
app.listen(3000, lambda config: print("Listening on port http://localhost:%d now\n" % config.port))
app.run()
```
> This example just show how intuitive is to start an simple hello world app.
@ -19,10 +23,13 @@ SSL version sample
``` python
from socketify import App, AppOptions
app = App(AppOptions(key_file_name="./misc/key.pem", cert_file_name="./misc/cert.pem", passphrase="1234"))
app.get("/", lambda res, req: res.end("Hello World socketify from Python!"))
app.listen(3000, lambda config: print("Listening on port http://localhost:%d now\n" % config.port))
app.run()
def make_app(app):
app.get("/", lambda res, req: res.end("Hello World socketify from Python!"))
if __name__ == "__main__":
app = App(AppOptions(key_file_name="./misc/key.pem", cert_file_name="./misc/cert.pem", passphrase="1234"))
app.listen(3000, lambda config: print("Listening on port http://localhost:%d now\n" % config.port))
app.run()
```
> We have a lot of SSL options, but this is the most common you can see all the options in the [API Reference](api.md)
@ -38,20 +45,25 @@ def ws_open(ws):
def ws_message(ws, message, opcode):
#Ok is false if backpressure was built up, wait for drain
ok = ws.send(message, opcode)
app = App()
app.ws("/*", {
'compression': CompressOptions.SHARED_COMPRESSOR,
'max_payload_length': 16 * 1024 * 1024,
'idle_timeout': 12,
'open': ws_open,
'message': ws_message,
'drain': lambda ws: print('WebSocket backpressure: %i' % ws.get_buffered_amount()),
'close': lambda ws, code, message: print('WebSocket closed')
})
app.any("/", lambda res,req: res.end("Nothing to see here!'"))
app.listen(3000, lambda config: print("Listening on port http://localhost:%d now\n" % (config.port)))
app.run()
def make_app(app):
app.ws("/*", {
'compression': CompressOptions.SHARED_COMPRESSOR,
'max_payload_length': 16 * 1024 * 1024,
'idle_timeout': 12,
'open': ws_open,
'message': ws_message,
'drain': lambda ws: print('WebSocket backpressure: %i' % ws.get_buffered_amount()),
'close': lambda ws, code, message: print('WebSocket closed')
})
app.any("/", lambda res,req: res.end("Nothing to see here!'"))
if __name__ == "__main__":
app = App()
make_app(app)
app.listen(3000, lambda config: print("Listening on port http://localhost:%d now\n" % (config.port)))
app.run()
```
> We can have multiple routes for WebSockets, but in this example we just get one for anything we need, adding an option of compression using SHARED_COMPRESSOR, max_payload_length of 1mb and an idle timeout of 12s just to show some most commonly used features you can see all these options in the [API Reference](api.md)
@ -59,4 +71,4 @@ app.run()
If you just wanna to see some more examples you can go to our [examples folder](https://github.com/cirospaciari/socketify.py/tree/main/examples) for more than 25 quick examples.
### Next [Corking Concept](corking.md)
### Next [Corking Concept](corking.md)

Wyświetl plik

@ -102,7 +102,7 @@ async def async_xablau(res, req):
# this can be async no problems
def on_error(error, res, req):
# here you can log properly the error and do a pretty response to your clients
print("Somethind goes %s" % str(error))
print("Something goes %s" % str(error))
# response and request can be None if the error is in an async function
if res != None:
# if response exists try to send something
@ -164,4 +164,4 @@ app.remove_server_name("*.google.*")
```
### Next [Middlewares](middlewares.md)
### Next [Middlewares](middlewares.md)

Wyświetl plik

@ -3,6 +3,52 @@ WebSocket "routes" are registered similarly, but not identically.
Every websocket route has the same pattern and pattern matching as for Http, but instead of one single callback you have a whole set of them, here's an example:
Configuration details, notes:
- *idle_timeout*: number of seconds of inactivity before client is disconnected. If set to 0, no policy is enforced (connections can be stale).
- *open*: callback function for websocket connection being open
```python
def on_open(ws : WebSocket):
"""
ws: WebSocket - websocket connection
"""
...
```
- *close*: callback function for websocket connection closed
```python
def on_close(ws: WebSocket, code: int, msg: Union[bytes, str]):
"""
ws: WebSocket
websocket connection
code: int
exit code from client
msg: byte, str
exit message
"""
...
```
- *upgrade*: callback function to upgrade socket connection details
```python
def on_upgrade(res: Response, req: Request, socket_context):
"""
res: Response
req: Request
"""
...
```
- *message*: callback function for websocket message received
```python
def on_message(ws: WebSocket, msg: Union[bytes, str], opcode: OpCode):
"""
ws: WebSocket
msg: bytes, str
opcode: OpCode
"""
```
- *drain*: in the event of backpressure, policy to drain ws buffer
```python
def on_drain(ws: WebSocket):
...
```
```python
app = App()
app.ws(
@ -11,10 +57,11 @@ app.ws(
"compression": CompressOptions.SHARED_COMPRESSOR,
"max_payload_length": 16 * 1024 * 1024,
"idle_timeout": 12,
"open": ws_open,
"message": ws_message,
'drain': lambda ws: print(f'WebSocket backpressure: {ws.get_buffered_amount()}'),
"close": lambda ws, code, message: print("WebSocket closed"),
"open": on_open,
"message": on_message,
"close": on_close,
"upgrade": on_upgrade,
'drain': on_drain,
"subscription": lambda ws, topic, subscriptions, subscriptions_before: print(f'subscription/unsubscription on topic {topic} {subscriptions} {subscriptions_before}'),
},
)
@ -24,6 +71,57 @@ You should use the provided user data feature to store and attach any per-socket
If you want to create something more elaborate you could have the user data hold a pointer to some dynamically allocated memory block that keeps a boolean whether the WebSocket is still valid or not. Sky is the limit here.
In order to do so, use the `upgrade` callback configuration in the `app.ws` settings.
Example:
```python
from socketify import App, WebSocket, OpCode
app = App()
ID = 0
def on_open(ws: WebSocket):
user_data = ws.get_user_data()
print('ws %s connected' % user_data['user_id'])
ws.send('Hello, world!')
def on_upgrade(res, req, socket_context):
global ID
ID += 1
key = req.get_header("sec-websocket-key")
protocol = req.get_header("sec-websocket-protocol")
extensions = req.get_header("sec-websocket-extensions")
user_data=dict(user_id=ID)
res.upgrade(key, protocol, extensions, socket_context, user_data)
def on_message(ws: WebSocket, msg: str, opcode: OpCode):
user_data = ws.get_user_data()
print('ws %s: %s' % (user_data['user_id'], msg))
def on_close(ws, code, msg):
user_data = ws.get_user_data()
print('ws %s closed' % user_data['user_id'])
def on_drain(ws: WebSocket):
user_data = ws.get_user_data()
print('ws %s backpressure: %s' % (user_data['user_id'], ws.get_buffered_amount()))
app.ws(
"/*",
{
"compression": CompressOptions.SHARED_COMPRESSOR,
"max_payload_length": 16 * 1024 * 1024,
"idle_timeout": 12,
"open": on_open,
"message": on_message,
"close": on_close,
"upgrade": on_upgrade,
"drain": on_drain,
"subscription": lambda ws, topic, subscriptions, subscriptions_before: print(f'subscription/unsubscription on topic {topic} {subscriptions} {subscriptions_before}'),
}
)
```
## WebSockets are valid from open to close
All given WebSocket are guaranteed to live from open event (where you got your WebSocket) until close event is called.
Message events will never emit outside of open/close. Calling ws.close or ws.end will immediately call the close handler.

Wyświetl plik

@ -2,7 +2,6 @@ from socketify import App
import os
import multiprocessing
def run_app():
app = App()
app.get("/", lambda res, req: res.end("Hello, World!"))
@ -16,15 +15,19 @@ def run_app():
app.run()
def create_fork():
n = os.fork()
# n greater than 0 means parent process
if not n > 0:
run_app()
pid_list = []
# fork limiting the cpu count - 1
for i in range(1, multiprocessing.cpu_count()):
create_fork()
for _ in range(1, multiprocessing.cpu_count()):
pid = os.fork()
# n greater than 0 means parent process
if not pid > 0:
run_app()
break
pid_list.append(pid)
run_app() # run app on the main process too :)
# sigint everything to graceful shutdown
import signal
for pid in pid_list:
os.kill(pid, signal.SIGINT)

Wyświetl plik

@ -0,0 +1,21 @@
from streaming_form_data import StreamingFormDataParser
from socketify import Response
def get_formdata(res: Response, parser: StreamingFormDataParser):
_dataFuture = res.app.loop.create_future()
def is_aborted(res):
res.aborted = True
try:
if not _dataFuture.done():
_dataFuture.set_result(parser)
except:
pass
def get_chunks(res, chunk, is_end):
parser.data_received(chunk)
if is_end:
_dataFuture.set_result(parser)
res.on_aborted(is_aborted)
res.on_data(get_chunks)
return _dataFuture

Wyświetl plik

@ -1,6 +1,6 @@
import strawberry
import strawberry.utils.graphiql
from io import BytesIO
def graphiql_from(Query, Mutation=None):
if Mutation:
@ -8,32 +8,45 @@ def graphiql_from(Query, Mutation=None):
else:
schema = strawberry.Schema(Query)
async def post(res, req):
def post(res, req):
# we can pass whatever we want to context, query, headers or params, cookies etc
context_value = req.preserve()
buffer = BytesIO()
def on_data(res, chunk, is_end):
buffer.write(chunk)
if is_end:
try:
body = res.app._json_serializer.loads(buffer.getvalue().decode("utf-8"))
res.run_async(graph_ql(res, body, context_value))
except Exception as err:
res.app.trigger_error(err, res, None)
res.grab_aborted_handler()
res.on_data(on_data)
# get all incoming data and parses as json
body = await res.get_json()
async def graph_ql(res, body, context_value):
query = body["query"]
variables = body.get("variables", None)
root_value = body.get("rootValue", None)
operation_name = body.get("operationName", None)
data = await schema.execute(
query,
variables,
context_value,
root_value,
operation_name,
)
query = body["query"]
variables = body.get("variables", None)
root_value = body.get("root_value", None)
operation_name = body.get("operation_name", None)
data = await schema.execute(
query,
variables,
context_value,
root_value,
operation_name,
)
res.cork_end(
{
"data": (data.data),
**({"errors": data.errors} if data.errors else {}),
**({"extensions": data.extensions} if data.extensions else {}),
}
)
res.cork_send(
{
"data": (data.data),
**({"errors": data.errors} if data.errors else {}),
**({"extensions": data.extensions} if data.extensions else {}),
}
)
return post

Wyświetl plik

@ -9,9 +9,10 @@ app = App(
)
app.get("/", lambda res, req: res.end("Hello World socketify from Python!"))
app.listen(
3000,
54321,
lambda config: print("Listening on port https://localhost:%d now\n" % config.port),
)
app.run()
# mkdir misc
# openssl req -newkey rsa:2048 -new -nodes -x509 -days 3650 -passout pass:1234 -keyout ./misc/key.pem -out ./misc/cert.pem

Wyświetl plik

@ -6,7 +6,7 @@ from socketify import App
def middleware(*functions):
def middleware_route(res, req):
data = None
# cicle to all middlewares
# circle to all middlewares
for function in functions:
# call middlewares
data = function(res, req, data)

Wyświetl plik

@ -6,8 +6,8 @@
# using oha -c 400 -z 5s http://localhost:3000/
# nginx - try_files - 77630.15 req/s
# pypy3 - socketify static - 15839.22 req/s
# python3 - socketify static - 8294.96 req/s
# pypy3 - socketify static - 16797.30 req/s
# python3 - socketify static - 10140.19 req/s
# node.js - @fastify/static - 5437.16 req/s
# node.js - express.static - 4077.49 req/s
# python3 - socketify static_aiofile - 2390.96 req/s

Wyświetl plik

@ -78,6 +78,69 @@ async def upload_multiple(res, req):
# We respond when we are done
res.cork_end("Thanks for the data!")
def upload_formdata(res, req):
# using streaming_form_data package for parsing
from streaming_form_data import StreamingFormDataParser
from streaming_form_data.targets import ValueTarget, FileTarget
print(f"Posted to {req.get_url()}")
parser = StreamingFormDataParser(headers=req.get_headers())
name = ValueTarget()
parser.register('name', name)
file = FileTarget('/tmp/file')
file2 = FileTarget('/tmp/file2')
parser.register('file', file)
parser.register('file2', file2)
def on_data(res, chunk, is_end):
parser.data_received(chunk)
if is_end:
res.cork(on_finish)
def on_finish(res):
print(name.value)
print(file.multipart_filename)
print(file.multipart_content_type)
print(file2.multipart_filename)
print(file2.multipart_content_type)
res.end("Thanks for the data!")
res.on_data(on_data)
async def upload_formhelper(res, req):
# using streaming_form_data package for parsing + helper
from streaming_form_data import StreamingFormDataParser
from streaming_form_data.targets import ValueTarget, FileTarget
from helpers.form_data import get_formdata
print(f"Posted to {req.get_url()}")
parser = StreamingFormDataParser(headers=req.get_headers())
name = ValueTarget()
parser.register('name', name)
file = FileTarget('/tmp/file')
file2 = FileTarget('/tmp/file2')
parser.register('file', file)
parser.register('file2', file2)
await get_formdata(res, parser)
print(name.value)
print(file.multipart_filename)
print(file.multipart_content_type)
print(file2.multipart_filename)
print(file2.multipart_content_type)
res.cork_end("Thanks for the data!")
app = App()
app.post("/", upload)
@ -86,6 +149,8 @@ app.post("/json", upload_json)
app.post("/text", upload_text)
app.post("/urlencoded", upload_urlencoded)
app.post("/multiple", upload_multiple)
app.post("/formdata", upload_formdata)
app.post("/formdata2", upload_formhelper)
app.any("/*", lambda res, _: res.write_status(404).end("Not Found"))
app.listen(

Wyświetl plik

@ -4,13 +4,14 @@ build-backend = "setuptools.build_meta"
[project]
name = "socketify"
version = "0.0.9"
version = "0.0.31"
dynamic = ["dependencies"]
authors = [
{ name="Ciro Spaciari", email="ciro.spaciari@gmail.com" },
]
description = "Bringing WebSockets, Http/Https High Performance servers for PyPy3 and Python3"
readme = "README.md"
requires-python = ">=3.7"
requires-python = ">=3.8"
classifiers = [
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",

Wyświetl plik

@ -1,8 +1,8 @@
import sys
vi = sys.version_info
if vi < (3, 7):
raise RuntimeError("socketify requires Python 3.7 or greater")
if vi < (3, 8):
raise RuntimeError("socketify requires Python 3.8 or greater")
# if sys.platform in ('win32', 'cygwin', 'cli'):
# raise RuntimeError('socketify does not support Windows at the moment')
@ -58,7 +58,7 @@ with open("README.md", "r", encoding="utf-8") as fh:
setuptools.setup(
name="socketify",
version="0.0.9",
version="0.0.31",
platforms=["any"],
author="Ciro Spaciari",
author_email="ciro.spaciari@gmail.com",
@ -88,7 +88,7 @@ setuptools.setup(
"./native/*/*/*",
]
},
python_requires=">=3.7",
python_requires=">=3.8",
install_requires=["cffi>=1.0", "setuptools>=58.1.0"],
has_ext_modules=lambda: True,
cmdclass={}, # cmdclass={'sdist': Prepare, 'build_ext': Makefile},

Wyświetl plik

@ -1,9 +1,9 @@
import asyncio
from .dataclasses import AppListenOptions, AppOptions
from .tasks import TaskFactory, create_task, RequestTask
from .socketify import (
App,
AppOptions,
AppListenOptions,
OpCode,
SendStatus,
CompressOptions,

Wyświetl plik

@ -1,7 +1,7 @@
from socketify import App, OpCode, Loop
from socketify import App, OpCode
from queue import SimpleQueue
from .native import lib, ffi
from .tasks import create_task, create_task_with_factory
from .tasks import create_task, TaskFactory
import os
import platform
import sys
@ -10,7 +10,15 @@ import uuid
import asyncio
is_pypy = platform.python_implementation() == "PyPy"
@ffi.callback("void(uws_res_t*, void*)")
def asgi_on_abort_handler(res, user_data):
ctx = ffi.from_handle(user_data)
ctx.aborted = True
ctx.loop.is_idle = False
if ctx.abort_future is not None:
ctx.abort_future.set_result(True)
ctx.abort_future = None
async def task_wrapper(task):
try:
@ -25,7 +33,6 @@ async def task_wrapper(task):
EMPTY_RESPONSE = {"type": "http.request", "body": b"", "more_body": False}
@ffi.callback("void(uws_websocket_t*, const char*, size_t, uws_opcode_t, void*)")
def ws_message(ws, message, length, opcode, user_data):
socket_data = ffi.from_handle(user_data)
@ -50,10 +57,11 @@ def ws_open(ws, user_data):
@ffi.callback(
"void(int, uws_res_t*, socketify_asgi_ws_data, uws_socket_context_t* socket, void*, bool*)"
"void(int, uws_res_t*, socketify_asgi_ws_data, uws_socket_context_t* socket, void*)"
)
def ws_upgrade(ssl, response, info, socket_context, user_data, aborted):
def ws_upgrade(ssl, response, info, socket_context, user_data):
app = ffi.from_handle(user_data)
app.server.loop.is_idle = False
headers = []
next_header = info.header_list
while next_header != ffi.NULL:
@ -83,6 +91,7 @@ def ws_upgrade(ssl, response, info, socket_context, user_data, aborted):
extensions = ffi.unpack(info.extensions, info.extensions_size).decode("utf8")
compress = app.ws_compression
ws = ASGIWebSocket(app.server.loop)
lib.uws_res_on_aborted(ssl, response, asgi_on_abort_handler, ws._ptr)
scope = {
"type": "websocket",
@ -98,7 +107,7 @@ def ws_upgrade(ssl, response, info, socket_context, user_data, aborted):
"root_path": "",
"path": url.decode("utf8"),
"raw_path": url,
"query_string": ffi.unpack(info.query_string, info.query_string_size),
"query_string": ffi.unpack(info.query_string, info.query_string_size)[1:],
"headers": headers,
"subprotocols": [protocol] if protocol else [],
"extensions": {
@ -109,8 +118,9 @@ def ws_upgrade(ssl, response, info, socket_context, user_data, aborted):
}
async def send(options):
if bool(aborted[0]):
if ws.aborted:
return False
ws.loop.is_idle = False
type = options["type"]
if type == "websocket.send":
data = options.get("bytes", None)
@ -238,6 +248,7 @@ def ws_upgrade(ssl, response, info, socket_context, user_data, aborted):
@ffi.callback("void(uws_res_t*, const char*, size_t, bool, void*)")
def asgi_on_data_handler(res, chunk, chunk_length, is_end, user_data):
data_response = ffi.from_handle(user_data)
data_response.loop.is_idle = False
data_response.is_end = bool(is_end)
more_body = not data_response.is_end
result = {
@ -259,6 +270,22 @@ class ASGIDataQueue:
self.is_end = False
self.next_data_future = loop.create_future()
class ASGIContext:
def __init__(self, ssl, response, loop):
self._ptr = ffi.new_handle(self)
self.aborted = False
self.sended_empty = False
self.data_queue = None
self.ssl = ssl
self.response = response
self.loop = loop
self.abort_future = None
async def wait_disconnect(self):
if not self.aborted:
if self.abort_future is None:
self.abort_future = self.loop.create_future()
await self.abort_future
class ASGIWebSocket:
def __init__(self, loop):
@ -273,6 +300,14 @@ class ASGIWebSocket:
self._message = None
self._ptr = ffi.new_handle(self)
self.unregister = None
self.aborted = False
self.abort_future = None
async def wait_disconnect(self):
if not self.aborted:
if self.abort_future is None:
self.abort_future = self.loop.create_future()
await self.abort_future
def accept(self):
self.accept_future = self.loop.create_future()
@ -405,10 +440,11 @@ def uws_asgi_corked_403_handler(res, user_data):
lib.uws_res_end_without_body(ssl, res, 0)
@ffi.callback("void(int, uws_res_t*, socketify_asgi_data, void*, bool*)")
def asgi(ssl, response, info, user_data, aborted):
@ffi.callback("void(int, uws_res_t*, socketify_asgi_data, void*)")
def asgi(ssl, response, info, user_data):
app = ffi.from_handle(user_data)
app.server.loop.is_idle = False
headers = []
next_header = info.header_list
while next_header != ffi.NULL:
@ -427,7 +463,7 @@ def asgi(ssl, response, info, user_data, aborted):
"http_version": "1.1",
"server": (app.SERVER_HOST, app.SERVER_PORT),
"client": (
ffi.unpack(info.remote_address, info.remote_address_size).decode("utf8"),
None if info.remote_address == ffi.NULL else ffi.unpack(info.remote_address, info.remote_address_size).decode("utf8"),
None,
),
"scheme": app.SERVER_SCHEME,
@ -435,18 +471,25 @@ def asgi(ssl, response, info, user_data, aborted):
"root_path": "",
"path": url.decode("utf8"),
"raw_path": url,
"query_string": ffi.unpack(info.query_string, info.query_string_size),
"query_string": ffi.unpack(info.query_string, info.query_string_size)[1:],
"headers": headers,
}
loop = app.server.loop
ctx = ASGIContext(ssl, response, loop)
if bool(info.has_content):
data_queue = ASGIDataQueue(app.server.loop)
data_queue = ASGIDataQueue(loop)
lib.uws_res_on_data(ssl, response, asgi_on_data_handler, data_queue._ptr)
else:
data_queue = None
ctx.data_queue = data_queue
lib.uws_res_on_aborted(ssl, response, asgi_on_abort_handler, ctx._ptr)
async def receive():
if bool(aborted[0]):
if ctx.aborted:
return {"type": "http.disconnect"}
ctx.loop.is_idle = False
data_queue = ctx.data_queue
if data_queue:
if data_queue.queue.empty():
if not data_queue.is_end:
@ -458,13 +501,24 @@ def asgi(ssl, response, info, user_data, aborted):
else:
return data_queue.queue.get(False) # consume queue
# no more body, just empty
return EMPTY_RESPONSE
# no more body, just EMPTY RESPONSE
if not ctx.sended_empty:
ctx.sended_empty = True
return EMPTY_RESPONSE
# already sended empty body so wait for aborted request
if not ctx.aborted:
await ctx.wait_disconnect()
return {"type": "http.disconnect"}
async def send(options):
if bool(aborted[0]):
if ctx.aborted:
return False
ctx.loop.is_idle = False
type = options["type"]
ssl = ctx.ssl
response = ctx.response
if type == "http.response.start":
# can also be more native optimized to do it in one GIL call
# try socketify_res_write_int_status_with_headers and create and socketify_res_cork_write_int_status_with_headers
@ -493,6 +547,12 @@ def asgi(ssl, response, info, user_data, aborted):
elif isinstance(message, str):
data = message.encode("utf-8")
lib.socketify_res_cork_end(ssl, response, data, len(data), 0)
if ctx.abort_future is not None:
ctx.aborted = True
ctx.abort_future.set_result(False)
ctx.abort_future = None
return True
return False
@ -513,8 +573,8 @@ class _ASGI:
self.server = App(options, task_factory_max_items=0)
self.SERVER_PORT = None
self.SERVER_HOST = ""
self.SERVER_SCHEME = "https" if self.server.options else "http"
self.SERVER_WS_SCHEME = "wss" if self.server.options else "ws"
self.SERVER_SCHEME = "https" if options and options.cert_file_name is not None else "http"
self.SERVER_WS_SCHEME = "wss" if options and options.cert_file_name is not None else "ws"
self.task_factory_max_items = task_factory_max_items
self.lifespan = lifespan
@ -523,38 +583,25 @@ class _ASGI:
# internally will still use custom task factory for pypy because of Loop
if is_pypy:
if task_factory_max_items > 0:
factory = create_task_with_factory(task_factory_max_items)
factory = TaskFactory(task_factory_max_items)
def run_task(task):
factory(loop, task_wrapper(task))
loop._run_once()
self._run_task = run_task
else:
def run_task(task):
create_task(loop, task_wrapper(task))
loop._run_once()
future = create_task(loop, task_wrapper(task))
future._log_destroy_pending = False
self._run_task = run_task
else:
if sys.version_info >= (3, 8): # name fixed to avoid dynamic name
def run_task(task):
future = loop.create_task(
task_wrapper(task), name="socketify.py-request-task"
)
future = create_task(loop, task_wrapper(task))
future._log_destroy_pending = False
loop._run_once()
self._run_task = run_task
else:
def run_task(task):
future = loop.create_task(task_wrapper(task))
future._log_destroy_pending = False
loop._run_once()
self._run_task = run_task
@ -641,12 +688,13 @@ class _ASGI:
asgi_app = self
self.is_starting = True
self.is_stopped = False
self.status = 0 # 0 starting, 1 ok, 2 error, 3 stoping, 4 stopped, 5 stopped with error, 6 no lifespan
self.status = 0 # 0 starting, 1 ok, 2 error, 3 stopping, 4 stopped, 5 stopped with error, 6 no lifespan
self.status_message = ""
self.stop_future = self.server.loop.create_future()
async def send(options):
nonlocal asgi_app
asgi_app.server.loop.is_idle = False
type = options["type"]
asgi_app.status_message = options.get("message", "")
if type == "lifespan.startup.complete":
@ -664,6 +712,7 @@ class _ASGI:
async def receive():
nonlocal asgi_app
asgi_app.server.loop.is_idle = False
while not asgi_app.is_stopped:
if asgi_app.is_starting:
asgi_app.is_starting = False
@ -686,7 +735,7 @@ class _ASGI:
asgi_app.server.listen(port_or_options, handler)
finally:
return None
self.server.loop.is_idle = False
# start lifespan
self.server.loop.ensure_future(task_wrapper(self.app(scope, receive, send)))
self.server.run()
@ -724,10 +773,13 @@ class _ASGI:
return self
def __del__(self):
if self.asgi_http_info:
lib.socketify_destroy_asgi_app_info(self.asgi_http_info)
if self.asgi_ws_info:
lib.socketify_destroy_asgi_ws_app_info(self.asgi_ws_info)
try:
if self.asgi_http_info:
lib.socketify_destroy_asgi_app_info(self.asgi_http_info)
if self.asgi_ws_info:
lib.socketify_destroy_asgi_ws_app_info(self.asgi_ws_info)
except:
pass
# "Public" ASGI interface to allow easy forks/workers
@ -748,12 +800,26 @@ class ASGI:
self.listen_options = None
self.task_factory_max_items = task_factory_max_items
self.lifespan = lifespan
self.server = None
self.pid_list = None
def listen(self, port_or_options, handler=None):
self.listen_options = (port_or_options, handler)
return self
def run(self, workers=1):
def close(self):
# always wait a sec so forks can start properly if close is called too fast
import time
time.sleep(1)
if self.server is not None:
self.server.close()
if self.pid_list is not None:
import signal
for pid in self.pid_list:
os.kill(pid, signal.SIGINT)
def run(self, workers=1, block=True):
def run_task():
server = _ASGI(
self.app,
@ -766,17 +832,27 @@ class ASGI:
if self.listen_options:
(port_or_options, handler) = self.listen_options
server.listen(port_or_options, handler)
server.run()
def create_fork():
n = os.fork()
# n greater than 0 means parent process
if not n > 0:
run_task()
self.server = server
server.run()
pid_list = []
start = 1 if block else 0
# fork limiting the cpu count - 1
for _ in range(1, workers):
create_fork()
for _ in range(block, workers):
pid = os.fork()
# n greater than 0 means parent process
if not pid > 0:
run_task()
break
pid_list.append(pid)
self.pid_list = pid_list
if block:
run_task() # run app on the main process too :)
# sigint everything to graceful shutdown
import signal
for pid in pid_list:
os.kill(pid, signal.SIGINT)
run_task() # run app on the main process too :)
return self

Wyświetl plik

@ -28,7 +28,7 @@ Options:
--ws-close-on-backpressure-limit BOOLEAN Close connections that hits maximum backpressure [default: False]
--lifespan [auto|on|off] Lifespan implementation. [default: auto]
--interface [auto|asgi|asgi3|wsgi|ssgi|socketify] Select ASGI (same as ASGI3), ASGI3, WSGI or SSGI as the application interface. [default: auto]
--disable-listen-log BOOLEAN Disable log when start listenning [default: False]
--disable-listen-log BOOLEAN Disable log when start listening [default: False]
--version or -v Display the socketify.py version and exit.
--ssl-keyfile TEXT SSL key file
--ssl-certfile TEXT SSL certificate file
@ -188,10 +188,10 @@ def execute(args):
elif interface == "ssgi":
# if not is_ssgi(module):
# return print("SSGI is in development yet but is comming soon")
# return print("SSGI is in development yet but is coming soon")
# from . import SSGI as Interface
# interface = "ssgi"
return print("SSGI is in development yet but is comming soon")
return print("SSGI is in development yet but is coming soon")
elif interface != "socketify":
return print(f"{interface} interface is not supported yet")
@ -310,22 +310,20 @@ def execute(args):
fork_app.listen(AppListenOptions(port=port, host=host), listen_log)
fork_app.run()
# now we can start all over again
def create_fork(_):
n = os.fork()
pid_list = []
# fork limiting the cpu count - 1
for _ in range(1, workers):
pid = os.fork()
# n greater than 0 means parent process
if not n > 0:
if not pid > 0:
run_app()
return n
break
pid_list.append(pid)
# run in all forks
pid_list = list(map(create_fork, range(1, workers)))
run_app() # run app on the main process too :)
# run in this process
run_app()
# sigint everything to gracefull shutdown
# sigint everything to graceful shutdown
import signal
for pid in pid_list:
os.kill(pid, signal.SIGINT)
else:

Wyświetl plik

@ -0,0 +1,52 @@
from dataclasses import dataclass
@dataclass
class AppListenOptions:
port: int = 0
host: str = None
options: int = 0
domain: str = None
def __post_init__(self):
if not isinstance(self.port, int):
raise RuntimeError("port must be an int")
if not isinstance(self.host, (type(None), str)):
raise RuntimeError("host must be a str if specified")
if not isinstance(self.domain, (type(None), str)):
raise RuntimeError("domain must be a str if specified")
if not isinstance(self.options, int):
raise RuntimeError("options must be an int")
if self.domain and (self.host or self.port != 0):
raise RuntimeError(
"if domain is specified, host and port will be no effect"
)
@dataclass
class AppOptions:
key_file_name: str = None
cert_file_name: str = None
passphrase: str = None
dh_params_file_name: str = None
ca_file_name: str = None
ssl_ciphers: str = None
ssl_prefer_low_memory_usage: int = 0
def __post_init__(self):
NoneType = type(None)
if not isinstance(self.key_file_name, (NoneType, str)):
raise RuntimeError("key_file_name must be a str if specified")
if not isinstance(self.cert_file_name, (NoneType, str)):
raise RuntimeError("cert_file_name must be a str if specified")
if not isinstance(self.passphrase, (NoneType, str)):
raise RuntimeError("passphrase must be a str if specified")
if not isinstance(self.dh_params_file_name, (NoneType, str)):
raise RuntimeError("dh_params_file_name must be a str if specified")
if not isinstance(self.ca_file_name, (NoneType, str)):
raise RuntimeError("ca_file_name must be a str if specified")
if not isinstance(self.ssl_ciphers, (NoneType, str)):
raise RuntimeError("ssl_ciphers must be a str if specified")
if not isinstance(self.ssl_prefer_low_memory_usage, int):
raise RuntimeError("ssl_prefer_low_memory_usage must be an int")

Wyświetl plik

@ -70,7 +70,7 @@ async def sendfile(res, req, filename):
def send_headers(res):
res.write_status(status)
# tells the broswer the last modified date
# tells the browser the last modified date
res.write_header(b"Last-Modified", last_modified)
# tells the browser that we support range
@ -147,7 +147,7 @@ def middleware(*functions):
# we use Optional data=None at the end so you can use and middleware inside a middleware
def optimized_middleware_route(res, req, data=None):
# cicle to all middlewares
# circle to all middlewares
for function in syncs:
# call middlewares
data = function(res, req, data)
@ -156,7 +156,7 @@ def middleware(*functions):
return
async def wrapper(res, req, data):
# cicle to all middlewares
# circle to all middlewares
for function in asyncs:
# detect if is coroutine or not
if inspect.iscoroutinefunction(function):
@ -182,7 +182,7 @@ def middleware(*functions):
def sync_middleware(*functions):
# we use Optional data=None at the end so you can use and middleware inside a middleware
def middleware_route(res, req, data=None):
# cicle to all middlewares
# circle to all middlewares
for function in functions:
# call middlewares
data = function(res, req, data)
@ -198,7 +198,7 @@ def async_middleware(*functions):
# we use Optional data=None at the end so you can use and middleware inside a middleware
async def middleware_route(res, req, data=None):
some_async_as_run = False
# cicle to all middlewares
# circle to all middlewares
for function in functions:
# detect if is coroutine or not
if inspect.iscoroutinefunction(function):
@ -363,61 +363,61 @@ class MiddlewareRouter:
self.middlewares = middlewares
def get(self, path, handler):
middies = list(*self.middlewares)
middies = list(self.middlewares)
middies.append(handler)
self.app.get(path, middleware(*middies))
return self
def post(self, path, handler):
middies = list(*self.middlewares)
middies = list(self.middlewares)
middies.append(handler)
self.app.post(path, middleware(*middies))
return self
def options(self, path, handler):
middies = list(*self.middlewares)
middies = list(self.middlewares)
middies.append(handler)
self.app.options(path, middleware(*middies))
return self
def delete(self, path, handler):
middies = list(*self.middlewares)
middies = list(self.middlewares)
middies.append(handler)
self.app.delete(path, middleware(*middies))
return self
def patch(self, path, handler):
middies = list(*self.middlewares)
middies = list(self.middlewares)
middies.append(handler)
self.app.patch(path, middleware(*middies))
return self
def put(self, path, handler):
middies = list(*self.middlewares)
middies = list(self.middlewares)
middies.append(handler)
self.app.put(path, middleware(*middies))
return self
def head(self, path, handler):
middies = list(*self.middlewares)
middies = list(self.middlewares)
middies.append(handler)
self.app.head(path, middleware(*middies))
return self
def connect(self, path, handler):
middies = list(*self.middlewares)
middies = list(self.middlewares)
middies.append(handler)
self.app.connect(path, middleware(*middies))
return self
def trace(self, path, handler):
middies = list(*self.middlewares)
middies = list(self.middlewares)
middies.append(handler)
self.app.trace(path, middleware(*middies))
return self
def any(self, path, handler):
middies = list(*self.middlewares)
middies = list(self.middlewares)
middies.append(handler)
self.app.any(path, middleware(*middies))
return self

Plik binarny nie jest wyświetlany.

Plik binarny nie jest wyświetlany.

Plik binarny nie jest wyświetlany.

Plik binarny nie jest wyświetlany.

Wyświetl plik

@ -1,6 +1,6 @@
import asyncio
import logging
from .tasks import create_task, create_task_with_factory
from .tasks import create_task, TaskFactory
from .uv import UVLoop
import asyncio
@ -8,7 +8,6 @@ import platform
is_pypy = platform.python_implementation() == "PyPy"
async def task_wrapper(exception_handler, loop, response, task):
try:
return await task
@ -26,16 +25,18 @@ async def task_wrapper(exception_handler, loop, response, task):
class Loop:
def __init__(self, exception_handler=None, task_factory_max_items=0):
def __init__(self, exception_handler=None, task_factory_max_items=0, idle_relaxation_time=0.01):
# get the current running loop or create a new one without warnings
self.loop = asyncio._get_running_loop()
self._idle_count = 0
self.is_idle = False
self.idle_relaxation_time = idle_relaxation_time
if self.loop is None:
self.loop = asyncio.new_event_loop()
asyncio.set_event_loop(self.loop)
self.uv_loop = UVLoop()
if hasattr(exception_handler, "__call__"):
self.exception_handler = exception_handler
self.loop.set_exception_handler(
@ -47,20 +48,27 @@ class Loop:
self.started = False
if is_pypy: # PyPy async Optimizations
if task_factory_max_items > 0: # Only available in PyPy for now
self._task_factory = create_task_with_factory(task_factory_max_items)
self._task_factory = TaskFactory(task_factory_max_items)
else:
self._task_factory = create_task
self.run_async = self._run_async_pypy
# custom task factory
# TODO: check if any framework breaks without current_task(loop) support
# custom task factory for other tasks
def pypy_task_factory(loop, coro, context=None):
return create_task(loop, coro, context=context)
self.loop.set_task_factory(pypy_task_factory)
else:
# CPython performs worse using custom create_task, so native create_task is used
# but this also did not allow the use of create_task_with_factory :/
# native create_task do not allow to change context, callbacks, state etc
# TODO: check if any framework breaks without current_task(loop) support
# custom task factory for other tasks
def cpython_task_factory(loop, coro, context=None):
return create_task(loop, coro, context=context)
self.loop.set_task_factory(cpython_task_factory)
# CPython performs equals or worse using TaskFactory
self.run_async = self._run_async_cpython
def set_timeout(self, timeout, callback, user_data):
@ -71,15 +79,71 @@ class Loop:
def _keep_alive(self):
if self.started:
self.uv_loop.run_once()
self.loop.call_soon(self._keep_alive)
relax = False
if not self.is_idle:
self._idle_count = 0
elif self._idle_count < 10000:
self._idle_count += 1
else:
relax = True
self.is_idle = True
if relax:
self.uv_loop.run_nowait()
if self._idle_count < 15000:
self._idle_count += 1
# we are idle not for long, wait 5s until next relax mode
self.loop.call_later(0.001, self._keep_alive)
else:
# we are really idle now lets use less CPU
self.loop.call_later(self.idle_relaxation_time, self._keep_alive)
else:
self.uv_loop.run_nowait()
# be more aggressive when needed
self.loop.call_soon(self._keep_alive)
def create_task(self, *args, **kwargs):
# this is not using optimized create_task yet
return self.loop.create_task(*args, **kwargs)
def ensure_future(self, task):
return asyncio.ensure_future(task, loop=self.loop)
def set_event_loop(self, loop):
needs_start = False
if self.loop.is_running():
self.stop()
self.loop = loop
if self.exception_handler is not None:
self.loop.set_exception_handler(
lambda loop, context: self.exception_handler(loop, context, None)
)
if is_pypy: # PyPy async Optimizations
# TODO: check if any framework breaks without current_task(loop) support
# custom task factory for other tasks
def pypy_task_factory(loop, coro, context=None):
return create_task(loop, coro, context=context)
self.loop.set_task_factory(pypy_task_factory)
else:
# TODO: check if any framework breaks without current_task(loop) support
# custom task factory for other tasks
def cpython_task_factory(loop, coro, context=None):
return create_task(loop, coro, context=context)
self.loop.set_task_factory(cpython_task_factory)
if needs_start:
self.run()
def create_background_task(self, bg_task):
def next_tick():
self.ensure_future(bg_task())
self.loop.call_soon(next_tick)
def run_until_complete(self, task=None):
self.started = True
@ -107,8 +171,15 @@ class Loop:
def run_once(self):
# run one step of asyncio
self.loop._stopping = True
self.loop._run_once()
# if loop._run_once is not available use loop.run_forever + loop.call_soon(loop.stop)
# this is useful when using uvloop or custom loops
try:
self.loop._stopping = True
self.loop._run_once()
except Exception:
# this can be _StopError with means we should not call run_forever, but we can ignore it
self.loop.call_soon(self.loop.stop)
self.loop.run_forever()
# run one step of libuv
self.uv_loop.run_once()
@ -123,24 +194,19 @@ class Loop:
return self.uv_loop.get_native_loop()
def _run_async_pypy(self, task, response=None):
# this garanties error 500 in case of uncaught exceptions, and can trigger the custom error handler
# this guarantees error 500 in case of uncaught exceptions, and can trigger the custom error handler
# using an coroutine wrapper generates less overhead than using add_done_callback
# this is an custom task/future with less overhead
# this is an custom task/future with less overhead and that calls the first step
future = self._task_factory(
self.loop, task_wrapper(self.exception_handler, self.loop, response, task)
)
# force asyncio run once to enable req in async functions before first await
self.loop._run_once()
return None # this future maybe already done and reused not safe to await
def _run_async_cpython(self, task, response=None):
# this garanties error 500 in case of uncaught exceptions, and can trigger the custom error handler
# this guarantees error 500 in case of uncaught exceptions, and can trigger the custom error handler
# using an coroutine wrapper generates less overhead than using add_done_callback
future = self.loop.create_task(
task_wrapper(self.exception_handler, self.loop, response, task)
)
# force asyncio run once to enable req in async functions before first await
self.loop._run_once()
# this is an custom task/future with less overhead and that calls the first step
future = create_task(self.loop, task_wrapper(self.exception_handler, self.loop, response, task))
return None # this future is safe to await but we return None for compatibility, and in the future will be the same behavior as PyPy
def dispose(self):

Wyświetl plik

@ -187,7 +187,7 @@ void uws_add_server_name_with_options(int ssl, uws_app_t *app, const char *hostn
void uws_missing_server_name(int ssl, uws_app_t *app, uws_missing_server_handler handler, void *user_data);
void uws_filter(int ssl, uws_app_t *app, uws_filter_handler handler, void *user_data);
void uws_res_close(int ssl, uws_res_t *res);
void uws_res_end(int ssl, uws_res_t *res, const char *data, size_t length, bool close_connection);
void uws_res_pause(int ssl, uws_res_t *res);
void uws_res_resume(int ssl, uws_res_t *res);
@ -215,7 +215,7 @@ size_t uws_res_get_proxied_remote_address_as_text(int ssl, uws_res_t *res, const
bool uws_req_is_ancient(uws_req_t *res);
bool uws_req_get_yield(uws_req_t *res);
void uws_req_set_field(uws_req_t *res, bool yield);
void uws_req_set_yield(uws_req_t *res, bool yield);
size_t uws_req_get_url(uws_req_t *res, const char **dest);
size_t uws_req_get_method(uws_req_t *res, const char **dest);
size_t uws_req_get_case_sensitive_method(uws_req_t *res, const char **dest);
@ -352,21 +352,21 @@ typedef struct {
socketify_header* header_list;
} socketify_asgi_ws_data;
typedef void (*socketify_asgi_method_handler)(int ssl, uws_res_t *response, socketify_asgi_data request, void *user_data, bool* aborted);
typedef void (*socketify_asgi_method_handler)(int ssl, uws_res_t *response, socketify_asgi_data request, void *user_data);
typedef struct {
int ssl;
uws_app_t* app;
socketify_asgi_method_handler handler;
void * user_data;
} socksocketify_asgi_app_info;
typedef void (*socketify_asgi_ws_method_handler)(int ssl, uws_res_t *response, socketify_asgi_ws_data request, uws_socket_context_t* socket, void *user_data, bool* aborted);
typedef void (*socketify_asgi_ws_method_handler)(int ssl, uws_res_t *response, socketify_asgi_ws_data request, uws_socket_context_t* socket, void *user_data);
typedef struct {
int ssl;
uws_app_t* app;
socketify_asgi_ws_method_handler handler;
uws_socket_behavior_t behavior;
void * user_data;
} socksocketify_asgi_ws_app_info;
} socketify_asgi_ws_app_info;
socketify_asgi_data socketify_asgi_request(int ssl, uws_req_t *req, uws_res_t *res);
void socketify_destroy_headers(socketify_header* headers);
@ -376,8 +376,8 @@ socketify_asgi_ws_data socketify_asgi_ws_request(int ssl, uws_req_t *req, uws_re
bool socketify_res_write_int_status(int ssl, uws_res_t* res, int code);
socksocketify_asgi_app_info* socketify_add_asgi_http_handler(int ssl, uws_app_t* app, socketify_asgi_method_handler handler, void* user_data);
void socketify_destroy_asgi_app_info(socksocketify_asgi_app_info* app);
socksocketify_asgi_ws_app_info* socketify_add_asgi_ws_handler(int ssl, uws_app_t* app, uws_socket_behavior_t behavior, socketify_asgi_ws_method_handler handler, void* user_data);
void socketify_destroy_asgi_ws_app_info(socksocketify_asgi_ws_app_info* app);
socketify_asgi_ws_app_info* socketify_add_asgi_ws_handler(int ssl, uws_app_t* app, uws_socket_behavior_t behavior, socketify_asgi_ws_method_handler handler, void* user_data);
void socketify_destroy_asgi_ws_app_info(socketify_asgi_ws_app_info* app);
void socketify_res_cork_write(int ssl, uws_res_t *response, const char* data, size_t length);
void socketify_res_cork_end(int ssl, uws_res_t *response, const char* data, size_t length, bool close_connection);
@ -385,6 +385,12 @@ void socketify_ws_cork_send(int ssl, uws_websocket_t *ws, const char* data, size
void socketify_ws_cork_send_with_options(int ssl, uws_websocket_t *ws, const char* data, size_t length, uws_opcode_t opcode, bool compress, bool close_connection);
void socketify_res_send_int_code(int ssl, uws_res_t *res, const char* content_data, size_t content_data_size, int code, const char *content_type, size_t content_type_size, bool close_connection);
void socketify_res_send(int ssl, uws_res_t *res, const char *content_data, size_t content_data_size, const char *status_code, size_t status_code_size, const char *content_type, size_t content_type_size, bool close_connection);
void socketify_res_cork_send_int_code(int ssl, uws_res_t *res, const char* content_data, size_t content_data_size, int code, const char *content_type, size_t content_type_size, bool close_connection);
void socketify_res_cork_send(int ssl, uws_res_t *res, const char *content_data, size_t content_data_size, const char *status_code, size_t status_code_size, const char *content_type, size_t content_type_size, bool close_connection);
"""
)

Wyświetl plik

@ -1,7 +1,7 @@
LIBRARY_NAME := libsocketify
UWS_LIBRARY_NAME := libuwebsockets
CC := clang
CXX := clang++
CC ?= clang
CXX ?= clang++
ARCH := amd64
ifeq ($(PLATFORM), arm64)
@ -72,17 +72,14 @@ macos:
# build boringssl
cd ../uWebSockets/uSockets/boringssl && mkdir -p amd64 && cd amd64 && cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_OSX_DEPLOYMENT_TARGET=10.14 .. && make crypto ssl
# build lsquic
cd ../uWebSockets/uSockets/lsquic && cmake -DCMAKE_POSITION_INDEPENDENT_CODE=ON -DBORINGSSL_DIR=../boringssl -DCMAKE_BUILD_TYPE=Release -DLSQUIC_BIN=Off . && make lsquic
# build uWebSockets
cd ../uWebSockets/uSockets && $(CC) -mmacosx-version-min=10.14 -I src -I lsquic/include -I boringssl/include -DUWS_WITH_PROXY -DLIBUS_USE_OPENSSL -DLIBUS_USE_LIBUV -DLIBUS_USE_QUIC -pthread -fPIC -std=c11 -O3 -c src/*.c src/eventing/*.c src/crypto/*.c
cd ../uWebSockets/uSockets && $(CXX) -stdlib=libc++ -mmacosx-version-min=10.14 -I boringssl/include -DUWS_WITH_PROXY -DLIBUS_USE_OPENSSL -DLIBUS_USE_LIBUV -DLIBUS_USE_QUIC -pthread -fPIC -std=c++17 -O3 -c src/crypto/*.cpp
cd ../uWebSockets/uSockets && $(CC) -mmacosx-version-min=10.14 -I src -I boringssl/include -DUWS_WITH_PROXY -DLIBUS_USE_OPENSSL -DLIBUS_USE_LIBUV -pthread -fPIC -std=c11 -O3 -c src/*.c src/eventing/*.c src/crypto/*.c
cd ../uWebSockets/uSockets && $(CXX) -stdlib=libc++ -mmacosx-version-min=10.14 -I boringssl/include -DUWS_WITH_PROXY -DLIBUS_USE_OPENSSL -DLIBUS_USE_LIBUV -pthread -fPIC -std=c++17 -O3 -c src/crypto/*.cpp
cd ../uWebSockets/uSockets && $(AR) rvs uSockets_darwin_amd64.a *.o
# build CAPI + libsocketify
$(CXX) -stdlib=libc++ -mmacosx-version-min=10.14 -I ./src -I ../uWebSockets/src -I ../uWebSockets/uSockets/src -I ../uWebSockets/capi -I ../uWebSockets/uSockets/lsquic/include -I ../uWebSockets/uSockets/boringssl/include -DUWS_WITH_PROXY -pthread -fPIC -std=c++17 -c -O3 ./src/$(LIBRARY_NAME).cpp
$(CXX) -stdlib=libc++ -mmacosx-version-min=10.14 -shared -undefined dynamic_lookup -o ../$(LIBRARY_NAME)_darwin_amd64.so $(LIBRARY_NAME).o ../uWebSockets/uSockets/uSockets_darwin_amd64.a ../uWebSockets/uSockets/boringssl/amd64/ssl/libssl.a ../uWebSockets/uSockets/boringssl/amd64/crypto/libcrypto.a ../uWebSockets/uSockets/lsquic/src/liblsquic/liblsquic.a -flto -fPIC -lz -luv
$(CXX) -stdlib=libc++ -mmacosx-version-min=10.14 -I ./src -I ../uWebSockets/src -I ../uWebSockets/uSockets/src -I ../uWebSockets/capi -I ../uWebSockets/uSockets/boringssl/include -DUWS_WITH_PROXY -pthread -fPIC -std=c++17 -c -O3 ./src/$(LIBRARY_NAME).cpp
$(CXX) -stdlib=libc++ -mmacosx-version-min=10.14 -shared -undefined dynamic_lookup -o ../$(LIBRARY_NAME)_darwin_amd64.so $(LIBRARY_NAME).o ../uWebSockets/uSockets/uSockets_darwin_amd64.a ../uWebSockets/uSockets/boringssl/amd64/ssl/libssl.a ../uWebSockets/uSockets/boringssl/amd64/crypto/libcrypto.a -flto -fPIC -lz -luv
linux:
$(MAKE) clean
@ -90,29 +87,26 @@ linux:
# build boringssl
cd ../uWebSockets/uSockets/boringssl && mkdir -p $(ARCH) && cd $(ARCH) && cmake -DCMAKE_POSITION_INDEPENDENT_CODE=ON -DCMAKE_BUILD_TYPE=Release .. && make crypto ssl
# build lsquic
cd ../uWebSockets/uSockets/lsquic && cmake -DCMAKE_POSITION_INDEPENDENT_CODE=ON -DBORINGSSL_DIR=../boringssl -DCMAKE_BUILD_TYPE=Release -DLSQUIC_BIN=Off . && make lsquic
# build uWebSockets
cd ../uWebSockets/uSockets && $(CC) -I src -I lsquic/include -I boringssl/include -DUWS_WITH_PROXY -DLIBUS_USE_OPENSSL -DLIBUS_USE_LIBUV -DLIBUS_USE_QUIC -pthread -fPIC -std=c11 -O3 -c src/*.c src/eventing/*.c src/crypto/*.c
cd ../uWebSockets/uSockets && $(CXX) -I boringssl/include -DUWS_WITH_PROXY -DLIBUS_USE_OPENSSL -DLIBUS_USE_LIBUV -DLIBUS_USE_QUIC -pthread -fPIC -std=c++17 -O3 -c src/crypto/*.cpp
cd ../uWebSockets/uSockets && $(CC) -I src -I boringssl/include -DUWS_WITH_PROXY -DLIBUS_USE_OPENSSL -DLIBUS_USE_LIBUV -pthread -fPIC -std=c11 -O3 -c src/*.c src/eventing/*.c src/crypto/*.c
cd ../uWebSockets/uSockets && $(CXX) -I boringssl/include -DUWS_WITH_PROXY -DLIBUS_USE_OPENSSL -DLIBUS_USE_LIBUV -pthread -fPIC -std=c++17 -O3 -c src/crypto/*.cpp
cd ../uWebSockets/uSockets && $(AR) rvs uSockets_linux_$(ARCH).a *.o
# build CAPI + libsocketify
$(CXX) -I ./src -I ../uWebSockets/src -I ../uWebSockets/uSockets/src -I ../uWebSockets/capi -I ../uWebSockets/uSockets/lsquic/include -I ../uWebSockets/uSockets/boringssl/include -DUWS_WITH_PROXY -pthread -fPIC -std=c++17 -c -O3 ./src/$(LIBRARY_NAME).cpp
$(CXX) -shared -static-libstdc++ -static-libgcc -s -o ../$(LIBRARY_NAME)_linux_$(ARCH).so $(LIBRARY_NAME).o ../uWebSockets/uSockets/uSockets_linux_$(ARCH).a ../uWebSockets/uSockets/boringssl/$(ARCH)/ssl/libssl.a ../uWebSockets/uSockets/boringssl/$(ARCH)/crypto/libcrypto.a ../uWebSockets/uSockets/lsquic/src/liblsquic/liblsquic.a -flto -fPIC -lz -luv
$(CXX) -I ./src -I ../uWebSockets/src -I ../uWebSockets/uSockets/src -I ../uWebSockets/capi -I ../uWebSockets/uSockets/boringssl/include -DUWS_WITH_PROXY -pthread -fPIC -std=c++17 -c -O3 ./src/$(LIBRARY_NAME).cpp
$(CXX) -shared -static-libstdc++ -static-libgcc -s -o ../$(LIBRARY_NAME)_linux_$(ARCH).so $(LIBRARY_NAME).o ../uWebSockets/uSockets/uSockets_linux_$(ARCH).a ../uWebSockets/uSockets/boringssl/$(ARCH)/ssl/libssl.a ../uWebSockets/uSockets/boringssl/$(ARCH)/crypto/libcrypto.a -flto -fPIC -lz -luv
linux-uws-socketify:
# build uWebSockets
cd ../uWebSockets/uSockets && $(CC) -I src -I lsquic/include -I boringssl/include -DUWS_WITH_PROXY -DLIBUS_USE_OPENSSL -DLIBUS_USE_LIBUV -DLIBUS_USE_QUIC -pthread -fPIC -std=c11 -O3 -c src/*.c src/eventing/*.c src/crypto/*.c
cd ../uWebSockets/uSockets && $(CXX) -I boringssl/include -DUWS_WITH_PROXY -DLIBUS_USE_OPENSSL -DLIBUS_USE_LIBUV -DLIBUS_USE_QUIC -pthread -fPIC -std=c++17 -O3 -c src/crypto/*.cpp
cd ../uWebSockets/uSockets && $(CC) -I src -I boringssl/include -DUWS_WITH_PROXY -DLIBUS_USE_OPENSSL -DLIBUS_USE_LIBUV -pthread -fPIC -std=c11 -O3 -c src/*.c src/eventing/*.c src/crypto/*.c
cd ../uWebSockets/uSockets && $(CXX) -I boringssl/include -DUWS_WITH_PROXY -DLIBUS_USE_OPENSSL -DLIBUS_USE_LIBUV -pthread -fPIC -std=c++17 -O3 -c src/crypto/*.cpp
cd ../uWebSockets/uSockets && $(AR) rvs uSockets_linux_$(ARCH).a *.o
# build CAPI + libsocketify
$(CXX) -I ./src -I ../uWebSockets/src -I ../uWebSockets/uSockets/src -I ../uWebSockets/capi -I ../uWebSockets/uSockets/lsquic/include -I ../uWebSockets/uSockets/boringssl/include -DUWS_WITH_PROXY -pthread -fPIC -std=c++17 -c -O3 ./src/$(LIBRARY_NAME).cpp
$(CXX) -shared -static-libstdc++ -static-libgcc -s -o ../$(LIBRARY_NAME)_linux_$(ARCH).so $(LIBRARY_NAME).o ../uWebSockets/uSockets/uSockets_linux_$(ARCH).a ../uWebSockets/uSockets/boringssl/$(ARCH)/ssl/libssl.a ../uWebSockets/uSockets/boringssl/$(ARCH)/crypto/libcrypto.a ../uWebSockets/uSockets/lsquic/src/liblsquic/liblsquic.a -flto -fPIC -lz -luv
$(CXX) -I ./src -I ../uWebSockets/src -I ../uWebSockets/uSockets/src -I ../uWebSockets/capi -I ../uWebSockets/uSockets/boringssl/include -DUWS_WITH_PROXY -pthread -fPIC -std=c++17 -c -O3 ./src/$(LIBRARY_NAME).cpp
$(CXX) -shared -static-libstdc++ -static-libgcc -s -o ../$(LIBRARY_NAME)_linux_$(ARCH).so $(LIBRARY_NAME).o ../uWebSockets/uSockets/uSockets_linux_$(ARCH).a ../uWebSockets/uSockets/boringssl/$(ARCH)/ssl/libssl.a ../uWebSockets/uSockets/boringssl/$(ARCH)/crypto/libcrypto.a -flto -fPIC -lz -luv
linux-socketify-only:
# build CAPI + libsocketify
$(CXX) -I ./src -I ../uWebSockets/src -I ../uWebSockets/uSockets/src -I ../uWebSockets/capi -I ../uWebSockets/uSockets/lsquic/include -I ../uWebSockets/uSockets/boringssl/include -DUWS_WITH_PROXY -pthread -fPIC -std=c++17 -c -O3 ./src/$(LIBRARY_NAME).cpp
$(CXX) -shared -static-libstdc++ -static-libgcc -s -o ../$(LIBRARY_NAME)_linux_$(ARCH).so $(LIBRARY_NAME).o ../uWebSockets/uSockets/uSockets_linux_$(ARCH).a ../uWebSockets/uSockets/boringssl/$(ARCH)/ssl/libssl.a ../uWebSockets/uSockets/boringssl/$(ARCH)/crypto/libcrypto.a ../uWebSockets/uSockets/lsquic/src/liblsquic/liblsquic.a -flto -fPIC -lz -luv
$(CXX) -I ./src -I ../uWebSockets/src -I ../uWebSockets/uSockets/src -I ../uWebSockets/capi -I ../uWebSockets/uSockets/boringssl/include -DUWS_WITH_PROXY -pthread -fPIC -std=c++17 -c -O3 ./src/$(LIBRARY_NAME).cpp
$(CXX) -shared -static-libstdc++ -static-libgcc -s -o ../$(LIBRARY_NAME)_linux_$(ARCH).so $(LIBRARY_NAME).o ../uWebSockets/uSockets/uSockets_linux_$(ARCH).a ../uWebSockets/uSockets/boringssl/$(ARCH)/ssl/libssl.a ../uWebSockets/uSockets/boringssl/$(ARCH)/crypto/libcrypto.a -flto -fPIC -lz -luv

Wyświetl plik

@ -8,136 +8,144 @@
extern "C"
{
#endif
DLL_EXPORT typedef void (*socketify_prepare_handler)(void* user_data);
DLL_EXPORT typedef void (*socketify_timer_handler)(void* user_data);
DLL_EXPORT typedef void (*socketify_prepare_handler)(void *user_data);
DLL_EXPORT typedef void (*socketify_timer_handler)(void *user_data);
DLL_EXPORT typedef enum {
SOCKETIFY_RUN_DEFAULT = 0,
SOCKETIFY_RUN_ONCE,
SOCKETIFY_RUN_NOWAIT
} socketify_run_mode;
DLL_EXPORT typedef enum {
SOCKETIFY_RUN_DEFAULT = 0,
SOCKETIFY_RUN_ONCE,
SOCKETIFY_RUN_NOWAIT
} socketify_run_mode;
DLL_EXPORT typedef struct {
void* uv_prepare_ptr;
DLL_EXPORT typedef struct
{
void *uv_prepare_ptr;
socketify_prepare_handler on_prepare_handler;
void* on_prepare_data;
void* uv_loop;
} socketify_loop;
void *on_prepare_data;
void *uv_loop;
} socketify_loop;
DLL_EXPORT typedef struct{
void* uv_timer_ptr;
DLL_EXPORT typedef struct
{
void *uv_timer_ptr;
socketify_timer_handler handler;
void* user_data;
} socketify_timer;
void *user_data;
} socketify_timer;
DLL_EXPORT typedef struct
{
DLL_EXPORT typedef struct {
const char *name;
const char *value;
const char* name;
const char* value;
size_t name_size;
size_t value_size;
void* next;
} socketify_header;
size_t name_size;
size_t value_size;
void *next;
} socketify_header;
DLL_EXPORT typedef struct {
DLL_EXPORT typedef struct
{
const char* full_url;
const char* url;
const char* query_string;
const char* method;
const char* remote_address;
const char *full_url;
const char *url;
const char *query_string;
const char *method;
const char *remote_address;
size_t full_url_size;
size_t url_size;
size_t query_string_size;
size_t method_size;
size_t remote_address_size;
size_t full_url_size;
size_t url_size;
size_t query_string_size;
size_t method_size;
size_t remote_address_size;
socketify_header* header_list;
socketify_header *header_list;
bool has_content;
} socketify_asgi_data;
bool has_content;
} socketify_asgi_data;
DLL_EXPORT typedef struct {
DLL_EXPORT typedef struct
{
const char* full_url;
const char* url;
const char* query_string;
const char* method;
const char* remote_address;
const char *full_url;
const char *url;
const char *query_string;
const char *method;
const char *remote_address;
size_t full_url_size;
size_t url_size;
size_t query_string_size;
size_t method_size;
size_t remote_address_size;
size_t full_url_size;
size_t url_size;
size_t query_string_size;
size_t method_size;
size_t remote_address_size;
const char* protocol;
const char* key;
const char* extensions;
size_t protocol_size;
size_t key_size;
size_t extensions_size;
const char *protocol;
const char *key;
const char *extensions;
size_t protocol_size;
size_t key_size;
size_t extensions_size;
socketify_header* header_list;
} socketify_asgi_ws_data;
socketify_header *header_list;
} socketify_asgi_ws_data;
DLL_EXPORT typedef void (*socketify_asgi_method_handler)(int ssl, uws_res_t *response, socketify_asgi_data request, void *user_data, bool* aborted);
DLL_EXPORT typedef void (*socketify_asgi_ws_method_handler)(int ssl, uws_res_t *response, socketify_asgi_ws_data request, uws_socket_context_t* socket, void *user_data, bool* aborted);
DLL_EXPORT typedef struct {
int ssl;
uws_app_t* app;
socketify_asgi_method_handler handler;
void * user_data;
} socksocketify_asgi_app_info;
DLL_EXPORT typedef void (*socketify_asgi_method_handler)(int ssl, uws_res_t *response, socketify_asgi_data request, void *user_data);
DLL_EXPORT typedef void (*socketify_asgi_ws_method_handler)(int ssl, uws_res_t *response, socketify_asgi_ws_data request, uws_socket_context_t *socket, void *user_data);
DLL_EXPORT typedef struct
{
int ssl;
uws_app_t *app;
socketify_asgi_method_handler handler;
void *user_data;
} socksocketify_asgi_app_info;
DLL_EXPORT typedef struct {
int ssl;
uws_app_t* app;
socketify_asgi_ws_method_handler handler;
uws_socket_behavior_t behavior;
void * user_data;
} socksocketify_asgi_ws_app_info;
DLL_EXPORT typedef struct
{
int ssl;
uws_app_t *app;
socketify_asgi_ws_method_handler handler;
uws_socket_behavior_t behavior;
void *user_data;
} socketify_asgi_ws_app_info;
DLL_EXPORT socketify_loop *socketify_create_loop();
DLL_EXPORT bool socketify_constructor_failed(socketify_loop *loop);
DLL_EXPORT bool socketify_on_prepare(socketify_loop *loop, socketify_prepare_handler handler, void *user_data);
DLL_EXPORT bool socketify_prepare_unbind(socketify_loop *loop);
DLL_EXPORT void socketify_destroy_loop(socketify_loop *loop);
DLL_EXPORT void *socketify_get_native_loop(socketify_loop *loop);
DLL_EXPORT socketify_loop * socketify_create_loop();
DLL_EXPORT bool socketify_constructor_failed(socketify_loop* loop);
DLL_EXPORT bool socketify_on_prepare(socketify_loop* loop, socketify_prepare_handler handler, void* user_data);
DLL_EXPORT bool socketify_prepare_unbind(socketify_loop* loop);
DLL_EXPORT void socketify_destroy_loop(socketify_loop* loop);
DLL_EXPORT void* socketify_get_native_loop(socketify_loop* loop);
DLL_EXPORT int socketify_loop_run(socketify_loop *loop, socketify_run_mode mode);
DLL_EXPORT void socketify_loop_stop(socketify_loop *loop);
DLL_EXPORT int socketify_loop_run(socketify_loop* loop, socketify_run_mode mode);
DLL_EXPORT void socketify_loop_stop(socketify_loop* loop);
DLL_EXPORT socketify_timer *socketify_create_timer(socketify_loop *loop, uint64_t timeout, uint64_t repeat, socketify_timer_handler handler, void *user_data);
DLL_EXPORT void socketify_timer_destroy(socketify_timer *timer);
DLL_EXPORT void socketify_timer_set_repeat(socketify_timer *timer, uint64_t repeat);
DLL_EXPORT socketify_timer* socketify_create_timer(socketify_loop* loop, uint64_t timeout, uint64_t repeat, socketify_timer_handler handler, void* user_data);
DLL_EXPORT void socketify_timer_destroy(socketify_timer* timer);
DLL_EXPORT void socketify_timer_set_repeat(socketify_timer* timer, uint64_t repeat);
DLL_EXPORT socketify_timer *socketify_create_check(socketify_loop *loop, socketify_timer_handler handler, void *user_data);
DLL_EXPORT void socketify_check_destroy(socketify_timer *timer);
DLL_EXPORT socketify_timer* socketify_create_check(socketify_loop* loop, socketify_timer_handler handler, void* user_data);
DLL_EXPORT void socketify_check_destroy(socketify_timer* timer);
DLL_EXPORT socketify_asgi_data socketify_asgi_request(int ssl, uws_req_t *req, uws_res_t *res);
DLL_EXPORT void socketify_destroy_headers(socketify_header *headers);
DLL_EXPORT bool socketify_res_write_int_status_with_headers(int ssl, uws_res_t *res, int code, socketify_header *headers);
DLL_EXPORT void socketify_res_write_headers(int ssl, uws_res_t *res, socketify_header *headers);
DLL_EXPORT bool socketify_res_write_int_status(int ssl, uws_res_t *res, int code);
DLL_EXPORT socketify_asgi_ws_data socketify_asgi_ws_request(int ssl, uws_req_t *req, uws_res_t *res);
DLL_EXPORT socketify_asgi_data socketify_asgi_request(int ssl, uws_req_t *req, uws_res_t *res);
DLL_EXPORT void socketify_destroy_headers(socketify_header* headers);
DLL_EXPORT bool socketify_res_write_int_status_with_headers(int ssl, uws_res_t* res, int code, socketify_header* headers);
DLL_EXPORT void socketify_res_write_headers(int ssl, uws_res_t* res, socketify_header* headers);
DLL_EXPORT bool socketify_res_write_int_status(int ssl, uws_res_t* res, int code);
DLL_EXPORT socketify_asgi_ws_data socketify_asgi_ws_request(int ssl, uws_req_t *req, uws_res_t *res);
DLL_EXPORT socksocketify_asgi_app_info *socketify_add_asgi_http_handler(int ssl, uws_app_t *app, socketify_asgi_method_handler handler, void *user_data);
DLL_EXPORT void socketify_destroy_asgi_app_info(socksocketify_asgi_app_info *app);
DLL_EXPORT socksocketify_asgi_app_info* socketify_add_asgi_http_handler(int ssl, uws_app_t* app, socketify_asgi_method_handler handler, void* user_data);
DLL_EXPORT void socketify_destroy_asgi_app_info(socksocketify_asgi_app_info* app);
DLL_EXPORT void socketify_res_cork_write(int ssl, uws_res_t *response, const char *data, size_t length);
DLL_EXPORT void socketify_res_cork_end(int ssl, uws_res_t *response, const char *data, size_t length, bool close_connection);
DLL_EXPORT void socketify_res_cork_write(int ssl, uws_res_t *response, const char* data, size_t length);
DLL_EXPORT void socketify_res_cork_end(int ssl, uws_res_t *response, const char* data, size_t length, bool close_connection);
DLL_EXPORT socksocketify_asgi_ws_app_info* socketify_add_asgi_ws_handler(int ssl, uws_app_t* app, uws_socket_behavior_t behavior, socketify_asgi_ws_method_handler handler, void* user_data);
DLL_EXPORT void socketify_destroy_asgi_ws_app_info(socksocketify_asgi_ws_app_info* app);
DLL_EXPORT void socketify_ws_cork_send(int ssl, uws_websocket_t *ws, const char* data, size_t length, uws_opcode_t opcode);
DLL_EXPORT void socketify_ws_cork_send_with_options(int ssl, uws_websocket_t *ws, const char* data, size_t length, uws_opcode_t opcode, bool compress, bool fin);
DLL_EXPORT socketify_asgi_ws_app_info *socketify_add_asgi_ws_handler(int ssl, uws_app_t *app, uws_socket_behavior_t behavior, socketify_asgi_ws_method_handler handler, void *user_data);
DLL_EXPORT void socketify_destroy_asgi_ws_app_info(socketify_asgi_ws_app_info *app);
DLL_EXPORT void socketify_ws_cork_send(int ssl, uws_websocket_t *ws, const char *data, size_t length, uws_opcode_t opcode);
DLL_EXPORT void socketify_ws_cork_send_with_options(int ssl, uws_websocket_t *ws, const char *data, size_t length, uws_opcode_t opcode, bool compress, bool fin);
DLL_EXPORT void socketify_res_send_int_code(int ssl, uws_res_t *res, const char *content_data, size_t content_data_size, int code, const char *content_type, size_t content_type_size, bool close_connection);
DLL_EXPORT void socketify_res_send(int ssl, uws_res_t *res, const char *content_data, size_t content_data_size, const char *status_code, size_t status_code_size, const char *content_type, size_t content_type_size, bool close_connection);
DLL_EXPORT void socketify_res_cork_send_int_code(int ssl, uws_res_t *res, const char *content_data, size_t content_data_size, int code, const char *content_type, size_t content_type_size, bool close_connection);
DLL_EXPORT void socketify_res_cork_send(int ssl, uws_res_t *res, const char *content_data, size_t content_data_size, const char *status_code, size_t status_code_size, const char *content_type, size_t content_type_size, bool close_connection);
#endif
#ifdef __cplusplus
}

Wyświetl plik

@ -25,7 +25,7 @@ class SSGIHttpResponse:
# send chunk of data, can be used to perform with less backpressure than using send
# total_size is the sum of all lengths in bytes of all chunks to be sended
# connection will end when total_size is met
# returns tuple(bool, bool) first bool represents if the chunk is succefully sended, the second if the connection has ended
# returns tuple(bool, bool) first bool represents if the chunk is successfully sended, the second if the connection has ended
def send_chunk(self, chunk: Union[bytes, bytearray, memoryview], total_size: int) -> Awaitable:
return self.res.send_chunk(chunk, total_size)

Wyświetl plik

@ -6,6 +6,7 @@ from asyncio import (
futures,
_register_task,
_enter_task,
current_task,
_leave_task,
_unregister_task,
)
@ -36,7 +37,9 @@ class RequestTask:
Differences:
- This class is only used by socketify.py loop.run_async
- This class do not support current_task
- This class executes the first step like node.js Promise
- This class is not thread-safe.
@ -89,6 +92,7 @@ class RequestTask:
# status is still pending
_log_destroy_pending = True
_parent_task = None
def __init__(
self, coro, loop, default_done_callback=None, no_register=False, context=None
):
@ -115,8 +119,12 @@ class RequestTask:
self._log_destroy_pending = False
if self._loop.get_debug():
self._source_traceback = format_helpers.extract_stack(sys._getframe(1))
self._loop.call_soon(self.__step, context=self._context)
_register_task(self)
if loop.is_running():
self.__step()
else:
self._loop.call_soon(self.__step, context=self._context)
def _reuse(self, coro, loop, default_done_callback=None):
"""Reuse an future that is not pending anymore."""
@ -147,8 +155,12 @@ class RequestTask:
self._fut_waiter = None
self._coro = coro
self._loop.call_soon(self.__step, context=self._context)
_register_task(self)
# if current_task():
# self._loop.call_soon(self.__step, context=self._context)
# else:
self.__step()
def __repr__(self):
return base_tasks._task_repr(self)
@ -487,7 +499,11 @@ class RequestTask:
self._must_cancel = False
coro = self._coro
self._fut_waiter = None
_parent_task = current_task(self._loop)
if _parent_task is not None:
_leave_task(self._loop, _parent_task)
self._parent_task = _parent_task
_enter_task(self._loop, self)
# Call either coro.throw(exc) or coro.send(None).
try:
@ -559,6 +575,9 @@ class RequestTask:
self._loop.call_soon(self.__step, new_exc, context=self._context)
finally:
_leave_task(self._loop, self)
if self._parent_task is not None:
_enter_task(self._loop, self._parent_task)
self._parent_task = None
self = None # Needed to break cycles when an exception occurs.
def __wakeup(self, future):
@ -580,29 +599,29 @@ class RequestTask:
__iter__ = __await__ # make compatible with 'yield from'.
def create_task_with_factory(task_factory_max_items=100_000):
items = []
for _ in range(0, task_factory_max_items):
task = RequestTask(None, None, None, True)
if task._source_traceback:
del task._source_traceback[-1]
items.append(task)
async def factory_task_wrapper(task, dispose):
try:
await task
finally:
dispose()
def factory(loop, coro, default_done_callback=None):
if len(items) == 0:
return create_task(loop, coro, default_done_callback)
task = items.pop()
class TaskFactory:
def __init__(self, task_factory_max_items=100_000):
self.items = []
for _ in range(0, task_factory_max_items):
task = RequestTask(None, None, None, True)
if task._source_traceback:
del task._source_traceback[-1]
self.items.append(task)
def done(f):
if default_done_callback is not None:
default_done_callback(f)
items.append(f)
def __call__(self, loop, coro):
if len(self.items) == 0:
return create_task(loop, coro)
task = self.items.pop()
task._reuse(coro, loop, done)
task._reuse(factory_task_wrapper(coro, lambda : self.items.append(task)), loop)
return task
return factory
def create_task(loop, coro, default_done_callback=None, context=None):
"""Schedule a coroutine object.

@ -1 +1 @@
Subproject commit 7187fc3d658d4335cdf0c79371eeb8310717b95c
Subproject commit f33291d10a2051ba0f10c18761c030fb00390fdf

Wyświetl plik

@ -1,3 +1,4 @@
from .native import ffi, lib
@ -90,7 +91,7 @@ class UVLoop:
def run(self):
if self._loop != ffi.NULL:
return lib.socketify_loop_run(self._loop, lib.SOCKETIFY_RUN_DEFAULT)
def run_once(self):
if self._loop != ffi.NULL:
return lib.socketify_loop_run(self._loop, lib.SOCKETIFY_RUN_ONCE)

Wyświetl plik

@ -7,14 +7,16 @@ from .native import lib, ffi
import platform
is_pypy = platform.python_implementation() == "PyPy"
from .tasks import create_task, create_task_with_factory
from .tasks import create_task, TaskFactory
import sys
import logging
import uuid
@ffi.callback("void(uws_res_t*, const char*, size_t, bool, void*)")
def wsgi_on_data_handler(res, chunk, chunk_length, is_end, user_data):
data_response = ffi.from_handle(user_data)
data_response.app.server.loop.is_idle = False
if chunk != ffi.NULL:
data_response.buffer.write(ffi.unpack(chunk, chunk_length))
if bool(is_end):
@ -25,6 +27,87 @@ def wsgi_on_data_handler(res, chunk, chunk_length, is_end, user_data):
data_response._ptr,
)
@ffi.callback("void(uws_res_t*, void*)")
def wsgi_on_data_ref_abort_handler(res, user_data):
data_retry = ffi.from_handle(user_data)
data_retry.aborted = True
data_retry.server.loop.is_idle = False
if data_retry.id is not None:
data_retry.app._data_refs.pop(data_retry.id, None)
@ffi.callback("bool(uws_res_t*, uintmax_t, void*)")
def wsgi_on_writable_handler(res, offset, user_data):
data_retry = ffi.from_handle(user_data)
if data_retry.aborted:
return False
chunks = data_retry.chunks
last_sended_offset = data_retry.last_offset
server = data_retry.app.server
ssl = server.SSL
server.loop.is_idle = False
content_length = data_retry.content_length
data = chunks[0]
data_size = len(data)
last_offset = int(lib.uws_res_get_write_offset(ssl, res))
if last_sended_offset != last_offset:
offset = last_offset - last_sended_offset
data = data[offset:data_size]
data_size = len(data)
if data_size == 0:
chunks.pop(0)
if len(chunks) == 0:
logging.error(AssertionError("Content-Length do not match sended content"))
lib.uws_res_close(
ssl,
res
)
if data_retry.id is not None:
data_retry.app._data_refs.pop(data_retry.id, None)
return True
data = chunks[0]
result = lib.uws_res_try_end(
ssl,
res,
data,
data_size,
content_length,
0,
)
has_responded = bool(result.has_responded)
ok = bool(result.ok)
data_retry.last_offset = int(lib.uws_res_get_write_offset(ssl, res))
if ok:
chunks.pop(0)
if not has_responded and len(chunks) == 0:
logging.error(AssertionError("Content-Length do not match sended content"))
lib.uws_res_close(
ssl,
res
)
if data_retry.id is not None:
data_retry.app._data_refs.pop(data_retry.id, None)
elif has_responded and data_retry.id is not None:
data_retry.app._data_refs.pop(data_retry.id, None)
elif not has_responded and len(chunks) == 0:
logging.error(AssertionError("Content-Length do not match sended content"))
lib.uws_res_close(
ssl,
res
)
if data_retry.id is not None:
data_retry.app._data_refs.pop(data_retry.id, None)
elif has_responded and data_retry.id is not None:
data_retry.app._data_refs.pop(data_retry.id, None)
return ok
class WSGIBody:
def __init__(self, buffer):
@ -115,14 +198,25 @@ class WSGIBody:
class WSGIDataResponse:
def __init__(self, app, environ, start_response, aborted, buffer, on_data):
def __init__(self, app, environ, start_response, buffer, on_data):
self.buffer = buffer
self.aborted = aborted
self._ptr = ffi.new_handle(self)
self.on_data = on_data
self.environ = environ
self.app = app
self.start_response = start_response
self.id = None
self.aborted = False
class WSGIRetryDataSend:
def __init__(self, app, chunks, content_length, last_offset):
self.chunks = chunks
self._ptr = ffi.new_handle(self)
self.app = app
self.content_length = content_length
self.last_offset = last_offset
self.id = None
self.aborted = False
@ffi.callback("void(uws_res_t*, void*)")
@ -131,9 +225,12 @@ def wsgi_corked_response_start_handler(res, user_data):
data_response.on_data(data_response, res)
@ffi.callback("void(int, uws_res_t*, socketify_asgi_data request, void*, bool*)")
def wsgi(ssl, response, info, user_data, aborted):
@ffi.callback("void(int, uws_res_t*, socketify_asgi_data request, void*)")
def wsgi(ssl, response, info, user_data):
app = ffi.from_handle(user_data)
app.server.loop.is_idle = False
# reusing the dict is slower than cloning because we need to clear HTTP headers
environ = dict(app.BASIC_ENVIRON)
@ -141,7 +238,7 @@ def wsgi(ssl, response, info, user_data, aborted):
environ["PATH_INFO"] = ffi.unpack(info.url, info.url_size).decode("utf8")
environ["QUERY_STRING"] = ffi.unpack(
info.query_string, info.query_string_size
).decode("utf8")
).decode("utf8")[1:]
if info.remote_address != ffi.NULL:
environ["REMOTE_ADDR"] = ffi.unpack(
info.remote_address, info.remote_address_size
@ -163,11 +260,13 @@ def wsgi(ssl, response, info, user_data, aborted):
headers_set = None
headers_written = False
status_text = None
is_chunked = False
content_length = -1
def write_headers(headers):
nonlocal headers_written, headers_set, status_text
nonlocal headers_written, headers_set, status_text, content_length, is_chunked, app
if headers_written or not headers_set:
return
app.server.loop.is_idle = False
headers_written = True
@ -183,20 +282,33 @@ def wsgi(ssl, response, info, user_data, aborted):
if (
key == "content-length"
or key == "Content-Length"
or key == "Transfer-Encoding"
):
content_length = int(value)
continue # auto generated by try_end
if (
key == "Transfer-Encoding"
or key == "transfer-encoding"
):
continue # auto
is_chunked = str(value) == "chunked"
if is_chunked:
continue
key_data = key.encode("utf-8")
elif isinstance(key, bytes):
# this is faster than using .lower()
if (
key == b"content-length"
or key == b"Content-Length"
or key == b"Transfer-Encoding"
):
content_length = int(value)
continue # auto
if (
key == b"Transfer-Encoding"
or key == b"transfer-encoding"
):
continue # auto
is_chunked = str(value) == "chunked"
if is_chunked:
continue
key_data = key
if isinstance(value, str):
@ -216,9 +328,14 @@ def wsgi(ssl, response, info, user_data, aborted):
lib.uws_res_write_header(
ssl, response, key_data, len(key_data), value_data, len(value_data)
)
# no content-length
if content_length < 0:
is_chunked = True
content_length = ffi.cast("uintmax_t", content_length)
def start_response(status, headers, exc_info=None):
nonlocal headers_set, status_text
nonlocal headers_set, status_text, app
app.server.loop.is_idle = False
if exc_info:
try:
if headers_written:
@ -233,9 +350,13 @@ def wsgi(ssl, response, info, user_data, aborted):
status_text = status
def write(data):
nonlocal is_chunked, app
app.server.loop.is_idle = False
if not headers_written:
write_headers(headers_set)
# will allow older frameworks only with is_chunked
is_chunked = True
if isinstance(data, bytes):
lib.uws_res_write(ssl, response, data, len(data))
elif isinstance(data, str):
@ -244,32 +365,69 @@ def wsgi(ssl, response, info, user_data, aborted):
return write
# check for body
if bool(info.has_content):
WSGI_INPUT = BytesIO()
environ["wsgi.input"] = WSGIBody(WSGI_INPUT)
def on_data(data_response, response):
if bool(data_response.aborted[0]):
return
last_offset = -1
data_retry = None
failed_chunks = None
if data_response.aborted:
return
data_response.app.server.loop.is_idle = False
ssl = data_response.app.server.SSL
data_response.environ["CONTENT_LENGTH"] = str(
data_response.buffer.getbuffer().nbytes
)
if data_response.id is not None:
data_response.app._data_refs.pop(data_response.id, None)
app_iter = data_response.app.wsgi(
data_response.environ, data_response.start_response
)
try:
for data in app_iter:
if data and not headers_written:
write_headers(headers_set)
if isinstance(data, bytes):
lib.uws_res_write(ssl, response, data, len(data))
elif isinstance(data, str):
data = data.encode("utf-8")
lib.uws_res_write(ssl, response, data, len(data))
if data:
if not headers_written:
write_headers(headers_set)
if is_chunked:
if isinstance(data, bytes):
lib.uws_res_write(ssl, response, data, len(data))
elif isinstance(data, str):
data = data.encode("utf-8")
lib.uws_res_write(ssl, response, data, len(data))
else:
if isinstance(data, str):
data = data.encode("utf-8")
if failed_chunks:
failed_chunks.append(data)
else:
last_offset = int(lib.uws_res_get_write_offset(ssl, response))
result = lib.uws_res_try_end(
ssl,
response,
data,
len(data),
content_length,
0,
)
# this should be very very rare for HTTP
if not bool(result.ok):
failed_chunks = []
# just mark the chunks
failed_chunks.append(data)
# add on writable handler
data_retry = WSGIRetryDataSend(
app, failed_chunks, content_length, last_offset
)
except Exception as error:
logging.exception(error)
@ -278,27 +436,76 @@ def wsgi(ssl, response, info, user_data, aborted):
app_iter.close()
if not headers_written:
write_headers(headers_set)
lib.uws_res_end_without_body(ssl, response, 0)
write_headers(headers_set)
if is_chunked:
lib.uws_res_end_without_body(ssl, response, 0)
elif data_retry is not None:
_id = uuid.uuid4()
app._data_refs[_id] = data_retry
lib.uws_res_on_aborted(ssl, response, wsgi_on_data_ref_abort_handler, data_retry._ptr)
lib.uws_res_on_writable(ssl, response, wsgi_on_writable_handler, data_retry._ptr)
elif result is None or (not bool(result.has_responded) and bool(result.ok)): # not reaches Content-Length
logging.error(AssertionError("Content-Length do not match sended content"))
lib.uws_res_close(
ssl,
response
)
data_response = WSGIDataResponse(
app, environ, start_response, aborted, WSGI_INPUT, on_data
app, environ, start_response, WSGI_INPUT, on_data
)
_id = uuid.uuid4()
data_response.id = _id
app._data_refs[_id] = data_response
lib.uws_res_on_aborted(ssl, response, wsgi_on_data_ref_abort_handler, data_response._ptr)
lib.uws_res_on_data(ssl, response, wsgi_on_data_handler, data_response._ptr)
else:
environ["wsgi.input"] = None
failed_chunks = None
last_offset = -1
data_retry = None
# Django do not check for None with is lame
# we use the same empty for everyone to avoid extra allocations
environ["wsgi.input"] = app.EMPTY_WSGI_BODY
# we also set CONTENT_LENGTH to 0 so if Django is lame again its covered
environ["CONTENT_LENGTH"] = "0"
app_iter = app.wsgi(environ, start_response)
result = None
try:
for data in app_iter:
if data and not headers_written:
write_headers(headers_set)
if data:
if not headers_written:
write_headers(headers_set)
if is_chunked:
if isinstance(data, bytes):
lib.uws_res_write(ssl, response, data, len(data))
elif isinstance(data, str):
data = data.encode("utf-8")
lib.uws_res_write(ssl, response, data, len(data))
else:
if isinstance(data, str):
data = data.encode("utf-8")
if failed_chunks: # if failed once, will fail again later
failed_chunks.append(data)
else:
last_offset = int(lib.uws_res_get_write_offset(ssl, response))
result = lib.uws_res_try_end(
ssl,
response,
data,
len(data),
content_length,
0,
)
# this should be very very rare for HTTP
if not bool(result.ok):
failed_chunks = []
# just mark the chunks
failed_chunks.append(data)
# add on writable handler
data_retry = WSGIRetryDataSend(
app, failed_chunks, content_length, last_offset
)
if isinstance(data, bytes):
lib.uws_res_write(ssl, response, data, len(data))
elif isinstance(data, str):
data = data.encode("utf-8")
lib.uws_res_write(ssl, response, data, len(data))
except Exception as error:
logging.exception(error)
finally:
@ -306,8 +513,21 @@ def wsgi(ssl, response, info, user_data, aborted):
app_iter.close()
if not headers_written:
write_headers(headers_set)
lib.uws_res_end_without_body(ssl, response, 0)
write_headers(headers_set)
if is_chunked:
lib.uws_res_end_without_body(ssl, response, 0)
elif data_retry is not None:
_id = uuid.uuid4()
data_retry.id = _id
app._data_refs[_id] = data_retry
lib.uws_res_on_aborted(ssl, response, wsgi_on_data_ref_abort_handler, data_retry._ptr)
lib.uws_res_on_writable(ssl, response, wsgi_on_writable_handler, data_retry._ptr)
elif result is None or (not bool(result.has_responded) and bool(result.ok)): # not reaches Content-Length
logging.error(AssertionError("Content-Length do not match sended content"))
lib.uws_res_close(
ssl,
response
)
def is_asgi(module):
@ -328,11 +548,12 @@ class _WSGI:
self.server = App(options, task_factory_max_items=0)
self.SERVER_HOST = None
self.SERVER_PORT = None
self.SERVER_WS_SCHEME = "wss" if self.server.options else "ws"
self.SERVER_WS_SCHEME = "wss" if self.server._options else "ws"
self.wsgi = app
self.EMPTY_WSGI_BODY = WSGIBody(BytesIO())
self.BASIC_ENVIRON = dict(os.environ)
self.ws_compression = False
self._data_refs = {}
self._ptr = ffi.new_handle(self)
self.asgi_http_info = lib.socketify_add_asgi_http_handler(
self.server.SSL, self.server.app, wsgi, self._ptr
@ -351,18 +572,17 @@ class _WSGI:
# internally will still use custom task factory for pypy because of Loop
if is_pypy:
if task_factory_max_items > 0:
factory = create_task_with_factory(task_factory_max_items)
factory = TaskFactory(task_factory_max_items)
def run_task(task):
factory(loop, task)
loop._run_once()
self._run_task = run_task
else:
def run_task(task):
create_task(loop, task)
loop._run_once()
future = create_task(loop, task)
future._log_destroy_pending = False
self._run_task = run_task
@ -370,15 +590,15 @@ class _WSGI:
if sys.version_info >= (3, 8): # name fixed to avoid dynamic name
def run_task(task):
loop.create_task(task, name="socketify.py-request-task")
loop._run_once()
future = create_task(loop, task)
future._log_destroy_pending = False
self._run_task = run_task
else:
def run_task(task):
loop.create_task(task)
loop._run_once()
future = create_task(loop, task)
future._log_destroy_pending = False
self._run_task = run_task
@ -451,7 +671,7 @@ class _WSGI:
"wsgi.errors": sys.stderr,
"wsgi.version": (1, 0),
"wsgi.run_once": False,
"wsgi.url_scheme": "https" if self.server.options else "http",
"wsgi.url_scheme": "https" if self.server._options and self.server._options.cert_file_name is not None else "http",
"wsgi.multithread": False,
"wsgi.multiprocess": False,
"wsgi.file_wrapper": None, # No file wrapper support for now
@ -471,10 +691,13 @@ class _WSGI:
return self
def __del__(self):
if self.asgi_http_info:
lib.socketify_destroy_asgi_app_info(self.asgi_http_info)
if self.asgi_ws_info:
lib.socketify_destroy_asgi_ws_app_info(self.asgi_ws_info)
try:
if self.asgi_http_info:
lib.socketify_destroy_asgi_app_info(self.asgi_http_info)
if self.asgi_ws_info:
lib.socketify_destroy_asgi_ws_app_info(self.asgi_ws_info)
except:
pass
# "Public" WSGI interface to allow easy forks/workers
@ -494,14 +717,28 @@ class WSGI:
self.websocket_options = websocket_options
self.listen_options = None
self.task_factory_max_items = task_factory_max_items
self.server = None
self.pid_list = None
# lifespan is not supported in WSGI
def listen(self, port_or_options, handler=None):
self.listen_options = (port_or_options, handler)
return self
def run(self, workers=1):
def run_app():
def close(self):
# always wait a sec so forks can start properly if close is called too fast
import time
time.sleep(1)
if self.server is not None:
self.server.close()
if self.pid_list is not None:
import signal
for pid in self.pid_list:
os.kill(pid, signal.SIGINT)
def run(self, workers=1, block=True):
def run_task():
server = _WSGI(
self.app,
self.options,
@ -512,17 +749,28 @@ class WSGI:
if self.listen_options:
(port_or_options, handler) = self.listen_options
server.listen(port_or_options, handler)
self.server = server
server.run()
def create_fork():
n = os.fork()
# n greater than 0 means parent process
if not n > 0:
run_app()
pid_list = []
start = 1 if block else 0
# fork limiting the cpu count - 1
for i in range(1, workers):
create_fork()
for _ in range(start, workers):
pid = os.fork()
# n greater than 0 means parent process
if not pid > 0:
run_task()
break
pid_list.append(pid)
self.pid_list = pid_list
if block:
run_task() # run app on the main process too :)
# sigint everything to graceful shutdown
import signal
for pid in pid_list:
os.kill(pid, signal.SIGINT)
run_app() # run app on the main process too :)
return self