Created
February 9, 2016 14:39
-
-
Save vmihailenco/78768e6eaa93aabd5888 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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