Skip to content

Commit

Permalink
fix: use bounded queue for usage reporter
Browse files Browse the repository at this point in the history
  • Loading branch information
aryascripts committed Oct 28, 2024
1 parent e5e4314 commit 72e9da0
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
4 changes: 3 additions & 1 deletion lib/graphql-hive.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class Hive < GraphQL::Tracing::PlatformTracing
read_operations: true,
report_schema: true,
buffer_size: 50,
bounded_queue_multiple: 5,
logger: nil,
collect_usage_sampling: 1.0
}.freeze
Expand Down Expand Up @@ -134,7 +135,8 @@ def initialize_options!(options)
options[:logger] = Logger.new($stderr)
original_formatter = Logger::Formatter.new
options[:logger].formatter = proc { |severity, datetime, progname, msg|
original_formatter.call(severity, datetime, progname, "[hive] #{msg.dump}")
msg = msg.respond_to?(:dump) ? msg.dump : msg
original_formatter.call(severity, datetime, progname, "[hive] #{msg}")
}
options[:logger].level = options[:debug] ? Logger::DEBUG : Logger::INFO
end
Expand Down
9 changes: 5 additions & 4 deletions lib/graphql-hive/usage_reporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require "digest"
require "graphql-hive/analyzer"
require "graphql-hive/printer"
require "graphql-hive/bounded_queue"

module GraphQL
class Hive < GraphQL::Tracing::PlatformTracing
Expand All @@ -16,15 +17,14 @@ def self.instance

def initialize(options, client)
@@instance = self

@options = options
@client = client

@options_mutex = Mutex.new
@queue = Queue.new

@sampler = Sampler.new(options[:collect_usage_sampling], options[:logger]) # NOTE: logs for deprecated field

queue_bound = (options[:buffer_size] * options[:bounded_queue_multiple]).to_int
@queue = BoundedQueue.new(size: queue_bound, logger: options[:logger])

start_thread
end

Expand All @@ -51,6 +51,7 @@ def start_thread

@thread = Thread.new do
buffer = []

while (operation = @queue.pop(false))
@options[:logger].debug("processing operation from queue: #{operation}")
buffer << operation if @sampler.sample?(operation)
Expand Down
12 changes: 12 additions & 0 deletions spec/graphql/graphql-hive/bounded_queue_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,16 @@
expect(queue.size).to eq(2)
expect(logger).to have_received(:error).with("BoundedQueue is full, discarding operation").twice
end

it "allows pushes after pops" do
queue.push("one")
queue.push("two")

queue.push("invalid")
expect(queue.size).to eq(2)

queue.pop
queue.push("three")
expect(queue.size).to eq(2)
end
end

Check failure on line 47 in spec/graphql/graphql-hive/bounded_queue_spec.rb

View workflow job for this annotation

GitHub Actions / standard

Layout/TrailingEmptyLines: Final newline missing.

0 comments on commit 72e9da0

Please sign in to comment.