@@ -49,18 +49,36 @@ const (
49
49
Implicit
50
50
)
51
51
52
- // State indicates the state of the FSM.
53
- type state uint8
52
+ // TransactionState indicates the state of the transactions FSM.
53
+ type TransactionState uint8
54
54
55
55
// Client Session states
56
56
const (
57
- None state = iota
57
+ None TransactionState = iota
58
58
Starting
59
59
InProgress
60
60
Committed
61
61
Aborted
62
62
)
63
63
64
+ // String implements the fmt.Stringer interface.
65
+ func (s TransactionState ) String () string {
66
+ switch s {
67
+ case None :
68
+ return "none"
69
+ case Starting :
70
+ return "starting"
71
+ case InProgress :
72
+ return "in progress"
73
+ case Committed :
74
+ return "committed"
75
+ case Aborted :
76
+ return "aborted"
77
+ default :
78
+ return "unknown"
79
+ }
80
+ }
81
+
64
82
// Client is a session for clients to run commands.
65
83
type Client struct {
66
84
* Server
@@ -89,10 +107,10 @@ type Client struct {
89
107
transactionWc * writeconcern.WriteConcern
90
108
transactionMaxCommitTime * time.Duration
91
109
92
- pool * Pool
93
- state state
94
- PinnedServer * description.Server
95
- RecoveryToken bson.Raw
110
+ pool * Pool
111
+ TransactionState TransactionState
112
+ PinnedServer * description.Server
113
+ RecoveryToken bson.Raw
96
114
}
97
115
98
116
func getClusterTime (clusterTime bson.Raw ) (uint32 , uint32 ) {
@@ -242,29 +260,29 @@ func (c *Client) EndSession() {
242
260
243
261
// TransactionInProgress returns true if the client session is in an active transaction.
244
262
func (c * Client ) TransactionInProgress () bool {
245
- return c .state == InProgress
263
+ return c .TransactionState == InProgress
246
264
}
247
265
248
266
// TransactionStarting returns true if the client session is starting a transaction.
249
267
func (c * Client ) TransactionStarting () bool {
250
- return c .state == Starting
268
+ return c .TransactionState == Starting
251
269
}
252
270
253
271
// TransactionRunning returns true if the client session has started the transaction
254
272
// and it hasn't been committed or aborted
255
273
func (c * Client ) TransactionRunning () bool {
256
- return c != nil && (c .state == Starting || c .state == InProgress )
274
+ return c != nil && (c .TransactionState == Starting || c .TransactionState == InProgress )
257
275
}
258
276
259
277
// TransactionCommitted returns true of the client session just committed a transaciton.
260
278
func (c * Client ) TransactionCommitted () bool {
261
- return c .state == Committed
279
+ return c .TransactionState == Committed
262
280
}
263
281
264
282
// CheckStartTransaction checks to see if allowed to start transaction and returns
265
283
// an error if not allowed
266
284
func (c * Client ) CheckStartTransaction () error {
267
- if c .state == InProgress || c .state == Starting {
285
+ if c .TransactionState == InProgress || c .TransactionState == Starting {
268
286
return ErrTransactInProgress
269
287
}
270
288
return nil
@@ -309,17 +327,17 @@ func (c *Client) StartTransaction(opts *TransactionOptions) error {
309
327
return ErrUnackWCUnsupported
310
328
}
311
329
312
- c .state = Starting
330
+ c .TransactionState = Starting
313
331
c .PinnedServer = nil
314
332
return nil
315
333
}
316
334
317
335
// CheckCommitTransaction checks to see if allowed to commit transaction and returns
318
336
// an error if not allowed.
319
337
func (c * Client ) CheckCommitTransaction () error {
320
- if c .state == None {
338
+ if c .TransactionState == None {
321
339
return ErrNoTransactStarted
322
- } else if c .state == Aborted {
340
+ } else if c .TransactionState == Aborted {
323
341
return ErrCommitAfterAbort
324
342
}
325
343
return nil
@@ -332,7 +350,7 @@ func (c *Client) CommitTransaction() error {
332
350
if err != nil {
333
351
return err
334
352
}
335
- c .state = Committed
353
+ c .TransactionState = Committed
336
354
return nil
337
355
}
338
356
@@ -351,11 +369,11 @@ func (c *Client) UpdateCommitTransactionWriteConcern() {
351
369
// CheckAbortTransaction checks to see if allowed to abort transaction and returns
352
370
// an error if not allowed.
353
371
func (c * Client ) CheckAbortTransaction () error {
354
- if c .state == None {
372
+ if c .TransactionState == None {
355
373
return ErrNoTransactStarted
356
- } else if c .state == Committed {
374
+ } else if c .TransactionState == Committed {
357
375
return ErrAbortAfterCommit
358
- } else if c .state == Aborted {
376
+ } else if c .TransactionState == Aborted {
359
377
return ErrAbortTwice
360
378
}
361
379
return nil
@@ -368,7 +386,7 @@ func (c *Client) AbortTransaction() error {
368
386
if err != nil {
369
387
return err
370
388
}
371
- c .state = Aborted
389
+ c .TransactionState = Aborted
372
390
c .clearTransactionOpts ()
373
391
return nil
374
392
}
@@ -379,15 +397,15 @@ func (c *Client) ApplyCommand(desc description.Server) {
379
397
// Do not change state if committing after already committed
380
398
return
381
399
}
382
- if c .state == Starting {
383
- c .state = InProgress
400
+ if c .TransactionState == Starting {
401
+ c .TransactionState = InProgress
384
402
// If this is in a transaction and the server is a mongos, pin it
385
403
if desc .Kind == description .Mongos {
386
404
c .PinnedServer = & desc
387
405
}
388
- } else if c .state == Committed || c .state == Aborted {
406
+ } else if c .TransactionState == Committed || c .TransactionState == Aborted {
389
407
c .clearTransactionOpts ()
390
- c .state = None
408
+ c .TransactionState = None
391
409
}
392
410
}
393
411
0 commit comments