Skip to content

Instantly share code, notes, and snippets.

package main
import (
"log"
"net/http"
"time"
"github.com/alexellis/faas/gateway/metrics"
"github.com/gorilla/mux"
"github.com/prometheus/client_golang/prometheus"
@farhany
farhany / rate-limiting.go
Last active April 26, 2019 20:28
Rate Limiting for HTTP
// Source: https://stackoverflow.com/questions/17959732/why-is-go-https-client-not-reusing-connections
import "time"
requests_per_second := 5
throttle := time.Tick(1000000000 / requests_per_second)
for i := 0; i < 16; i += 1 {
<-throttle
go serveQueue()
@farhany
farhany / readBodyCloser.go
Last active April 26, 2019 20:30
Close http client connections properly
// Source: https://stackoverflow.com/questions/17948827/reusing-http-connections-in-golang
// It is the caller's responsibility to
// close Body. The default HTTP client's Transport may not
// reuse HTTP/1.x "keep-alive" TCP connections if the Body is
// not read to completion and closed.
package main
import (
@farhany
farhany / jetsonNanoHeadless.sh
Created April 28, 2019 00:32
Jetson Nano - Headless
# Source: https://devtalk.nvidia.com/default/topic/1049266/headless-os/
# WARNING: This could render your system unbootable. Use at your own risk, and backup first.
sudo systemctl enable multi-user.target
sudo systemctl set-default multi-user.target
sudo apt remove whoopsie
sudo apt remove unattended-upgrades
@farhany
farhany / docker-cmdline
Last active April 28, 2019 01:33
docker cmdline - useful commands
# Get IP
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' container_name_or_id
# Remove stopped images/etc
docker system prune
docker image prune
docker volume prune
@farhany
farhany / multipart_upload.go
Created April 28, 2019 08:54 — forked from mattetti/multipart_upload.go
Example of doing a multipart upload in Go (golang)
package main
import (
"bytes"
"fmt"
"io"
"log"
"mime/multipart"
"net/http"
"os"
Great article on subject:
https://medium.freecodecamp.org/useful-tricks-you-might-not-know-about-git-stash-e8a9490f0a1a
@farhany
farhany / go-bench
Created May 14, 2019 20:02
Go benchmark memory
Source: https://github.com/kkdai/pubsub/blob/master/.travis.yml
- $HOME/gopath/bin/goveralls -coverprofile=coverage.cov -service=travis-ci
- go test -bench=. -benchmem .
- sh ./install_all_cmd.sh
@farhany
farhany / check.go
Created May 22, 2019 18:26 — forked from mattes/check.go
Check if file or directory exists in Golang
if _, err := os.Stat("/path/to/whatever"); os.IsNotExist(err) {
// path/to/whatever does not exist
}
if _, err := os.Stat("/path/to/whatever"); !os.IsNotExist(err) {
// path/to/whatever exists
}
@farhany
farhany / exec.go
Created May 30, 2019 04:31 — forked from danesparza/exec.go
Go exec.Command example
package main
import (
"bytes"
"fmt"
"os/exec"
"strings"
)
func main() {