diff --git a/lib/graphql-hive.rb b/lib/graphql-hive.rb index 5bbb6b0..ea7be94 100644 --- a/lib/graphql-hive.rb +++ b/lib/graphql-hive.rb @@ -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 @@ -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 diff --git a/lib/graphql-hive/usage_reporter.rb b/lib/graphql-hive/usage_reporter.rb index 5d6d7e1..3ea1153 100644 --- a/lib/graphql-hive/usage_reporter.rb +++ b/lib/graphql-hive/usage_reporter.rb @@ -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 @@ -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 @@ -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) diff --git a/spec/graphql/graphql-hive/bounded_queue_spec.rb b/spec/graphql/graphql-hive/bounded_queue_spec.rb index 11236c2..7bb0dc2 100644 --- a/spec/graphql/graphql-hive/bounded_queue_spec.rb +++ b/spec/graphql/graphql-hive/bounded_queue_spec.rb @@ -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 \ No newline at end of file