Skip to content

Commit

Permalink
Remove allocator from context (#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-fowler authored Aug 14, 2024
1 parent 3d694eb commit b199fd6
Show file tree
Hide file tree
Showing 11 changed files with 19 additions and 27 deletions.
4 changes: 1 addition & 3 deletions Sources/HummingbirdWSClient/WebSocketClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,9 @@ public struct WebSocketClient {
/// Basic context implementation of ``WebSocketContext``.
/// Used by non-router web socket handle function
public struct Context: WebSocketContext {
public let allocator: ByteBufferAllocator
public let logger: Logger

package init(allocator: ByteBufferAllocator, logger: Logger) {
self.allocator = allocator
package init(logger: Logger) {
self.logger = logger
}
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/HummingbirdWSClient/WebSocketClientChannel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ struct WebSocketClientChannel: ClientConnectionChannel {
autoPing: self.configuration.autoPing
),
asyncChannel: webSocketChannel,
context: WebSocketClient.Context(allocator: webSocketChannel.channel.allocator, logger: logger),
context: WebSocketClient.Context(logger: logger),
handler: self.handler
)
case .notUpgraded:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,11 @@ struct PerMessageDeflateExtension: WebSocketExtension {
unmaskedData.writeBytes([0, 0, 255, 255])
self.state = .idle
}
frame.data = try unmaskedData.decompressStream(with: self.decompressor, maxSize: maxSize, allocator: context.allocator)
frame.data = try unmaskedData.decompressStream(
with: self.decompressor,
maxSize: maxSize,
allocator: ByteBufferAllocator()
)
frame.maskKey = nil
if resetStream, frame.fin {
try self.decompressor.resetStream()
Expand Down Expand Up @@ -241,7 +245,7 @@ struct PerMessageDeflateExtension: WebSocketExtension {
newFrame.rsv1 = true
self.sendState = .sendingMessage
}
newFrame.data = try newFrame.data.compressStream(with: self.compressor, flush: .sync, allocator: context.allocator)
newFrame.data = try newFrame.data.compressStream(with: self.compressor, flush: .sync, allocator: ByteBufferAllocator())
// if final frame then remove last four bytes 0x00 0x00 0xff 0xff
// (see https://datatracker.ietf.org/doc/html/rfc7692#section-7.2.1)
if newFrame.fin {
Expand Down
1 change: 0 additions & 1 deletion Sources/HummingbirdWSCore/WebSocketContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,5 @@ import NIOCore

/// Protocol for WebSocket Data handling functions context parameter
public protocol WebSocketContext: Sendable {
var allocator: ByteBufferAllocator { get }
var logger: Logger { get }
}
4 changes: 1 addition & 3 deletions Sources/HummingbirdWSCore/WebSocketExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,9 @@ import NIOWebSocket

/// Basic context implementation of ``WebSocketContext``.
public struct WebSocketExtensionContext {
public let allocator: ByteBufferAllocator
public let logger: Logger

init(allocator: ByteBufferAllocator, logger: Logger) {
self.allocator = allocator
init(logger: Logger) {
self.logger = logger
}
}
Expand Down
8 changes: 3 additions & 5 deletions Sources/HummingbirdWSCore/WebSocketHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ package actor WebSocketHandler {
let type: WebSocketType
let configuration: Configuration
let logger: Logger
let allocator: ByteBufferAllocator
var pingData: ByteBuffer
var pingTime: ContinuousClock.Instant = .now
var closeState: CloseState
Expand All @@ -94,7 +93,6 @@ package actor WebSocketHandler {
self.type = type
self.configuration = configuration
self.logger = context.logger
self.allocator = context.allocator
self.pingData = ByteBufferAllocator().buffer(capacity: Self.pingDataSize)
self.closeState = .open
}
Expand Down Expand Up @@ -208,7 +206,7 @@ package actor WebSocketHandler {
for ext in self.configuration.extensions {
frame = try await ext.processFrameToSend(
frame,
context: WebSocketExtensionContext(allocator: self.allocator, logger: self.logger)
context: WebSocketExtensionContext(logger: self.logger)
)
}
} catch {
Expand Down Expand Up @@ -281,7 +279,7 @@ package actor WebSocketHandler {
) async throws {
switch self.closeState {
case .open:
var buffer = self.allocator.buffer(capacity: 2 + (reason?.utf8.count ?? 0))
var buffer = ByteBufferAllocator().buffer(capacity: 2 + (reason?.utf8.count ?? 0))
buffer.write(webSocketErrorCode: code)
if let reason {
buffer.writeString(reason)
Expand Down Expand Up @@ -326,7 +324,7 @@ package actor WebSocketHandler {
.protocolError
}

var buffer = self.allocator.buffer(capacity: 2)
var buffer = ByteBufferAllocator().buffer(capacity: 2)
buffer.write(webSocketErrorCode: code)

try await self.write(frame: .init(fin: true, opcode: .connectionClose, data: buffer))
Expand Down
2 changes: 1 addition & 1 deletion Sources/HummingbirdWSCore/WebSocketInboundStream.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public final class WebSocketInboundStream: AsyncSequence, Sendable {
for ext in self.handler.configuration.extensions.reversed() {
frame = try await ext.processReceivedFrame(
frame,
context: WebSocketExtensionContext(allocator: self.handler.allocator, logger: self.handler.logger)
context: WebSocketExtensionContext(logger: self.handler.logger)
)
}
return .init(from: frame)
Expand Down
4 changes: 2 additions & 2 deletions Sources/HummingbirdWSCore/WebSocketOutboundWriter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public struct WebSocketOutboundWriter: Sendable {
try await self.handler.write(frame: .init(fin: true, opcode: .binary, data: buffer))
case .text(let string):
// send text based data
let buffer = self.handler.allocator.buffer(string: string)
let buffer = ByteBuffer(string: string)
try await self.handler.write(frame: .init(fin: true, opcode: .text, data: buffer))
case .pong:
// send unexplained pong as a heartbeat
Expand Down Expand Up @@ -73,7 +73,7 @@ public struct WebSocketOutboundWriter: Sendable {

/// Write string to WebSocket frame
public mutating func callAsFunction(_ text: String) async throws {
let buffer = self.handler.allocator.buffer(string: text)
let buffer = ByteBuffer(string: text)
try await self.write(buffer, opcode: self.opcode)
}

Expand Down
8 changes: 3 additions & 5 deletions Sources/HummingbirdWebSocket/WebSocketChannel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,9 @@ public struct HTTP1WebSocketUpgradeChannel: ServerChildChannel, HTTPChannelHandl
/// Basic context implementation of ``WebSocketContext``.
/// Used by non-router web socket handle function
public struct Context: WebSocketContext {
public let allocator: ByteBufferAllocator
public let logger: Logger

package init(allocator: ByteBufferAllocator, logger: Logger) {
self.allocator = allocator
package init(logger: Logger) {
self.logger = logger
}
}
Expand Down Expand Up @@ -77,7 +75,7 @@ public struct HTTP1WebSocketUpgradeChannel: ServerChildChannel, HTTPChannelHandl
logger: logger
)
return (headers, { asyncChannel, logger in
let context = Context(allocator: channel.allocator, logger: logger)
let context = Context(logger: logger)
do {
_ = try await WebSocketHandler.handle(
type: .server,
Expand Down Expand Up @@ -126,7 +124,7 @@ public struct HTTP1WebSocketUpgradeChannel: ServerChildChannel, HTTPChannelHandl
logger: logger
)
return (headers, { asyncChannel, logger in
let context = Context(allocator: channel.allocator, logger: logger)
let context = Context(logger: logger)
do {
_ = try await WebSocketHandler.handle(
type: .server,
Expand Down
3 changes: 0 additions & 3 deletions Sources/HummingbirdWebSocket/WebSocketRouter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@ public struct WebSocketRouterContext<Context: WebSocketContext>: WebSocketContex
/// Logger attached to request context
@inlinable
public var logger: Logger { self.requestContext.logger }
/// ByteBuffer allocator attached to request context
@inlinable
public var allocator: ByteBufferAllocator { self.requestContext.allocator }
}

/// Reference to a WebSocket handler
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ struct XorWebSocketExtension: WebSocketExtension {
func shutdown() {}

func xorFrame(_ frame: WebSocketFrame, context: WebSocketExtensionContext) -> WebSocketFrame {
var newBuffer = context.allocator.buffer(capacity: frame.data.readableBytes)
var newBuffer = ByteBufferAllocator().buffer(capacity: frame.data.readableBytes)
for byte in frame.unmaskedData.readableBytesView {
newBuffer.writeInteger(byte ^ self.value)
}
Expand Down

0 comments on commit b199fd6

Please sign in to comment.