Skip to content

Instantly share code, notes, and snippets.

View bkcsoft's full-sized avatar

Kim "BKC" Carlbäcker bkcsoft

  • Sweden
  • 08:06 (UTC +02:00)
View GitHub Profile
#!/usr/bin/env bash
PATHS=( `find $GOPATH/src -type d -name ${1} | sed "s;$GOPATH/src/;;"` )
for path in "${PATHS[@]}"; do
[[ ${path} =~ /vendor/ ]] && continue
test="${GOPATH}/src/${path}/.git"
if [[ -d "$GOPATH/src/${path}/.git" ]]; then
echo $path
fi
@bkcsoft
bkcsoft / gitea-http.log
Created March 21, 2017 02:27
500 Error in /api/v1/user/repos
Mar 21 03:17:37 hax0r gitea[4654]: [Macaron] 2017-03-21 03:17:37: Started GET /api/v1/user/repos for 51.15.43.181
Mar 21 03:17:37 hax0r gitea[4654]: [Macaron] PANIC: runtime error: index out of range
Mar 21 03:17:37 hax0r gitea[4654]: /usr/local/go/src/runtime/panic.go:489 (0x433abf)
Mar 21 03:17:37 hax0r gitea[4654]: /usr/local/go/src/runtime/panic.go:28 (0x43273e)
Mar 21 03:17:37 hax0r gitea[4654]: /ext-go/1/src/code.gitea.io/gitea/routers/api/v1/user/repo.go:29 (0xf6a6d2)
Mar 21 03:17:37 hax0r gitea[4654]: /ext-go/1/src/code.gitea.io/gitea/routers/api/v1/user/repo.go:46 (0xf6a8a7)
Mar 21 03:17:37 hax0r gitea[4654]: /usr/local/go/src/runtime/asm_amd64.s:514 (0x460ae8)
Mar 21 03:17:37 hax0r gitea[4654]: /usr/local/go/src/reflect/value.go:434 (0x4c328f)
Mar 21 03:17:37 hax0r gitea[4654]: /usr/local/go/src/reflect/value.go:302 (0x4c2854)
Mar 21 03:17:37 hax0r gitea[4654]: /ext-go/1/src/code.gitea.io/gitea/vendor/github.com/go-macaron/inject/inject.go:177 (0x86aa4f)

Scaling your API with rate limiters

The following are examples of the four types rate limiters discussed in the accompanying blog post. In the examples below I've used pseudocode-like Ruby, so if you're unfamiliar with Ruby you should be able to easily translate this approach to other languages. Complete examples in Ruby are also provided later in this gist.

In most cases you'll want all these examples to be classes, but I've used simple functions here to keep the code samples brief.

Request rate limiter

This uses a basic token bucket algorithm and relies on the fact that Redis scripts execute atomically. No other operations can run between fetching the count and writing the new count.

@bkcsoft
bkcsoft / template-patcher.sh
Created January 2, 2018 00:35
Template Patcher for try.gitea.io
#!/usr/bin/env bash
function die() {
echo -n $@ 1>&2
exit 1
}
cd $(docker volume inspect -f '{{ .Mountpoint }}' demo_gitea)/templates/base/
wget -o head.tmpl.master https://raw.githubusercontent.com/go-gitea/gitea/master/templates/base/head.tmpl || die "fetch failed"