Skip to content

Instantly share code, notes, and snippets.

@vmihailenco
Created February 9, 2016 14:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vmihailenco/78768e6eaa93aabd5888 to your computer and use it in GitHub Desktop.
Save vmihailenco/78768e6eaa93aabd5888 to your computer and use it in GitHub Desktop.
package main
import (
"log"
"math/rand"
"strings"
"gopkg.in/pg.v4"
)
func main() {
db := pg.Connect(&pg.Options{
User: "postgres",
PoolSize: 50,
})
qs := []string{
`DROP TABLE IF EXISTS repack_issue_70`,
`CREATE TABLE repack_issue_70 (id serial PRIMARY KEY, hash int)`,
`CREATE UNIQUE INDEX on repack_issue_70 (hash) WHERE hash IS NOT NULL`,
}
for _, q := range qs {
_, err := db.Exec(q)
if err != nil {
log.Fatal(err)
}
}
for i := 0; i < 100; i++ {
go thread(db)
}
log.Print("ready...")
select {}
}
func thread(db *pg.DB) {
for i := uint(0); ; i++ {
hash := rand.Intn(100)
_, err := db.Exec(`
INSERT INTO repack_issue_70 (hash) VALUES (?)
`, hash)
if err != nil {
if !strings.Contains(err.Error(), "duplicate key") {
log.Fatal(err)
}
}
if i%2 == 0 {
_, err := db.Exec(`
UPDATE repack_issue_70
SET hash = NULL
WHERE hash = ?
`, hash)
if err != nil {
log.Fatal(err)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment