Skip to content

Instantly share code, notes, and snippets.

View WJWH's full-sized avatar

Wander Hillen WJWH

View GitHub Profile
@WJWH
WJWH / gist:f3a196e65fdabd6eace5f89da430600e
Created October 1, 2021 14:16
Server that does no syscalls for handling connections
// Extremely hacky server program that will send a standard response
// to every client that connects, then closes the connection. Will
// issue no system calls (as measured by `strace`) after initial setup
// no matter how many requests are served.
// Yes, this program is sorely lacking in error checking. It's a toy
// and not meant to be taken seriously.
// compile with gcc no_syscall_server.c -luring
@WJWH
WJWH / c10k.ru
Created October 15, 2018 09:12
The double hijack trick, serving 65k connections from a single ruby process
# This server demo does a socket hijack in Rack and then saves the socket to a global variable
# to prevent it from being GCed when the Puma thread ends. It will then write "BEEP" to each
# socket every ten seconds to prevent the connection timing out. During testing, it easily
# handled up to 65523 connections, after which it ran into the `ulimit` for open file descriptors.
# The bit with the waiting area is there because a normal `Set` is not thread safe and it would
# drop socket due to race conditions. The `Queue` is thread safe and will make sure all sockets
# are preserved.
# run with `rackup -q -p 8000 -o 0.0.0.0 c10k.ru`
# testing: install `ab` and then run `ab -c 20000 -n 20000 <ip adress of server>:8000/
@WJWH
WJWH / day15.hs
Created December 15, 2022 17:11
Haskell solution progression for AOC 2022 day 15 part 2
module Main where
import Utils
import Control.Concurrent
import Control.Concurrent.Chan
import Data.List
import qualified Data.Map.Strict as M
import qualified Data.Set as S
import Data.Range as Range
@WJWH
WJWH / Main.hs
Last active April 18, 2022 19:30
Files accompanying blog post on wjwh.eu
{-# LANGUAGE OverloadedStrings, DeriveGeneric, DerivingStrategies, DeriveAnyClass #-}
module Main where
import Prelude hiding (foldl)
import Conduit hiding (connect)
import Data.Attoparsec.ByteString as Attoparsec
import Data.Attoparsec.ByteString.Char8
import qualified Data.ByteString as B
import Data.Aeson
import Data.Conduit.Attoparsec
-- To benchmark, make sure to expose the GHC.Event.* in /libraries/base/base.cabal,
-- otherwise GHC will complain that they are hidden modules.
import GHC.Event.Manager hiding (closeFd)
import GHC.Event.Internal
import GHC.Event.IoUring as IoUring
import GHC.Event.EPoll as Epoll
import Control.Monad
import Data.Time
import System.Posix.IO
@WJWH
WJWH / gist:7867b0726fa6d667bb9c456c52bc303c
Created July 20, 2020 18:56
Not segfaulting program
{-# LANGUAGE OverloadedStrings, BangPatterns, GeneralizedNewtypeDeriving, CPP #-}
module Main where
#include <sys/epoll.h>
import Foreign.C.Error (eNOENT, getErrno, throwErrno,
throwErrnoIfMinus1, throwErrnoIfMinus1_)
import Foreign.C.Types
import Foreign.Marshal hiding (void, newArray)
{-# LANGUAGE OverloadedStrings #-}
module Main where
import Foreign.C.Types
import System.Posix.IO
import System.Posix.Types
import System.Linux.IO.URing
import System.Linux.IO.URing.PollEvent
(gdb) x/80a 0x42009ffe28-80
0x42009ffdd8: 0x2d68 0x718018 <base_GHCziEventziPSQ_zdwunsafeInsertNew_info+2408>
0x42009ffde8: 0x42003216b1 0x8
0x42009ffdf8: 0x4200613ed9 0x7899c0 <ghczmbignum_GHCziNumziInteger_integerToWordzh_info+48>
0x42009ffe08: 0x769528 <base_GHCziEventziIoUring_zdwconstructUserdata_info+816> 0x0
0x42009ffe18: 0x1 0x825fd0 <stg_ret_p_info>
0x42009ffe28: 0x7ffff45e0940 0x75e7c8 <base_SystemziLinuxziIOziURingziRing_zdwpushSqe_info+264>
0x42009ffe38: 0x7ffff45dc040 0x7ffff45dc100
0x42009ffe48: 0x7ffff45e0940 0xa19
0x42009ffe58: 0x1ff 0xa19
(gdb) bt
#0 LOOKS_LIKE_CLOSURE_PTR (p=0x318)
at includes/rts/storage/ClosureMacros.h:255
#1 0x0000000000a044e6 in evacuate (p=0x420193ce48) at rts/sm/Evac.c:591
#2 0x0000000000a14ddb in scavenge_small_bitmap (p=0x420193ce48, size=1,
bitmap=0) at rts/sm/Scav.c:289
#3 0x0000000000a17692 in scavenge_stack (p=0x420193ce48,
stack_end=0x420193cf80) at rts/sm/Scav.c:1923
#4 0x0000000000a16e15 in scavenge_one (p=0x420193cbf8) at rts/sm/Scav.c:1484
#5 0x0000000000a17218 in scavenge_mutable_list (bd=0x4201500880, gen=0xcb88e0)
{-# LANGUAGE OverloadedStrings #-}
-- Echo server program
module Main (main) where
import Control.Concurrent
import Control.Concurrent.MVar
import qualified Control.Exception as E
import Control.Monad (unless, forever, void)
import qualified Data.ByteString as S
import qualified Data.ByteString.Char8 as S8