The current repo belongs to Paused status, and some functions are restricted. For details, please refer to the description of repo status
55 Star 271 Fork 100

fagongzi/gateway
Paused

Create your Gitee Account
Explore and code with more than 13.5 million developers,Free private repositories !:)
Sign up
文件
Clone or Download
txn.go 1.14 KB
Copy Edit Raw Blame History
张旭 authored 2017-12-11 10:46 +08:00 . dev: use grpc
package store
import (
"time"
"github.com/coreos/etcd/clientv3"
"github.com/fagongzi/log"
"golang.org/x/net/context"
)
// slowLogTxn wraps etcd transaction and log slow one.
type slowLogTxn struct {
clientv3.Txn
cancel context.CancelFunc
}
func newSlowLogTxn(client *clientv3.Client) clientv3.Txn {
ctx, cancel := context.WithTimeout(client.Ctx(), DefaultRequestTimeout)
return &slowLogTxn{
Txn: client.Txn(ctx),
cancel: cancel,
}
}
func (t *slowLogTxn) If(cs ...clientv3.Cmp) clientv3.Txn {
return &slowLogTxn{
Txn: t.Txn.If(cs...),
cancel: t.cancel,
}
}
func (t *slowLogTxn) Then(ops ...clientv3.Op) clientv3.Txn {
return &slowLogTxn{
Txn: t.Txn.Then(ops...),
cancel: t.cancel,
}
}
// Commit implements Txn Commit interface.
func (t *slowLogTxn) Commit() (*clientv3.TxnResponse, error) {
start := time.Now()
resp, err := t.Txn.Commit()
t.cancel()
cost := time.Now().Sub(start)
if cost > DefaultSlowRequestTime {
log.Warn("slow: txn runs too slow, resp=<%v> cost=<%s> errors:\n %+v",
resp,
cost,
err)
}
return resp, err
}
func (e *EtcdStore) txn() clientv3.Txn {
return newSlowLogTxn(e.rawClient)
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/fagongzi/gateway.git
git@gitee.com:fagongzi/gateway.git
fagongzi
gateway
gateway
v2.5.1

Search