Copilot commented on code in PR #3634:
URL: https://github.com/apache/thrift/pull/3634#discussion_r3566588205


##########
lib/rb/lib/thrift/server/rack_application.rb:
##########
@@ -0,0 +1,83 @@
+# frozen_string_literal: true
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+
+require 'rack'
+require 'stringio'
+require 'thrift/multiplexed_processor'
+require 'thrift/transport/io_stream_transport'
+require 'thrift/transport/memory_buffer_transport'
+require 'thrift/types'
+
+module Thrift
+  class RackApplication
+    THRIFT_HEADER = "application/x-thrift"
+
+    def initialize(processor, protocol_factory = BinaryProtocolFactory.new)
+      @processor = processor
+      @protocol_factory = protocol_factory
+    end
+
+    def call(env)
+      request = Rack::Request.new(env)
+
+      if self.class.valid_thrift_request?(request)
+        self.class.successful_request(request, @processor, 
@protocol_factory).finish
+      else
+        self.class.failed_request.finish
+      end
+    end
+
+    def self.mapped(path, processor, protocol_factory = 
BinaryProtocolFactory.new)
+      Rack::Builder.new do
+        use Rack::CommonLogger
+        use Rack::ShowExceptions
+        use Rack::Lint
+        use Rack::ContentLength
+        map path do
+          run RackApplication.new(processor, protocol_factory)
+        end
+      end
+    end
+
+    def self.successful_request(rack_request, processor, protocol_factory)
+      response = Rack::Response.new([], 200, {Rack::CONTENT_TYPE => 
THRIFT_HEADER})
+      input_protocol = request_protocol(rack_request, protocol_factory).first
+      output_protocol = 
protocol_factory.get_protocol(IOStreamTransport.new(StringIO.new, response))
+      processor.process input_protocol, output_protocol
+
+      response
+    end
+
+    def self.failed_request
+      Rack::Response.new(['Not Found'], 404, {Rack::CONTENT_TYPE => 
THRIFT_HEADER})
+    end
+
+    def self.valid_thrift_request?(rack_request)
+      rack_request.post? && rack_request.media_type == THRIFT_HEADER
+    end
+
+    def self.request_protocol(rack_request, protocol_factory)
+      protocol = 
protocol_factory.get_protocol(MemoryBufferTransport.new(rack_request.body.read))
+      name, type, seqid = protocol.read_message_begin
+
+      [StoredMessageProtocol.new(protocol, [name, type, seqid]), type]

Review Comment:
   `request_protocol` currently buffers the entire request body into memory 
(`rack_request.body.read`) before protocol parsing. That makes memory usage 
proportional to request size and can become a reliability bottleneck under 
large Thrift payloads. It’s possible to keep the current StoredMessageProtocol 
approach while streaming from `rack_request.body` (read only the message header 
eagerly, then continue reading from the same underlying stream).



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to