-
Notifications
You must be signed in to change notification settings - Fork 0
/
tx.go
81 lines (71 loc) · 1.77 KB
/
tx.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
package stomp
import (
"errors"
"io"
)
// ErrTxDone is returned when a complete transaction is used after
// a commit or abort.
var ErrTxDone = errors.New("stomp: transaction has already been committed or aborted")
// Tx represents an ongoing STOMP transaction.
type Tx struct {
tid string
done bool
receipts *receipts
transport *Transport
}
// Commit commits the transaction.
func (t *Tx) Commit(receipt bool) error {
if t.done {
return ErrTxDone
}
defer func() {
t.done = true
}()
if receipt {
return doWithReceipt(t.receipts, func(rid string) error {
return t.transport.TxCommit(t.tid, &rid)
})
}
return t.transport.TxCommit(t.tid, nil)
}
// Abort aborts the transaction.
// Abort will not return ErrTxDone so it is safe to call
// after commiting, for instance, when defered.
func (t *Tx) Abort(receipt bool) error {
if t.done {
return nil
}
defer func() {
t.done = true
}()
if receipt {
return doWithReceipt(t.receipts, func(rid string) error {
return t.transport.TxAbort(t.tid, &rid)
})
}
return t.transport.TxAbort(t.tid, nil)
}
// Send sends a message to the requested destination dest.
// The parameters hdrs and body may be nil, indicating that they
// will not be used for the sent message.
// Send automatically generates a content-length for the provided body.
func (t *Tx) Send(dest string, hdrs *map[string]string, bodyType string, body io.Reader) error {
if t.done {
return ErrTxDone
}
return t.transport.TxSend(t.tid, dest, hdrs, bodyType, body)
}
// Ack sends an ACK frame.
func (t *Tx) Ack(id string) error {
if t.done {
return ErrTxDone
}
return t.transport.TxAck(t.tid, id)
}
// Nack sends a NACK frame.
func (t *Tx) Nack(id string) error {
if t.done {
return ErrTxDone
}
return t.transport.TxNack(t.tid, id)
}