-
Notifications
You must be signed in to change notification settings - Fork 17
/
multiple_queue.go
133 lines (111 loc) · 2.62 KB
/
multiple_queue.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package main
import (
"io"
"log"
"math/rand"
"net/http"
"os"
"strconv"
"sync"
"time"
"github.com/globalsign/mgo"
"github.com/globalsign/mgo/bson"
)
var globalDB *mgo.Database
var in []chan Data
var maxUser = 100
var maxThread = 10
// Data struct
type Data struct {
Account string
Result *chan float64
}
type currency struct {
ID bson.ObjectId `json:"id" bson:"_id,omitempty"`
Amount float64 `bson:"amount"`
Account string `bson:"account"`
Code string `bson:"code"`
Version int `bson:"version"`
}
// Random get random value
func Random(min, max int) int {
rand.Seed(time.Now().UTC().UnixNano())
return rand.Intn(max-min+1) + min
}
func pay(w http.ResponseWriter, r *http.Request) {
wg := sync.WaitGroup{}
wg.Add(1)
go func(wg *sync.WaitGroup) {
number := Random(1, maxUser)
channelNumber := number % maxThread
account := "user" + strconv.Itoa(number)
result := make(chan float64)
in[channelNumber] <- Data{
Account: account,
Result: &result,
}
select {
case result := <-result:
log.Printf("account: %v, result: %+v\n", account, result)
wg.Done()
}
}(&wg)
wg.Wait()
io.WriteString(w, "ok")
}
func main() {
port := os.Getenv("PORT")
if port == "" {
port = "8000"
}
in = make([]chan Data, maxThread)
session, _ := mgo.Dial("localhost:27017")
globalDB = session.DB("logs")
globalDB.C("bank").DropCollection()
for i := range in {
in[i] = make(chan Data)
}
// create 100 user
for i := 0; i < maxUser; i++ {
account := "user" + strconv.Itoa(i+1)
user := currency{Account: account, Amount: 1000.00, Code: "USD", Version: 1}
if err := globalDB.C("bank").Insert(&user); err != nil {
panic("insert error")
}
}
for i := range in {
go func(in *chan Data, i int) {
for {
select {
case data := <-*in:
LOOP:
entry := currency{}
// step 1: get current amount
err := globalDB.C("bank").Find(bson.M{"account": data.Account}).One(&entry)
if err != nil {
panic(err)
}
//step 3: subtract current balance and update back to database
entry.Amount = entry.Amount + 50.000
err = globalDB.C("bank").Update(bson.M{
"version": entry.Version,
"_id": entry.ID,
}, bson.M{"$set": map[string]interface{}{
"amount": entry.Amount,
"version": (entry.Version + 1),
}})
if err != nil {
log.Println("got errors: ", err)
goto LOOP
}
*data.Result <- entry.Amount
}
}
}(&in[i], i)
}
log.Println("Listen server on " + port + " port")
http.HandleFunc("/", pay)
if err := http.ListenAndServe(":"+port, nil); err != nil {
log.Fatal(err)
}
}