Pigeon Facts as a Service

Friend is into pigeons, so the only thing that makes sense is to buy a domain with his name and create a PFaaS (Pigeon-Facts-as-a-Service), so that he can flaunt in front of his other, pigeon loving friends

Means of production

Although there are plenty of frameworks that could be used to bootstrap REST API, Bash shell with nc was chosen, based on it’s supreme benchmarks

Netcat benchmark

Connections during time – constant output (1) from nc, no fluctuations

Netcat is handling the routing, with responses passing through a FIFO pipe

cat "${PFAAS_PIPE}" | nc -n -l -p ${PFAAS_PORT} > >(
  while read l; do
    # [ ... ]
  done
)

Facts are read from a separate file and fed into an array that is used to show either a random or a specific fact (/api/fact/{id})

IFS=,$'\n' read -d '' -a FACTS < ${PFAAS_FACTS}
# [ ... ]
id=$RANDOM
let "id %= ${#FACTS[@]}"
fact="${FACTS[${id}]}"

After a GET request is parsed (and optional fact ID retrieved), either the target fact or a random one will be JSON marshaled and returned to the client

Obligatory benchmark

Apache Bench shows neat results for one user / one request at a time, expectedly breaking when either consecutive requests (nc is not fond of RFC 2616’s “Connection: close”) or concurrent ones appear

$ ab -n 1 -c 1 -k -H "Content-Type: application/json" \
                   http://localhost:1805/api/fact/ | tail -n+15 | head -4
Concurrency Level:      1
Time taken for tests:   0.015 seconds
Complete requests:      1
Failed requests:        0

Apache Bench – 1 req

$ ab -n 2 -c 1 http://localhost:1805/api/fact/ > /dev/null
apr_socket_recv: Connection refused (111)

Anything above one (w/o increasing concurrency level)

Pigeon facts to go

Code and a complementary Docker image are available, if you prefer to have it running on-prem

For those that simply want to expand their vast pigeon knowledge, feel free to checkout a random fact on friend’s PFaaS

If you get an error, another user is accessing it, try in a sec

PFaaS in action

2019-03-11T10:20:02+00:00