-
-
Save jan-g/b0359bc5a739a1a7d2bce16ce72a3fbb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/zsh | |
zmodload zsh/net/tcp | |
debug() { | |
echo "$@" >&2 | |
} | |
debug "recv" | |
input="$(cat; echo -n '$')" | |
debug "DB" | |
ztcp -d 4 localhost 12346 | |
# Send it all to the DB | |
printf "%q\n" "$input" >&4 | |
cat <&4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
./zdb & | |
trap "kill %1" INT | |
socat UDP-RECVFROM:12345,fork,reuseaddr,null-eof EXEC:./downstream |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This is a bit ugly. | |
We need to ensure that socat doesn't discard zero-size payloads (because they're a request for the value assigned to the | |
null identifier.) | |
We use a single standalone single-threaded process to serve up the persistent DB. | |
We can't shutdown sockets, which means that we can't just `cat` content from the `downstream` packet handler into a receiving | |
`cat` in the zdb; this'll hang because nothing is closing the TCP connection to it. If we close the outgoing connection, the | |
read connection (that associates a request to a response) gets closed too. | |
So instead we use `read -r` and ensure that the input is quoted, so it all appears on one line. | |
We also need to dodge the way a shell will strip trailing whitespace from a captured command. We do this by appending a `$` | |
to the end of the payload, and stripping it off at the point we're going to be dealing with shell variables and nothing else. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/zsh | |
zmodload zsh/net/tcp | |
ztcp -ld3 12346 | |
debug() { | |
echo "$@" >&2 | |
} | |
declare -A db | |
while : | |
do | |
ztcp -ad4 3 | |
printf "received connection on fd4\n" | |
read -r req <&4 | |
req="$(eval echo "$req")" | |
req="${req%$}" | |
case "$req" in | |
version) | |
echo "version=jan's db" >&4 | |
;; | |
*=*) | |
id="${req%%=*}" | |
val="${req#*=}" | |
debug "Assignment: id='$id' val='$val'" | |
db[$id]="$val" | |
;; | |
*) | |
debug "Read: '$req' -> '$db[$req]'" | |
printf "%s=%s" "$req" "$db[$req]" >&4 | |
;; | |
esac | |
ztcp -c 4 | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment