コード例 #1
0
ファイル: lease.go プロジェクト: Clarifai/kubernetes
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
		}

		ttl, err := ls.le.LeaseRenew(lease.LeaseID(req.ID))
		if err == lease.ErrLeaseNotFound {
			return rpctypes.ErrLeaseNotFound
		}

		if err != nil && err != lease.ErrLeaseNotFound {
			return err
		}

		resp := &pb.LeaseKeepAliveResponse{ID: req.ID, TTL: ttl}
		err = stream.Send(resp)
		if err != nil {
			return err
		}
	}
}
コード例 #2
0
ファイル: lease.go プロジェクト: hadoop835/etcd
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
		}

		ttl, err := ls.le.LeaseRenew(lease.LeaseID(req.ID))
		if err != nil {
			if err == lease.ErrLeaseNotFound {
				return ErrLeaseNotFound
			}
			// TODO: handle not primary error by forwarding renew requests to leader
			panic("TODO: handle not primary error by forwarding renew requests to leader")
		}

		resp := &pb.LeaseKeepAliveResponse{ID: req.ID, TTL: ttl}
		err = stream.Send(resp)
		if err != nil {
			return err
		}
	}
}
コード例 #3
0
ファイル: lease.go プロジェクト: mgurevin/etcd
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
		}
	}
}
コード例 #4
0
ファイル: lease.go プロジェクト: CliffYuan/etcd
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(lease.LeaseID(req.ID))
		if err == lease.ErrLeaseNotFound {
			err = nil
			ttl = 0
		}

		if err != nil {
			return err
		}

		resp.TTL = ttl
		err = stream.Send(resp)
		if err != nil {
			return err
		}
	}
}