This repository has been archived by the owner on Dec 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
347 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
require "monitor" | ||
|
||
module Timber | ||
module LogDevices | ||
class HTTP | ||
# Maintains a triggered buffer, where the trigger is {PAYLOAD_LIMIT_BYTES}. Once the buffer | ||
# exceeds this limit it will lock and return that buffer up to that point while still making | ||
# a new buffer available for writes. This ensures that the HTTP client can attempt to deliver | ||
# the buffer contents without blocking execution of the application. | ||
# | ||
# If the overall buffer exceeeds the overall limit (specified by the `:limit_bytes` option), | ||
# then a buffer overflow is triggered. This can be customized using the `:overflow_handler` | ||
# option. | ||
class TriggeredBuffer | ||
DEFAULT_PAYLOAD_LIMIT_BYTES = 5_000_000 # 5mb, the Timber API will not accept messages larger than this | ||
DEFAULT_LIMIT_BYTES = 50_000_000 # 50mb | ||
|
||
def initialize(options = {}) | ||
@buffers = [] | ||
@monitor = Monitor.new | ||
@payload_limit_bytes = options[:payload_limit_bytes] || DEFAULT_PAYLOAD_LIMIT_BYTES | ||
@limit_bytes = options[:limit_bytes] || DEFAULT_LIMIT_BYTES | ||
@overflow_handler = options[:overflow_handler] | ||
end | ||
|
||
def write(msg) | ||
if msg.bytesize > @payload_limit_bytes || (msg.bytesize + total_bytesize) > @limit_bytes | ||
handle_overflow(msg) | ||
return nil | ||
end | ||
|
||
@monitor.synchronize do | ||
buffer = writable_buffer | ||
if @buffers == [] || buffer.nil? || buffer.frozen? | ||
@buffers << msg | ||
nil | ||
elsif (buffer.bytesize + msg.bytesize) > @payload_limit_bytes | ||
@buffers << msg | ||
buffer.freeze | ||
else | ||
buffer << msg | ||
nil | ||
end | ||
end | ||
end | ||
|
||
def reserve | ||
@monitor.synchronize do | ||
buffer = writable_buffer | ||
if buffer | ||
buffer.freeze | ||
end | ||
end | ||
end | ||
|
||
def remove(buffer) | ||
@monitor.synchronize do | ||
@buffers.delete(buffer) | ||
end | ||
end | ||
|
||
private | ||
def total_bytesize | ||
@buffers.reduce(0) { |acc, buffer| acc + buffer.bytesize } | ||
end | ||
|
||
def writable_buffer | ||
@buffers.find { |buffer| !buffer.frozen? } | ||
end | ||
|
||
def handle_overflow(msg) | ||
if @overflow_handler | ||
@overflow_handler.call(msg) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
require "spec_helper" | ||
|
||
describe Timber::LogDevices::HTTP::TriggeredBuffer do | ||
describe "#write" do | ||
it "should trigger a buffer overflow for large messages" do | ||
buffer = described_class.new(:payload_limit_bytes => 10) | ||
msg = "a" * 11 | ||
expect(buffer).to receive(:handle_overflow).exactly(1).times.with(msg) | ||
buffer.write(msg) | ||
end | ||
|
||
it "should trigger a buffer overflow when exceeding the limit" do | ||
buffer = described_class.new(:limit_bytes => 10) | ||
msg = "a" * 11 | ||
expect(buffer).to receive(:handle_overflow).exactly(1).times.with(msg) | ||
buffer.write(msg) | ||
end | ||
|
||
it "should start a new buffer when empty and append when not" do | ||
buffer = described_class.new | ||
result = buffer.write("test") | ||
expect(result).to be_nil | ||
expect(buffer.send(:writable_buffer)).to eq("test") | ||
result = buffer.write("again") | ||
expect(result).to be_nil | ||
expect(buffer.send(:writable_buffer)).to eq("testagain") | ||
end | ||
|
||
it "should return the old buffer when it has exceeded it's limit" do | ||
buffer = described_class.new(:payload_limit_bytes => 10) | ||
msg = "a" * 6 | ||
result = buffer.write(msg) | ||
expect(result).to be_nil | ||
result = buffer.write(msg) | ||
expect(result).to eq(msg) | ||
expect(result).to be_frozen | ||
end | ||
|
||
it "should write a new buffer when the latest is frozen" do | ||
buffer = described_class.new | ||
buffer.write("test") | ||
result = buffer.reserve | ||
expect(result).to eq("test") | ||
buffer.write("again") | ||
expect(buffer.send(:writable_buffer)).to eq("again") | ||
end | ||
end | ||
|
||
describe "#reserve" do | ||
it "should reserve the latest buffer and freeze it" do | ||
buffer = described_class.new | ||
buffer.write("test") | ||
result = buffer.reserve | ||
expect(result).to eq("test") | ||
expect(result).to be_frozen | ||
end | ||
end | ||
end |
Oops, something went wrong.