Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 40 additions & 3 deletions lib/etcdv3/lease.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,32 @@
class Etcdv3
class Lease

# this is used for gRPC proxy compatibility so that we do not
# mark as finished writing until we've received a response
class BlockingRequest
def initialize(request_op)
@blocked = false
@request_op = request_op
end

def read_done!
@proceed = true
end

def blocked?
@blocked
end

def each
@blocked = true

yield @request_op

sleep 0.001 until @proceed == true
@blocked = false
end
end

def initialize(hostname, credentials, timeout, metadata={})
@stub = Etcdserverpb::Lease::Stub.new(hostname, credentials)
@timeout = timeout
Expand All @@ -22,10 +49,20 @@ def lease_ttl(id, timeout: nil)
end

def lease_keep_alive_once(id, timeout: nil)
request = Etcdserverpb::LeaseKeepAliveRequest.new(ID: id)
@stub.lease_keep_alive([request], metadata: @metadata, deadline: deadline(timeout)).each do |resp|
return resp
request = BlockingRequest.new Etcdserverpb::LeaseKeepAliveRequest.new(ID: id)
response = nil
begin
@stub.lease_keep_alive(request, metadata: @metadata, deadline: deadline(timeout)).each do |resp|
response = resp
break;
end
ensure
request.read_done!
while request.blocked?
sleep 0.001
end
end
return response
end

private
Expand Down
5 changes: 5 additions & 0 deletions spec/etcdv3/lease_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@
stub = local_stub(Etcdv3::Lease, 0)
expect { stub.lease_keep_alive_once(id) }.to raise_error(GRPC::DeadlineExceeded)
end
it "doesn't orphan threads if there is a server error" do
expect_any_instance_of(GRPC::BidiCall).to receive(:read_loop).and_raise(GRPC::DeadlineExceeded)
stub = local_stub(Etcdv3::Lease, 2)
expect { stub.lease_keep_alive_once(314159) rescue nil; sleep 0.5}.to_not change { Thread.list.size }
end
end

describe '#lease_ttl' do
Expand Down