-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfrontend.go
174 lines (142 loc) · 4.11 KB
/
frontend.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package main
import (
"errors"
"fmt"
"github.com/garyburd/redigo/redis"
"log"
"net/http"
"runtime"
"strconv"
"time"
)
const redisInstancesCount = 2
var redisConnectionShards [redisInstancesCount]redis.Conn
var redisErr error
// Return a connection to the Redis instance that is responsible for this account.
func redisConnection(account int) redis.Conn {
return redisConnectionShards[account%redisInstancesCount]
}
// Return the key that will store the account balance.
func accountKey(account int) string {
return fmt.Sprintf("account:%d", account)
}
// Return the lock key for the given account.
func accountLockKey(account int) string {
return fmt.Sprintf("accountlock:%d", account)
}
// Return the balance of an account.
func getBalance(account int) int {
redisConnection(account).Do("HSETNX", "accounts", account, 0)
balance, err := redis.Int(redisConnection(account).Do("HGET", "accounts", account))
if err != nil {
log.Print(err)
}
return balance
}
// Acquire the lock on the given account.
func acquireLock(account int) error {
retries := 0
for retries <= 100 {
reply, _ := redis.Int(redisConnection(account).Do("SETNX", accountLockKey(account), "LOCKED"))
if reply == 1 {
redisConnection(account).Do("EXPIRE", accountLockKey(account), 5)
return nil
} else {
time.Sleep(100 * time.Millisecond)
retries += 1
if retries == 10 {
return errors.New("could not acquire lock")
}
}
}
return nil
}
// Release the lock on the given account.
func releaseLock(account int) {
redisConnection(account).Do("DEL", accountLockKey(account))
}
func depositHandler(w http.ResponseWriter, r *http.Request) {
account, err := strconv.Atoi(r.FormValue("account"))
if err != nil {
log.Print(err)
}
if err := acquireLock(account); err != nil {
fmt.Fprintf(w, "{error: \"could not acquire lock\"}")
return
}
defer releaseLock(account)
amount, err := strconv.Atoi(r.FormValue("amount"))
if err != nil {
log.Print(err)
}
getBalance(account)
redisConnection(account).Do("HINCRBY", "accounts", account, amount)
fmt.Fprintf(w, "{account: %d, balance: %d}", account, getBalance(account))
}
func withdrawHandler(w http.ResponseWriter, r *http.Request) {
account, err := strconv.Atoi(r.FormValue("account"))
if err != nil {
log.Print(err)
}
if err := acquireLock(account); err != nil {
fmt.Fprintf(w, "{error: \"could not acquire lock\"}")
return
}
defer releaseLock(account)
amount, err := strconv.Atoi(r.FormValue("amount"))
if err != nil {
log.Print(err)
}
if getBalance(account) >= amount {
redisConnection(account).Do("HINCRBY", "accounts", account, -amount)
fmt.Fprintf(w, "{account: %d, balance: %d}", account, getBalance(account))
} else {
fmt.Fprintf(w, "{error: \"insufficient funds\"}")
}
}
func transferHandler(w http.ResponseWriter, r *http.Request) {
source, err := strconv.Atoi(r.FormValue("source"))
if err != nil {
log.Print(err)
}
if err := acquireLock(source); err != nil {
fmt.Fprintf(w, "{error: \"could not acquire lock\"}")
return
}
defer releaseLock(source)
destination, err := strconv.Atoi(r.FormValue("destination"))
if err != nil {
log.Print(err)
}
if err := acquireLock(destination); err != nil {
fmt.Fprintf(w, "{error: \"could not acquire lock\"}")
return
}
defer releaseLock(destination)
amount, err := strconv.Atoi(r.FormValue("amount"))
if err != nil {
log.Print(err)
}
if getBalance(source) >= amount {
getBalance(destination)
redisConnection(source).Do("HINCRBY", "accounts", source, -amount)
redisConnection(destination).Do("HINCRBY", "accounts", destination, amount)
fmt.Fprintf(w, "{success: \"ok\"}")
} else {
fmt.Fprintf(w, "{error: \"insufficient funds\"}")
}
}
func main() {
runtime.GOMAXPROCS(128)
redisInstances := [redisInstancesCount]string{"10.6.1.160:6379", "169.254.9.181:6379"}
for i, s := range redisInstances {
redisConnectionShards[i], redisErr = redis.Dial("tcp", s)
if redisErr != nil {
log.Print(redisErr)
}
}
http.HandleFunc("/deposit", depositHandler)
http.HandleFunc("/withdraw", withdrawHandler)
http.HandleFunc("/transfer", transferHandler)
log.Print(http.ListenAndServe(":8341", nil))
}