kopia lustrzana https://github.com/cirospaciari/socketify.py
optimize send a little bit
rodzic
f04a5fd642
commit
a84dba6eab
|
@ -4,10 +4,12 @@ import multiprocessing
|
||||||
import asyncio
|
import asyncio
|
||||||
def run_app():
|
def run_app():
|
||||||
app = App(request_response_factory_max_items=200_000)
|
app = App(request_response_factory_max_items=200_000)
|
||||||
async def home(res, req):
|
router = app.router()
|
||||||
res.end("Hello, World!")
|
|
||||||
|
@router.get("/")
|
||||||
|
def home(res, req):
|
||||||
|
res.send(b"Hello, World!")
|
||||||
|
|
||||||
app.get("/", home)
|
|
||||||
app.listen(
|
app.listen(
|
||||||
8000,
|
8000,
|
||||||
lambda config: print(
|
lambda config: print(
|
||||||
|
|
|
@ -61,7 +61,7 @@ class AppResponse:
|
||||||
def get_proxied_remote_address_bytes(self):
|
def get_proxied_remote_address_bytes(self):
|
||||||
def get_proxied_remote_address(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 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 end(self, message, end_connection=False):
|
||||||
def pause(self):
|
def pause(self):
|
||||||
def resume(self):
|
def resume(self):
|
||||||
|
|
|
@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
||||||
|
|
||||||
[project]
|
[project]
|
||||||
name = "socketify"
|
name = "socketify"
|
||||||
version = "0.0.12"
|
version = "0.0.13"
|
||||||
authors = [
|
authors = [
|
||||||
{ name="Ciro Spaciari", email="ciro.spaciari@gmail.com" },
|
{ name="Ciro Spaciari", email="ciro.spaciari@gmail.com" },
|
||||||
]
|
]
|
||||||
|
|
2
setup.py
2
setup.py
|
@ -58,7 +58,7 @@ with open("README.md", "r", encoding="utf-8") as fh:
|
||||||
|
|
||||||
setuptools.setup(
|
setuptools.setup(
|
||||||
name="socketify",
|
name="socketify",
|
||||||
version="0.0.12",
|
version="0.0.13",
|
||||||
platforms=["any"],
|
platforms=["any"],
|
||||||
author="Ciro Spaciari",
|
author="Ciro Spaciari",
|
||||||
author_email="ciro.spaciari@gmail.com",
|
author_email="ciro.spaciari@gmail.com",
|
||||||
|
|
|
@ -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_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);
|
||||||
"""
|
"""
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
Plik diff jest za duży
Load Diff
|
@ -138,6 +138,10 @@ DLL_EXPORT socksocketify_asgi_ws_app_info* socketify_add_asgi_ws_handler(int ssl
|
||||||
DLL_EXPORT void socketify_destroy_asgi_ws_app_info(socksocketify_asgi_ws_app_info* app);
|
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(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_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
|
#endif
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
|
|
@ -1733,6 +1733,7 @@ class AppResponse:
|
||||||
headers=None,
|
headers=None,
|
||||||
end_connection: bool = False,
|
end_connection: bool = False,
|
||||||
):
|
):
|
||||||
|
# TODO: use socketify_res_cork_send_int_code and socketify_res_cork_send after optimize headers
|
||||||
self.cork(
|
self.cork(
|
||||||
lambda res: res.send(message, content_type, status, headers, end_connection)
|
lambda res: res.send(message, content_type, status, headers, end_connection)
|
||||||
)
|
)
|
||||||
|
@ -1740,42 +1741,107 @@ class AppResponse:
|
||||||
|
|
||||||
def send(
|
def send(
|
||||||
self,
|
self,
|
||||||
message: any,
|
message: any = b"",
|
||||||
content_type: Union[str, bytes] = b"text/plain",
|
content_type: Union[str, bytes] = b"text/plain",
|
||||||
status: Union[str, bytes, int] = b"200 OK",
|
status: Union[str, bytes, int] = b"200 OK",
|
||||||
headers=None,
|
headers = None,
|
||||||
end_connection: bool = False,
|
end_connection: bool = False,
|
||||||
):
|
):
|
||||||
if self.aborted:
|
|
||||||
return self
|
|
||||||
|
|
||||||
self.write_status(status)
|
|
||||||
|
|
||||||
|
# TODO: optimize headers
|
||||||
if headers is not None:
|
if headers is not None:
|
||||||
for name, value in headers:
|
for name, value in headers:
|
||||||
self.write_header(name, value)
|
self.write_header(name, value)
|
||||||
try:
|
try:
|
||||||
|
|
||||||
|
# TODO: optimize Set-Cookie
|
||||||
if self._write_jar is not None:
|
if self._write_jar is not None:
|
||||||
self.write_header("Set-Cookie", self._write_jar.output(header=""))
|
self.write_header("Set-Cookie", self._write_jar.output(header=""))
|
||||||
self._write_jar = None
|
self._write_jar = None
|
||||||
|
|
||||||
if isinstance(message, str):
|
if isinstance(message, str):
|
||||||
data = message.encode("utf-8")
|
data = message.encode("utf-8")
|
||||||
self.write_header(b"Content-Type", content_type)
|
|
||||||
elif isinstance(message, bytes):
|
elif isinstance(message, bytes):
|
||||||
self.write_header(b"Content-Type", content_type)
|
|
||||||
data = message
|
data = message
|
||||||
elif message is None:
|
elif message is None:
|
||||||
self.write_header(b"Content-Type", content_type)
|
if isinstance(status, int):
|
||||||
self.end_without_body(end_connection)
|
lib.socketify_res_send_int_code(
|
||||||
|
self.app.SSL,
|
||||||
|
self.res,
|
||||||
|
ffi.NULL,
|
||||||
|
0,
|
||||||
|
status,
|
||||||
|
content_type,
|
||||||
|
len(content_type),
|
||||||
|
1 if end_connection else 0,
|
||||||
|
)
|
||||||
|
elif isinstance(status, str):
|
||||||
|
status = status.encode("utf-8")
|
||||||
|
lib.socketify_res_send(
|
||||||
|
self.app.SSL,
|
||||||
|
self.res,
|
||||||
|
ffi.NULL,
|
||||||
|
0,
|
||||||
|
status,
|
||||||
|
len(status),
|
||||||
|
content_type,
|
||||||
|
len(content_type),
|
||||||
|
1 if end_connection else 0,
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
lib.socketify_res_send(
|
||||||
|
self.app.SSL,
|
||||||
|
self.res,
|
||||||
|
ffi.NULL,
|
||||||
|
0,
|
||||||
|
status,
|
||||||
|
len(status),
|
||||||
|
content_type,
|
||||||
|
len(content_type),
|
||||||
|
1 if end_connection else 0,
|
||||||
|
)
|
||||||
return self
|
return self
|
||||||
else:
|
else:
|
||||||
data = self.app._json_serializer.dumps(message).encode("utf-8")
|
data = self.app._json_serializer.dumps(message).encode("utf-8")
|
||||||
# ignores content_type should always be json here
|
content_type = b"application/json"
|
||||||
self.write_header(b"Content-Type", b"application/json")
|
|
||||||
|
if isinstance(status, int):
|
||||||
|
lib.socketify_res_send_int_code(
|
||||||
|
self.app.SSL,
|
||||||
|
self.res,
|
||||||
|
data,
|
||||||
|
len(data),
|
||||||
|
status,
|
||||||
|
content_type,
|
||||||
|
len(content_type),
|
||||||
|
1 if end_connection else 0,
|
||||||
|
)
|
||||||
|
elif isinstance(status, str):
|
||||||
|
status = status.encode("utf-8")
|
||||||
|
lib.socketify_res_send(
|
||||||
|
self.app.SSL,
|
||||||
|
self.res,
|
||||||
|
ffi.NULL,
|
||||||
|
0,
|
||||||
|
status,
|
||||||
|
len(status),
|
||||||
|
content_type,
|
||||||
|
len(content_type),
|
||||||
|
1 if end_connection else 0,
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
lib.socketify_res_send(
|
||||||
|
self.app.SSL,
|
||||||
|
self.res,
|
||||||
|
data,
|
||||||
|
len(data),
|
||||||
|
status,
|
||||||
|
len(status),
|
||||||
|
content_type,
|
||||||
|
len(content_type),
|
||||||
|
1 if end_connection else 0,
|
||||||
|
)
|
||||||
|
|
||||||
lib.uws_res_end(
|
|
||||||
self.app.SSL, self.res, data, len(data), 1 if end_connection else 0
|
|
||||||
)
|
|
||||||
finally:
|
finally:
|
||||||
return self
|
return self
|
||||||
|
|
||||||
|
@ -2506,7 +2572,6 @@ class App:
|
||||||
self._native_options.append(cert_file_name)
|
self._native_options.append(cert_file_name)
|
||||||
socket_options.cert_file_name = cert_file_name
|
socket_options.cert_file_name = cert_file_name
|
||||||
|
|
||||||
|
|
||||||
passphrase = (
|
passphrase = (
|
||||||
ffi.NULL
|
ffi.NULL
|
||||||
if options.passphrase is None
|
if options.passphrase is None
|
||||||
|
|
Ładowanie…
Reference in New Issue