You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@pekko.apache.org by "pjfanning (via GitHub)" <gi...@apache.org> on 2026/08/31 16:59:40 UTC

[PR] feat: allow empty HTTP/2 DATA frames to be throttled [pekko-http]

pjfanning opened a new pull request, #1265:
URL: https://github.com/apache/pekko-http/pull/1265

   ### Motivation
   
   A DATA frame carrying no payload has `sizeInWindow == 0`, so it consumes no flow-control window. The *number* of such frames is therefore not bounded by flow control at all, and on an open stream a peer can send them continuously — each one costing a pass through the stream state machine, a `buffer ++= empty`, and a flow-control recompute.
   
   The `frame-type-throttle` mechanism could not be pointed at them. It maps an alias to a frame type name:
   
   ```scala
   case "reset" => Some("RstStreamFrame")
   ...
   case _       => None
   ```
   
   and had no alias for DATA, so DATA frames were unthrottleable by configuration.
   
   Adding a plain `"data"` alias would not have helped. Throttling *every* DATA frame at the configured rate throttles legitimate throughput along with it, so nobody could realistically enable it for this purpose — which is presumably why the config documents the throttle as being for "non-data frame types". Only the empty frames need it, because data-carrying ones are already bounded by flow control.
   
   ### Modification
   
   Add an `"empty-data"` alias that charges DATA frames whose payload is empty. It resolves to a name that is deliberately not a real `frameTypeName`, which `frameCost` recognises and applies to empty DATA frames only:
   
   ```scala
   case d: DataFrame if d.payload.isEmpty && frameTypesForThrottle.contains(EmptyDataFrameThrottleName) => 1
   ```
   
   Off by default, like every throttle target other than `"reset"`.
   
   ### Result
   
   An operator can throttle a flood of empty DATA frames without throttling data-carrying ones.
   
   ### Tests
   
   - `sbt "http-core/testOnly org.apache.pekko.http.impl.engine.http2.Http2BlueprintSpec"` — pass (11 tests), with a new case for the alias.
   - `sbt "http2-tests/testOnly ...Http2ServerEmptyDataThrottleSpec ...Http2ServerEnableFrameTypeThrottleSpec ...Http2ServerDisableFrameTypeThrottleSpec"` — pass (3 tests). The new spec floods empty DATA frames with the throttle configured for them and requires the connection to be torn down. Verified it fails when the alias resolves to `None`, which is the behaviour before this change.
   - `sbt http-core/mimaReportBinaryIssues` — pass.
   - Header on the new file generated with `sbt headerCreateAll`; native `scalafmt` clean.
   
   ### References
   
   Refs #332 - extends the frame type throttle to empty DATA frames
   
   🤖 Generated with [Claude Code](https://claude.com/claude-code)


-- 
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: notifications-unsubscribe@pekko.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org
For additional commands, e-mail: notifications-help@pekko.apache.org


Re: [PR] feat: throttle empty HTTP/2 DATA frames that do not end their stream [pekko-http]

Posted by "pjfanning (via GitHub) via notifications" <no...@pekko.apache.org>.
pjfanning merged PR #1265:
URL: https://github.com/apache/pekko-http/pull/1265


-- 
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: notifications-unsubscribe@pekko.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org
For additional commands, e-mail: notifications-help@pekko.apache.org


Re: [PR] feat: throttle empty HTTP/2 DATA frames that do not end their stream [pekko-http]

Posted by "Copilot (via GitHub) via notifications" <no...@pekko.apache.org>.
Copilot commented on code in PR #1265:
URL: https://github.com/apache/pekko-http/pull/1265#discussion_r4477654892


##########
http-core/src/main/resources/reference.conf:
##########
@@ -332,10 +332,25 @@ pekko.http {
       frame-type-throttle {
         # Configure the throttle for non-data frame types (https://github.com/apache/pekko-http/issues/332).

Review Comment:
   The comment says this throttle is for \"non-data frame types\", but the newly documented `empty-data*` options explicitly cover DATA frames. Please update the wording to avoid misleading operators (e.g., \"non-data frame types and selected empty DATA frames\" or similar).



##########
http2-tests/src/test/scala/org/apache/pekko/http/impl/engine/http2/Http2ServerEmptyDataNoEndStreamThrottleSpec.scala:
##########
@@ -0,0 +1,45 @@
+/*
+ * 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.
+ */
+
+package org.apache.pekko.http.impl.engine.http2
+
+import org.apache.pekko
+import pekko.http.impl.engine.http2.Http2Protocol.FrameType
+import pekko.http.impl.engine.http2.framing.FrameRenderer
+import pekko.util.ByteString
+
+/**
+ * Tests that DATA frames which carry no payload and do not end their stream are throttled by default. They consume
+ * no flow-control window, so unlike data-carrying frames their number is not bounded by flow control, and unlike the
+ * empty DATA frame that carries END_STREAM they have no legitimate use.
+ *
+ * The config deliberately does not set `frame-type-throttle.frame-types`, so this covers the default.
+ */
+class Http2ServerEmptyDataNoEndStreamThrottleSpec extends Http2SpecWithMaterializer("""
+    pekko.http.server.http2.log-frames = on
+  """) {
+  override val expectSevereLogsOnlyToMatch: Option[String] = Some(
+    "HTTP2 connection failed with error [Maximum throttle throughput exceeded.]. Sending INTERNAL_ERROR and closing connection.")
+
+  "The Http/2 server implementation" should {
+    "cancel connection when flooded with empty DATA frames that do not end the stream".inAssertAllStagesStopped(
+      new TestSetup with RequestResponseProbes {
+        val emptyDataFrame = FrameRenderer.renderFrame(FrameType.DATA, ByteFlag.Zero, 1, ByteString.empty)
+        network.sendBytes(Seq.fill(1000)(emptyDataFrame).reduce(_ ++ _))
+      })
+  }
+}

Review Comment:
   Same as in `Http2ServerEmptyDataThrottleSpec`: repeated `ByteString` concatenation via `reduce(_ ++ _)` can create many intermediate objects. Consider using a `ByteString` builder or single-pass concatenation to reduce allocations.



##########
http2-tests/src/test/scala/org/apache/pekko/http/impl/engine/http2/Http2ServerEmptyDataThrottleSpec.scala:
##########
@@ -0,0 +1,44 @@
+/*
+ * 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.
+ */
+
+package org.apache.pekko.http.impl.engine.http2
+
+import org.apache.pekko
+import pekko.http.impl.engine.http2.Http2Protocol.FrameType
+import pekko.http.impl.engine.http2.framing.FrameRenderer
+import pekko.util.ByteString
+
+/**
+ * Tests the opt-in "empty-data" throttle, which charges every DATA frame carrying no payload - including the one
+ * with END_STREAM that a client sends to close a request body. The default throttles only the empty DATA frames
+ * that do not end their stream, see [[Http2ServerEmptyDataNoEndStreamThrottleSpec]].
+ */
+class Http2ServerEmptyDataThrottleSpec extends Http2SpecWithMaterializer("""
+    pekko.http.server.http2.log-frames = on
+    pekko.http.server.http2.frame-type-throttle.frame-types = ["empty-data"]
+  """) {
+  override val expectSevereLogsOnlyToMatch: Option[String] = Some(
+    "HTTP2 connection failed with error [Maximum throttle throughput exceeded.]. Sending INTERNAL_ERROR and closing connection.")
+
+  "The Http/2 server implementation" should {
+    "cancel connection when flooded with empty DATA frames".inAssertAllStagesStopped(
+      new TestSetup with RequestResponseProbes {
+        val emptyDataFrame = FrameRenderer.renderFrame(FrameType.DATA, ByteFlag.Zero, 1, ByteString.empty)
+        network.sendBytes(Seq.fill(1000)(emptyDataFrame).reduce(_ ++ _))

Review Comment:
   `Seq.fill(...).reduce(_ ++ _)` repeatedly concatenates `ByteString`s and can be quadratic due to intermediate allocations. Prefer building once (e.g., using a `ByteString` builder or an API that concatenates a collection in one pass) to keep the test efficient and avoid allocation spikes.



-- 
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: notifications-unsubscribe@pekko.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org
For additional commands, e-mail: notifications-help@pekko.apache.org


Re: [PR] feat: throttle empty HTTP/2 DATA frames that do not end their stream [pekko-http]

Posted by "pjfanning (via GitHub) via notifications" <no...@pekko.apache.org>.
pjfanning commented on code in PR #1265:
URL: https://github.com/apache/pekko-http/pull/1265#discussion_r4478479028


##########
http2-tests/src/test/scala/org/apache/pekko/http/impl/engine/http2/Http2ServerEmptyDataThrottleSpec.scala:
##########
@@ -0,0 +1,44 @@
+/*
+ * 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.
+ */
+
+package org.apache.pekko.http.impl.engine.http2
+
+import org.apache.pekko
+import pekko.http.impl.engine.http2.Http2Protocol.FrameType
+import pekko.http.impl.engine.http2.framing.FrameRenderer
+import pekko.util.ByteString
+
+/**
+ * Tests the opt-in "empty-data" throttle, which charges every DATA frame carrying no payload - including the one
+ * with END_STREAM that a client sends to close a request body. The default throttles only the empty DATA frames
+ * that do not end their stream, see [[Http2ServerEmptyDataNoEndStreamThrottleSpec]].
+ */
+class Http2ServerEmptyDataThrottleSpec extends Http2SpecWithMaterializer("""
+    pekko.http.server.http2.log-frames = on
+    pekko.http.server.http2.frame-type-throttle.frame-types = ["empty-data"]
+  """) {
+  override val expectSevereLogsOnlyToMatch: Option[String] = Some(
+    "HTTP2 connection failed with error [Maximum throttle throughput exceeded.]. Sending INTERNAL_ERROR and closing connection.")
+
+  "The Http/2 server implementation" should {
+    "cancel connection when flooded with empty DATA frames".inAssertAllStagesStopped(
+      new TestSetup with RequestResponseProbes {
+        val emptyDataFrame = FrameRenderer.renderFrame(FrameType.DATA, ByteFlag.Zero, 1, ByteString.empty)
+        network.sendBytes(Seq.fill(1000)(emptyDataFrame).reduce(_ ++ _))

Review Comment:
   Declining this one: it builds 1000 small frames once in a test, so the cost is not significant, and `Http2ServerEnableFrameTypeThrottleSpec` and `Http2ServerDisableFrameTypeThrottleSpec` use the same idiom. Changing only the new specs would make them inconsistent.



##########
http2-tests/src/test/scala/org/apache/pekko/http/impl/engine/http2/Http2ServerEmptyDataNoEndStreamThrottleSpec.scala:
##########
@@ -0,0 +1,45 @@
+/*
+ * 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.
+ */
+
+package org.apache.pekko.http.impl.engine.http2
+
+import org.apache.pekko
+import pekko.http.impl.engine.http2.Http2Protocol.FrameType
+import pekko.http.impl.engine.http2.framing.FrameRenderer
+import pekko.util.ByteString
+
+/**
+ * Tests that DATA frames which carry no payload and do not end their stream are throttled by default. They consume
+ * no flow-control window, so unlike data-carrying frames their number is not bounded by flow control, and unlike the
+ * empty DATA frame that carries END_STREAM they have no legitimate use.
+ *
+ * The config deliberately does not set `frame-type-throttle.frame-types`, so this covers the default.
+ */
+class Http2ServerEmptyDataNoEndStreamThrottleSpec extends Http2SpecWithMaterializer("""
+    pekko.http.server.http2.log-frames = on
+  """) {
+  override val expectSevereLogsOnlyToMatch: Option[String] = Some(
+    "HTTP2 connection failed with error [Maximum throttle throughput exceeded.]. Sending INTERNAL_ERROR and closing connection.")
+
+  "The Http/2 server implementation" should {
+    "cancel connection when flooded with empty DATA frames that do not end the stream".inAssertAllStagesStopped(
+      new TestSetup with RequestResponseProbes {
+        val emptyDataFrame = FrameRenderer.renderFrame(FrameType.DATA, ByteFlag.Zero, 1, ByteString.empty)
+        network.sendBytes(Seq.fill(1000)(emptyDataFrame).reduce(_ ++ _))
+      })
+  }
+}

Review Comment:
   Declining this one: it builds 1000 small frames once in a test, so the cost is not significant, and `Http2ServerEnableFrameTypeThrottleSpec` and `Http2ServerDisableFrameTypeThrottleSpec` use the same idiom. Changing only the new specs would make them inconsistent.



-- 
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: notifications-unsubscribe@pekko.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org
For additional commands, e-mail: notifications-help@pekko.apache.org


Re: [PR] feat: throttle empty HTTP/2 DATA frames that do not end their stream [pekko-http]

Posted by "He-Pin (via GitHub) via notifications" <no...@pekko.apache.org>.
He-Pin commented on code in PR #1265:
URL: https://github.com/apache/pekko-http/pull/1265#discussion_r4477540052


##########
http-core/src/main/scala/org/apache/pekko/http/impl/engine/http2/Http2Blueprint.scala:
##########
@@ -238,17 +220,48 @@ private[http] object Http2Blueprint {
     }
   }
 
+  /**
+   * Not a real `frameTypeName`, so it never matches one directly: [[frameCost]] recognises it and charges every DATA
+   * frame that carries no payload.
+   *
+   * Such a frame consumes no flow-control window, so unlike a data-carrying one its number is not bounded by flow
+   * control at all and a peer can send them continuously. Data-carrying frames are deliberately not covered, because
+   * throttling those would throttle legitimate throughput along with them.
+   */
+  private[http2] val EmptyDataFrameThrottleName = "EmptyDataFrame"
+
+  /**
+   * As [[EmptyDataFrameThrottleName]], but only for the empty DATA frames that do not carry END_STREAM. An empty
+   * DATA frame with END_STREAM is how a client closes a request body whose length it did not know up front, so it is
+   * both legitimate and self-limiting: one per stream, after which the stream is half-closed. An empty DATA frame
+   * that does not end its stream has no such use, which is why this is the alias that is throttled by default.
+   */
+  private[http2] val EmptyDataFrameNoEndStreamThrottleName = "EmptyDataFrameNoEndStream"
+
+  private[http2] def frameCost(frameTypesForThrottle: Set[String], event: FrameEvent): Int =
+    if (frameTypesForThrottle.contains(event.frameTypeName)) 1
+    else event match {
+      case d: DataFrame if d.payload.isEmpty && isThrottledEmptyDataFrame(frameTypesForThrottle, d) => 1
+      case _                                                                                        => 0
+    }
+
+  private def isThrottledEmptyDataFrame(frameTypesForThrottle: Set[String], frame: DataFrame): Boolean =
+    frameTypesForThrottle.contains(EmptyDataFrameThrottleName) ||
+    (!frame.endStream && frameTypesForThrottle.contains(EmptyDataFrameNoEndStreamThrottleName))
+
   private[http2] def frameTypeAliasToFrameTypeName(frameType: String): Option[String] = {
     toRootLowerCase(frameType) match {
-      case "reset"         => Some("RstStreamFrame")
-      case "headers"       => Some("HeadersFrame")
-      case "continuation"  => Some("ContinuationFrame")
-      case "go-away"       => Some("GoAwayFrame")
-      case "priority"      => Some("PriorityFrame")
-      case "ping"          => Some("PingFrame")
-      case "push-promise"  => Some("PushPromiseFrame")
-      case "window-update" => Some("WindowUpdateFrame")
-      case _               => None
+      case "empty-data"               => Some(EmptyDataFrameThrottleName)
+      case "empty-data-no-end-stream" => Some(EmptyDataFrameNoEndStreamThrottleName)
+      case "reset"                    => Some("RstStreamFrame")
+      case "headers"                  => Some("HeadersFrame")
+      case "continuation"             => Some("ContinuationFrame")
+      case "go-away"                  => Some("GoAwayFrame")
+      case "priority"                 => Some("PriorityFrame")
+      case "ping"                     => Some("PingFrame")
+      case "push-promise"             => Some("PushPromiseFrame")
+      case "window-update"            => Some("WindowUpdateFrame")

Review Comment:
   I think we should extract these into a static constant.



-- 
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: notifications-unsubscribe@pekko.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org
For additional commands, e-mail: notifications-help@pekko.apache.org