Skip to content

Commit

Permalink
feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
ehaydenr committed Dec 20, 2024
1 parent f75a34f commit a95faa1
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ let package = Package(
.library(name: "NIOHTTPResponsiveness", targets: ["NIOHTTPResponsiveness"]),
],
dependencies: [
.package(url: "https://github.com/apple/swift-nio.git", from: "2.67.0"),
.package(url: "https://github.com/apple/swift-nio.git", from: "2.77.0"),
.package(url: "https://github.com/apple/swift-nio-http2.git", from: "1.27.0"),
.package(url: "https://github.com/apple/swift-http-types.git", from: "1.3.0"),
.package(url: "https://github.com/apple/swift-http-structured-headers.git", from: "1.1.0"),
Expand Down
22 changes: 18 additions & 4 deletions Sources/NIOHTTPResponsiveness/HTTPDrippingDownloadHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public final class HTTPDrippingDownloadHandler: ChannelDuplexHandler {

private var phase = Phase.waitingOnHead
private var scheduled: Scheduled<Void>?
private var scheduledCallbackHandler: HTTPDrippingDownloadHandlerScheduledCallbackHandler?
private var pendingRead = false
private var pendingWrite = false
private var activelyWritingChunk = false
Expand Down Expand Up @@ -156,6 +157,7 @@ public final class HTTPDrippingDownloadHandler: ChannelDuplexHandler {
public func channelInactive(context: ChannelHandlerContext) {
self.phase = .done
self.scheduled?.cancel()
context.fireChannelInactive()
}

public func channelWritabilityChanged(context: ChannelHandlerContext) {
Expand Down Expand Up @@ -219,10 +221,22 @@ public final class HTTPDrippingDownloadHandler: ChannelDuplexHandler {
// More chunks to write.. Kick off timer
drippingState.currentChunkBytesLeft = self.size
self.phase = .dripping(drippingState)
let this = NIOLoopBound(self, eventLoop: context.eventLoop)
let loopBoundContext = NIOLoopBound(context, eventLoop: context.eventLoop)
self.scheduled = context.eventLoop.scheduleTask(in: self.frequency) {
this.value.writeChunk(context: loopBoundContext.value)
if self.scheduledCallbackHandler == nil {
let this = NIOLoopBound(self, eventLoop: context.eventLoop)
let loopBoundContext = NIOLoopBound(context, eventLoop: context.eventLoop)
self.scheduledCallbackHandler = HTTPDrippingDownloadHandlerScheduledCallbackHandler(handler: this, context: loopBoundContext)
}
// SAFTEY: scheduling the callback only potentially throws when invoked off eventloop
try! context.eventLoop.scheduleCallback(in: self.frequency, handler: self.scheduledCallbackHandler!)
}

private struct HTTPDrippingDownloadHandlerScheduledCallbackHandler: NIOScheduledCallbackHandler & Sendable {
var handler: NIOLoopBound<HTTPDrippingDownloadHandler>
var context: NIOLoopBound<ChannelHandlerContext>

func handleScheduledCallback(eventLoop: some EventLoop) {
self.handler.value.writeChunk(context: self.context.value)
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public final class HTTPReceiveDiscardHandler: ChannelInboundHandler {
string:
"Received in excess of expectation; expected(\(expectation)) received(\(self.received))"
)
self.writeSimpleResponse(context: context, status: .ok, body: body)
self.writeSimpleResponse(context: context, status: .badRequest, body: body)
}

private func writeSimpleResponse(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ public final class SimpleResponsivenessRequestMux: ChannelInboundHandler {
status: .ok,
body: self.responsivenessConfigBuffer
)
case (.get, .some(""), .some("responsiveness"), .some("download"), .some(let count)):
self.addHandlerOrInternalError(context: context, handler: HTTPDrippingDownloadHandler(count: 1, size: count))
case (.get, .some(""), .some("responsiveness"), .some("download"), .some(let size)):
self.addHandlerOrInternalError(context: context, handler: HTTPDrippingDownloadHandler(count: 1, size: size))
case (.post, .some(""), .some("responsiveness"), .some("upload"), .none):
// Check if we should expect a certain count
var expectation: Int?
Expand Down

0 comments on commit a95faa1

Please sign in to comment.