-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathmultiple_queue.go
More file actions
124 lines (102 loc) · 2.37 KB
/
multiple_queue.go
File metadata and controls
124 lines (102 loc) · 2.37 KB
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
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"`
}
// 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"}
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:
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").UpdateId(entry.ID, &entry)
if err != nil {
panic("update error")
}
*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)
}
}