本文整理汇总了Golang中github.com/coreos/etcd/etcdserver/etcdserverpb.Lease_LeaseKeepAliveServer.Context方法的典型用法代码示例。如果您正苦于以下问题:Golang Lease_LeaseKeepAliveServer.Context方法的具体用法?Golang Lease_LeaseKeepAliveServer.Context怎么用?Golang Lease_LeaseKeepAliveServer.Context使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/coreos/etcd/etcdserver/etcdserverpb.Lease_LeaseKeepAliveServer
的用法示例。
在下文中一共展示了Lease_LeaseKeepAliveServer.Context方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: LeaseKeepAlive
func (lp *leaseProxy) LeaseKeepAlive(stream pb.Lease_LeaseKeepAliveServer) error {
conn := lp.client.ActiveConnection()
ctx, cancel := context.WithCancel(stream.Context())
lc, err := pb.NewLeaseClient(conn).LeaseKeepAlive(ctx)
if err != nil {
cancel()
return err
}
go func() {
// Cancel the context attached to lc to unblock lc.Recv when
// this routine returns on error.
defer cancel()
for {
// stream.Recv will be unblock when the loop in the parent routine
// returns on error.
rr, err := stream.Recv()
if err != nil {
return
}
err = lc.Send(rr)
if err != nil {
return
}
}
}()
for {
rr, err := lc.Recv()
if err != nil {
return err
}
err = stream.Send(rr)
if err != nil {
return err
}
}
}
示例2: LeaseKeepAlive
func (ls *LeaseServer) LeaseKeepAlive(stream pb.Lease_LeaseKeepAliveServer) error {
for {
req, err := stream.Recv()
if err == io.EOF {
return nil
}
if err != nil {
return err
}
// Create header before we sent out the renew request.
// This can make sure that the revision is strictly smaller or equal to
// when the keepalive happened at the local server (when the local server is the leader)
// or remote leader.
// Without this, a lease might be revoked at rev 3 but client can see the keepalive succeeded
// at rev 4.
resp := &pb.LeaseKeepAliveResponse{ID: req.ID, Header: &pb.ResponseHeader{}}
ls.hdr.fill(resp.Header)
ttl, err := ls.le.LeaseRenew(stream.Context(), lease.LeaseID(req.ID))
if err == lease.ErrLeaseNotFound {
err = nil
ttl = 0
}
if err != nil {
return togRPCError(err)
}
resp.TTL = ttl
err = stream.Send(resp)
if err != nil {
return err
}
}
}