Skip to content

Instantly share code, notes, and snippets.

View reterVision's full-sized avatar
🍔

Chao Gao reterVision

🍔
View GitHub Profile
@reterVision
reterVision / test_rest.go
Last active August 29, 2015 14:01
Test Rest API + Redis usage in Go
package main
import (
"encoding/json"
"fmt"
"strings"
"github.com/garyburd/redigo/redis"
"github.com/ant0ine/go-json-rest/rest"
"log"
"net/http"
@reterVision
reterVision / postgres.go
Created May 22, 2014 11:00
Load Django User table information from PostgreSQL in Go
//
// go get github.com/lib/pq
//
package main
import (
_ "github.com/lib/pq"
"database/sql"
"fmt"
"log"
@reterVision
reterVision / lcs.go
Created June 20, 2014 03:25
Longest Common Strings in Go
package main
import (
"bufio"
"fmt"
"log"
"os"
"strings"
)
# Build dependencies for OpenResty.
sudo apt-get install build-essential libpcre3-dev libssl-dev libgeoip-dev
# Install standard Nginx first so that you get the relevant service scripts installed too
sudo apt-get install nginx
# If you want to access Postgres via Nginx
sudo apt-get install libpq-dev

This example is based on having a cascading setup, where you have a single master, a single "primary" slave, and cascading slaves which are being replicated from the primary slave. For an example of this setup, check out http://bartek.im/blog/2012/12/04/postgresql-92-streaming-primer.html

On the existing master, if accessible, stop postgres.

$ sudo service postgresql stop

And better to be safe than sorry. We can't have two masters running. (I only use this in an automated script. If you're doing this manually, you'd know if it was shutoff)

$ kill -9 `sudo cat /var/lib/postgresql/9.2/main/postmaster.pid | head -n 1` &> /dev/null
@reterVision
reterVision / atomic_test.go
Created May 16, 2015 08:38
Benchmark Testing: RWMutex vs sync/atomic
package main
import (
"sync"
"sync/atomic"
"testing"
)
func Benchmark_Lock(b *testing.B) {
var lock sync.RWMutex
@reterVision
reterVision / brew_install_postgres.sh
Last active December 10, 2015 02:28
Show you the steps of upgrading postgres to 9.2 on Mac OS X using homebrew
# Update brew & install the latest postgres first
brew update
brew install postgres
# Shut down the old running postgres
# Assume you previously installed postgres using homebrew
pg_ctl -D /usr/local/Cellar/posgres/9.1.1 stop
# If postgres won't stop, you have to do the following step to unload it from LaunchAgents
launchctl unload -w ~/Library/LaunchAgents/org.postgresql.postgres.plist
@reterVision
reterVision / create_and_grant.sql
Last active December 10, 2015 04:08
How to create a role and grant a view to him with the read only privilege in postgres.
CREATE ROLE <newuser> WITH LOGIN ENCRYPTED PASSWORD <password>;
CREATE VIEW <viewname> AS SELECT * FROM <yourtable> LIMIT 20;
GRANT CONNECT ON DATABASE "<databasename>" TO <newuser>;
GRANT SELECT ON schemaname.viewname TO <newuser>;
REVOKE ALL PRIVILEGES ON <schemaname> FROM <newuser>;
# How to merge other branch to master.
# First fetch the changes from your branch, could be made by others
git fetch
# Then commit your changes.
git commit -m "some"
git push origin my-branch
# Switch to master branch and pull the latest code to your local.
# Sometimes we could mistakenly commit something without pull first, so
# here is how we revert this process and keep our master branch clean.
# Reset back to the commit before you last commit.
# git reset --mixed will keep your changes and revert other files back
# to that commit.
git pull
git reset --mixed HEAD-before-your-last-commit
# Commit your changes again.