Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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()
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 / Setup macOS
Last active April 28, 2019 04:47
Setup macOS
# https://github.com/tonsky/FiraCode/wiki
# Fonts
brew tap caskroom/fonts
brew cask install font-fira-code
# ZSH
brew install zsh-syntax-highlighting zsh-autosuggestions
brew install fasd
brew install ripgrep
@farhany
farhany / APICodes.txt
Created April 24, 2019 06:53
API Codes
HTTP Status Codes (General)
2xx = Success
3xx = Redirect
4xx = User error
5xx = Server error
Success codes:
200 - OK
201 - Created
202 - Accepted (Mostly used for DELETE)
@farhany
farhany / TeeReader.go
Created April 24, 2019 03:58
Basic use of TeeReader to reduce memory consumption
// Source: https://stackoverflow.com/questions/41947307/how-to-return-hash-and-bytes-in-one-step-in-go
hasher := sha256.New()
f, err := os.Open(fname)
data := io.TeeReader(f, hasher)
// Now read from data as usual, which is still a stream.
@farhany
farhany / custom404.go
Created April 24, 2019 03:55
Custom Error Pages in Go
// Source: https://stackoverflow.com/questions/9996767/showing-custom-404-error-page-with-standard-http-package
package main
import (
"fmt"
"net/http"
)
func main() {
@farhany
farhany / dumpresponse.go
Last active April 24, 2019 03:51
httputil.DumpResponse use in Go
res, err := c.httpClient.Do(req)
dump, _ := httputil.DumpResponse(res, true)
fmt.Printf("DUMP: %q\n", dump)
@farhany
farhany / httpServer-timeout.go
Created April 21, 2019 20:31
httpServer-timeout
s := &http.Server{
Addr: ":8080",
Handler: myHandler,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
}
log.Fatal(s.ListenAndServe())