本文整理汇总了Golang中github.com/coreos/etcd/mvcc.KV.TxnBegin方法的典型用法代码示例。如果您正苦于以下问题:Golang KV.TxnBegin方法的具体用法?Golang KV.TxnBegin怎么用?Golang KV.TxnBegin使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/coreos/etcd/mvcc.KV
的用法示例。
在下文中一共展示了KV.TxnBegin方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: applyTransaction
func applyTransaction(kv mvcc.KV, lessor lease.Lessor, req *etcdserverpb.TxnRequest) (*etcdserverpb.TxnResponse, error) {
var revision int64
ok := true
for _, c := range req.Compare {
if revision, ok = applyCompare(kv, c); !ok {
break
}
}
var reqs []*etcdserverpb.RequestUnion
if ok {
reqs = req.Success
} else {
reqs = req.Failure
}
if err := checkRequestLeases(lessor, reqs); err != nil {
return nil, err
}
if err := checkRequestRange(kv, reqs); err != nil {
return nil, err
}
// When executing the operations of txn, we need to hold the txn lock.
// So the reader will not see any intermediate results.
txnID := kv.TxnBegin()
defer func() {
err := kv.TxnEnd(txnID)
if err != nil {
panic(fmt.Sprint("unexpected error when closing txn", txnID))
}
}()
resps := make([]*etcdserverpb.ResponseUnion, len(reqs))
for i := range reqs {
resps[i] = applyUnion(txnID, kv, reqs[i])
}
if len(resps) != 0 {
revision++
}
txnResp := &etcdserverpb.TxnResponse{}
txnResp.Header = &etcdserverpb.ResponseHeader{}
txnResp.Header.Revision = revision
txnResp.Responses = resps
txnResp.Succeeded = ok
return txnResp, nil
}