mummy README

README проекта с github.com/guzba/mummy. Одним файлом markdown: README.md.

Mummy

nimble install mummy

Github Actions

API reference

Mummy is a multi-threaded HTTP 1.1 and WebSocket server written entirely in Nim.

A return to the ancient ways of threads.

Mummy has been written specifically to maximize the performance of your server hardware without compromising on programmer happiness.

Mummy requires --threads:on and --mm:orc or --mm:arc.

The Mummy name refers to historical Egypt stuff.

Sites using Mummy

Other libraries built to work with Mummy

How is Mummy different?

Mummy operates with this basic model: handle all socket IO on one thread and dispatch incoming HTTP requests and WebSocket events to a pool of worker threads. Your HTTP handlers probably won't even need to think about threads at all.

This model has many great benefits and is ready to take advantage of continued server core count increases (AMD just announced a 96 core 192 thread server CPU!).

Why use Mummy instead of async?

Why prioritize WebSockets?

WebSockets are wonderful and can have substantial advantages over more traditional API paradigms like REST and various flavors of RPC.

Unfortunately, most HTTP servers pretend WebSockets don't exist.

This means developers need to hack support in through additional dependencies, hijacking connections etc and it all rarely adds up into something really great.

I see no reason why Websockets should not work exceptionally well right out of the box, saving developers a lot of uncertainty and time researching which of the possible ways to wedge WebSocket support in to an HTTP server is "best".

What is Mummy not great for?

Everything comes with trade-offs. Mummy is focused on being an exceptional API server. Think REST, JSON, RPC, WebSockets, HTML from templates etc.

The property these share in common is they are all relatively memory-light. Most things are, which is great, but if you're specifically going to be serving a lot of very large files or expect large file uploads, Mummy is probably not the best choice unless your server has the RAM to handle the large files.

Why is Mummy not great for large files? This is because Mummy dispatches fully received in-memory requests to worker threads and sends in-memory responses. This is great for everything except very large files.

Example HTTP server

import mummy, mummy/routers

proc indexHandler(request: Request) =
  var headers: HttpHeaders
  headers["Content-Type"] = "text/plain"
  request.respond(200, headers, "Hello, World!")

var router: Router
router.get("/", indexHandler)

let server = newServer(router)
echo "Serving on http://localhost:8080"
server.serve(Port(8080))

nim c --threads:on --mm:orc -r examples/basic_router.nim

Example WebSocket server

import mummy, mummy/routers

proc indexHandler(request: Request) =
  var headers: HttpHeaders
  headers["Content-Type"] = "text/html"
  request.respond(200, headers, """
  <script>
    var ws = new WebSocket("ws://localhost:8080/ws");
    ws.onmessage = function (event) {
      document.body.innerHTML = event.data;
    };
  </script>
  """)

proc upgradeHandler(request: Request) =
  let websocket = request.upgradeToWebSocket()
  websocket.send("Hello world from WebSocket!")

proc websocketHandler(
  websocket: WebSocket,
  event: WebSocketEvent,
  message: Message
) =
  case event:
  of OpenEvent:
    discard
  of MessageEvent:
    echo message.kind, ": ", message.data
  of ErrorEvent:
    discard
  of CloseEvent:
    discard

var router: Router
router.get("/", indexHandler)
router.get("/ws", upgradeHandler)

let server = newServer(router, websocketHandler)
echo "Serving on http://localhost:8080"
server.serve(Port(8080))

See the examples/ folder for more sample code, including an example WebSocket chat server.

nim c --threads:on --mm:orc -r examples/basic_websockets.nim

Performance

Benchmarking HTTP servers is a bit like benchmarking running shoes.

Certainly, there are some terrible shoes to run in (heels, clogs, etc), but once you're in a reasonable pair of shoes it is the runner that's going to matter, not the shoes.

In this analogy, the runner is what your handlers are actually doing and the shoes are the HTTP server choice.

With that in mind, I suggest three priorities:

  1. Ensure your HTTP server choice does not unnecessarily hamper performance.

  2. Avoid HTTP servers that have easy performance vulnerabilities.

  3. Prioritize what will enable you to write and maintain performant and reliable handlers.

I believe Mummy clears all three priorities:

  1. Mummy prioritizes efficiency in receiving and dispatching incoming requests and sending outgoing responses. This means things like avoiding unnecessary memory copying, ensuring the CPU spends all of its time in your handlers.

  2. Because Mummy uses multiplexed IO just like async, Mummy is not vulnerable to attacks like low-and-slow which traditionally multi-threaded servers are vulnerable to. Additionally, while a single blocking or CPU heavy operation can stall an entire async server, this is not a problem for Mummy.

  3. Request handlers with Mummy are just plain-old inline Nim code. They have a straightforward request-in-response-out API. Keeping things simple is great for maintenance, reliability and performance.

Benchmarks

Benchmarking was done on an Ubuntu 22.04 server with a 4 core / 8 thread CPU.

The tests/wrk_ servers that are being benchmarked attempt to simulate requests that take ~10ms to complete.

All benchmarks were tested by:

wrk -t10 -c100 -d10s http://localhost:8080

The exact commands for each server are:

Mummy

nim c --mm:orc --threads:on -d:release -r tests/wrk_mummy.nim

Requests/sec: 9,547.56

AsyncHttpServer

nim c --mm:orc --threads:off -d:release -r tests/wrk_asynchttpserver.nim

Requests/sec: 7,979.67

HttpBeast

nim c --mm:orc --threads:on -d:release -r tests/wrk_httpbeast.nim

Requests/sec: 9,862.00

Jester

nim c --mm:orc --threads:off -d:release -r tests/wrk_jester.nim

Requests/sec: 9,692.81

Prologue

nim c --mm:orc --threads:off -d:release -r tests/wrk_prologue.nim

Requests/sec: 9,749.22

NodeJS

node tests/wrk_node.js

Requests/sec: 8,544.60

Go

go run tests/wrk_go.go

Requests/sec: 9,171.55

Testing

A fuzzer has been run against Mummy's socket reading and parsing code to ensure Mummy does not crash or otherwise misbehave on bad data from sockets. You can run the fuzzer any time by running nim c -r tests/fuzz_recv.nim.

mummy (guzba/mummy)

Ищем в базе…

342 ·GitHub
Что это такое?Mummy — это библиотека Nim для многопоточных HTTP 1.1 и WebSocket серверов. Она обрабатывает ввод-вывод сокетов в одном потоке и передаёт запросы пулу рабочих потоков, а не использует async/await.
Нужно ли писать свою программу вокруг неё или Mummy работает как отдельный сервер?Это библиотека Nim, которую вы подключаете к своей программе командой nimble install mummy. Вы пишете обычный блокирующий код обработчиков, а Mummy берёт на себя многопоточность и распределение задач.
Как добавить её в проект и запустить сервер?Установите её командой nimble install mummy, затем скомпилируйте программу с флагами --threads:on и --mm:orc (или --mm:arc), например: nim c --threads:on --mm:orc -r examples/basicrouter.nim.
Для чего Mummy не очень подходит?Mummy буферизует полные запросы и ответы в памяти перед передачей рабочим потокам, поэтому она плохо подходит для отдачи очень больших файлов или приёма больших загрузок, если на сервере недостаточно оперативной памяти.
Почему Mummy использует потоки, а не async?Блокирующий код обработчиков позволяет избежать «цвета функций» и рисков зависаний async-кода, например когда DNS-запросы или чтение файлов блокируют всё остальное. При этом сохраняется та же пропускная способность за счёт мультиплексированного неблокирующего ввода-вывода сокетов.
Насколько хороша поддержка WebSocket?WebSocket встроен в библиотеку с самого начала, поэтому вы получаете полную поддержку сразу, а не подключаете её через дополнительные зависимости или перехват соединений, как во многих других HTTP-серверах.
Насколько Mummy быстрее других серверов?В тестах на сервере Ubuntu с 4 ядрами и 8 потоками Mummy обработала около 9 547 запросов в секунду. Это близко к результату HttpBeast (9 862) и выше, чем у AsyncHttpServer (7 979), Node.js (8 544) и Go (9 171).
Использовали ли Mummy в реальной эксплуатации?Да. Один продакшен-сайт обрабатывает более 500 HTTP-запросов в секунду на небольшой виртуальной машине с очень низкой нагрузкой на процессор и память. Другой поддерживает 100 тысяч одновременных WebSocket-соединений с запасом для роста больше 1 миллиона.
Какие другие библиотеки хорошо сочетаются с Mummy?Curly упрощает работу с libcurl для HTTP RPC-вызовов, а Ready — это клиент Redis, созданный для многопоточных серверов вроде Mummy.
Какие есть похожие проекты?aiohttp, uvicorn, h2o, chi, tokio, fasthttp, echo, fiber
Автор
guzba
Размер
0.6 MB
Звёзды
342
Возраст
4 г
Коммиты · 90 дней