From 2e3d3ec2f8b56832ac6efbe6033a663577a56b34 Mon Sep 17 00:00:00 2001 From: Armon Dadgar Date: Thu, 15 May 2014 11:50:30 -0700 Subject: [PATCH] consul: Adding Defer to MDBTxn --- consul/mdb_table.go | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/consul/mdb_table.go b/consul/mdb_table.go index f70923cb3..c4c84b0dc 100644 --- a/consul/mdb_table.go +++ b/consul/mdb_table.go @@ -63,6 +63,7 @@ type MDBTxn struct { readonly bool tx *mdb.Txn dbis map[string]mdb.DBI + after []func() } // Abort is used to close the transaction @@ -74,7 +75,19 @@ func (t *MDBTxn) Abort() { // Commit is used to commit a transaction func (t *MDBTxn) Commit() error { - return t.tx.Commit() + if err := t.tx.Commit(); err != nil { + return err + } + for _, f := range t.after { + f() + } + t.after = nil + return nil +} + +// Defer is used to defer a function call until a successful commit +func (t *MDBTxn) Defer(f func()) { + t.after = append(t.after, f) } type IndexFunc func(*MDBIndex, []string) string