forked from freeeve/cq
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathtransaction.go
249 lines (228 loc) · 5.6 KB
/
transaction.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package cq
import (
"bytes"
"database/sql/driver"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/url"
"time"
)
type transactionResponse struct {
Commit string `json:"commit"`
Transaction struct {
Expires string
}
Errors []commitError `json:"errors"`
location string
}
type cypherTransactionStatement struct {
Statement *string `json:"statement"`
Parameters map[string]interface{} `json:"parameters"`
}
type cypherTransaction struct {
Statements []cypherTransactionStatement `json:"statements"`
commitURL string
transactionURL string
expiration time.Time
c *conn
rows []*rows
keepAlive *time.Timer
}
type commitError struct {
Code string `json:"code"`
Message string `json:"message"`
}
type commitResponse struct {
Errors []commitError `json:"errors"`
}
func (c *conn) Begin() (driver.Tx, error) {
if c.transactionURL == "" {
return nil, ErrTransactionsNotSupported
}
transResponse, err := getTransactionResponse(c.transactionURL,
cypherTransaction{Statements: make([]cypherTransactionStatement, 0)})
if err != nil {
return nil, err
}
exp, err := time.Parse(time.RFC1123Z, transResponse.Transaction.Expires)
if err != nil {
log.Println(err, c)
err = nil
}
commitURL, err := url.Parse(transResponse.Commit)
if err != nil {
return nil, err
}
commitURL.Scheme = c.scheme
commitURL.User = c.userInfo
transactionURL, err := url.Parse(transResponse.location)
if err != nil {
return nil, err
}
transactionURL.Scheme = c.scheme
transactionURL.User = c.userInfo
c.transaction = &cypherTransaction{
commitURL: commitURL.String(),
transactionURL: transactionURL.String(),
c: c,
expiration: exp,
}
c.transaction.updateKeepAlive()
return c.transaction, nil
}
func (tx *cypherTransaction) query(query *string, args []driver.Value) error {
stmt := cypherTransactionStatement{
Statement: query,
Parameters: makeArgsMap(args),
}
tx.Statements = append(tx.Statements, stmt)
if len(tx.Statements) >= 100 {
err := tx.exec()
if err != nil {
return err
}
}
return nil
}
func (tx *cypherTransaction) exec() error {
trans, err := getTransactionResponse(tx.transactionURL, *tx)
if err != nil {
return err
}
tx.expiration, err = time.Parse(time.RFC1123Z, trans.Transaction.Expires)
if err != nil {
log.Print(err, tx)
err = nil
}
tx.updateKeepAlive()
tx.Statements = tx.Statements[:0]
if len(trans.Errors) > 0 {
return errors.New("exec errors: " + fmt.Sprintf("%q", trans))
}
if err != nil {
return err
}
return nil
}
func (tx *cypherTransaction) Commit() error {
if tx.Statements == nil {
return nil
}
var buf bytes.Buffer
err := json.NewEncoder(&buf).Encode(tx)
if err != nil {
return err
}
req, err := http.NewRequest("POST", tx.commitURL, &buf)
if err != nil {
return err
}
setDefaultHeaders(req)
res, err := client.Do(req)
defer res.Body.Close()
if err != nil {
return err
}
commit := commitResponse{}
json.NewDecoder(res.Body).Decode(&commit)
io.Copy(ioutil.Discard, res.Body)
res.Body.Close()
if err != nil {
return err
}
if len(commit.Errors) > 0 {
return errors.New("commit errors: " + fmt.Sprintf("%q", commit))
}
tx.c.transaction = nil
if tx.keepAlive != nil {
tx.keepAlive.Stop()
}
return nil
}
func (tx *cypherTransaction) Rollback() error {
req, err := http.NewRequest("DELETE", tx.transactionURL, nil)
if err != nil {
return err
}
setDefaultHeaders(req)
res, err := client.Do(req)
if err != nil {
return err
}
commit := commitResponse{}
json.NewDecoder(res.Body).Decode(&commit)
io.Copy(ioutil.Discard, res.Body)
res.Body.Close()
if err != nil {
return err
}
if len(commit.Errors) > 0 {
return errors.New("rollback errors: " + fmt.Sprintf("%q", commit))
}
tx.c.transaction = nil
if tx.keepAlive != nil {
tx.keepAlive.Stop()
}
return nil
}
func getTransactionResponse(url string, cypherTransReq cypherTransaction) (*transactionResponse, error) {
var buf bytes.Buffer
json.NewEncoder(&buf).Encode(cypherTransReq)
req, err := http.NewRequest("POST", url, &buf)
if err != nil {
return nil, err
}
setDefaultHeaders(req)
res, err := client.Do(req)
if err != nil {
return nil, err
}
transResponse := transactionResponse{}
json.NewDecoder(res.Body).Decode(&transResponse)
io.Copy(ioutil.Discard, res.Body)
res.Body.Close()
transResponse.location = res.Header.Get("Location")
return &transResponse, nil
}
// updateKeepAlive cancels the current timer, if it is set, and
// schedules a timer to send a keepalive message before the transaction expires.
func (tx *cypherTransaction) updateKeepAlive() {
if tx.keepAlive != nil {
tx.keepAlive.Stop()
}
dur := getDurToKeepAlive(tx.expiration)
tx.keepAlive = time.AfterFunc(dur, func() { sendKeepAlive(tx.transactionURL) })
}
// sendKeepAlive sends an empty Cypher transactional statement
// to the URL given. It parses the response and schedules a new
// call to itself halfway to the next timeout
func sendKeepAlive(txURL string) {
trans, err := getTransactionResponse(txURL, cypherTransaction{Statements: []cypherTransactionStatement{}})
if err != nil {
return
}
if len(trans.Errors) > 0 {
return
}
exp, err := time.Parse(time.RFC1123Z, trans.Transaction.Expires)
if err != nil {
return
}
dur := getDurToKeepAlive(exp)
time.AfterFunc(dur, func() { sendKeepAlive(txURL) })
}
// getDurToKeepAlive gets the difference between now and the expiration
// and returns the duration until the halfway point
func getDurToKeepAlive(t time.Time) time.Duration {
dur := -1 * time.Since(t)
if dur < time.Second*1 {
dur = time.Second * 1
}
dur /= 2
return dur
}