-
Notifications
You must be signed in to change notification settings - Fork 17
/
single_queue.go
110 lines (87 loc) · 1.93 KB
/
single_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
package main
import (
"io"
"log"
"net/http"
"os"
"sync"
"github.com/globalsign/mgo"
"github.com/globalsign/mgo/bson"
)
var globalDB *mgo.Database
var account = "appleboy"
var in chan Data
// 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"`
}
func pay(w http.ResponseWriter, r *http.Request) {
wg := sync.WaitGroup{}
wg.Add(1)
go func(wg *sync.WaitGroup) {
result := make(chan float64)
in <- Data{
Account: account,
Result: &result,
}
for {
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)
session, err := mgo.Dial("localhost:27017")
if err != nil {
panic("can't connect mongodb server")
}
globalDB = session.DB("logs")
globalDB.C("bank").DropCollection()
user := currency{Account: account, Amount: 1000.00, Code: "USD"}
err = globalDB.C("bank").Insert(&user)
if err != nil {
panic("insert error")
}
go func(in *chan Data) {
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)
log.Println("Listen server on " + port + " port")
http.HandleFunc("/", pay)
if err := http.ListenAndServe(":"+port, nil); err != nil {
log.Fatal(err)
}
}