Commit Graph
55 Commits
Author SHA1 Message Date
akash-55andCory Benfield 7688cf712f #1714 Update Codec.swift (#1873)
* Update Codec.swift

* Update Sources/NIO/Codec.swift

Co-authored-by: Cory Benfield <lukasa@apple.com>

Co-authored-by: Cory Benfield <lukasa@apple.com>
2021-06-09 15:57:47 +01:00
Johannes Weiss c5a214f075 B2MD: Don't try to reclaim if continuing to parse (#1733)
Motivation:

B2MD called out to the decoder's `shouldReclaimBytes` after every
parsing attempt, even if the parser said `.continue`.

That's quite pointless because we won't add any bytes into the buffer
before we're trying the parser again.

Modifications:

Only ask the decoder if we should reclaim bytes if the decoder actually
says `.needMoreData`.

Result:

Faster, better, more sensible.
2021-01-29 17:03:04 +00:00
Fabian Fett 6b41c18bf6 Fix Codec memory reclaim bug (#1729) 2021-01-21 20:32:20 +00:00
Shekhar Rajak 2900f03352 ByteToMessageDecoder: Default implementation for decodeLast should be provided (#1426)
* removed decodeLast function from NIOChatServer

* updated decodeLast function from WebSocketFrameDecoder

* default implementation of decodeLast function

* removed implementation of decodeLast in WebSocketFrameDecoder
2020-03-04 16:22:06 +00:00
Johannes Weiss e3290d891a make sure ByteToMessageHandler is happy never being in a pipeline (#1288)
Motivation:

ByteToMessageHandler was expecting to always be added to a
ChannelPipeline. That's mostly true but not always.

Modifications:

Make sure we don't crash if a ByteToMessageHandler was never added.

Result:

Fewer crashes.
2019-12-05 10:53:31 +00:00
Johannes Weiss e38f1b695a apply suggestions from #1117 (#1118)
Motivation:

@ravikandhadai suggested some minor changes in #1117 which we should
apply.

Modifications:

Apply all suggested changes all over the code base.

Result:

- fixes #1117
2019-08-20 09:24:58 +01:00
Will Lisac 26586835fd Fail promise in MessageToByteHandler write error cases (#1096)
* Fail promise in MessageToByteHandler write error cases

* Add test case for MessageToByteHandler failing write promises
2019-08-08 10:33:00 +01:00
Johannes WeissandCory Benfield b07efecaea fix double remove of handlers (#1091)
* fix double remove of handlers

Motivation:

Previously, it was possible that a handler was removed twice: Once by
Channel teardown and another time by a user-trigger removal that wasn't
instantaneous. This racy situation can happen in the real world. NIO
behaved wrongly in two ways:

1. we would call `handlerRemoved` twiced
2. ByteToMessageHandler would fail an assertion about the current
   removal state

Modifications:

- Only call `handlerRemoved` when the handler actually gets removed.
- Fix the assertion.

Result:

fewer bugs

* Update Sources/NIO/ChannelPipeline.swift

Co-Authored-By: Cory Benfield <lukasa@apple.com>
2019-07-31 16:18:15 +01:00
Cory Benfield 25bb2831ac Extend the ByteToMessageDecoder documentation. (#1019) 2019-05-28 10:21:43 +01:00
Johannes Weiss 7623b5e4f1 undo tiny public API breakage (#1008)
Motivation:

In #957 we accidentally broke the API just a little bit. We turned the
`ByteToMessageHandler.init` from

    public init(_ decoder: Decoder)

into

    public init(_ decoder: Decoder, maximumBufferSize: Int? = nil)

This looks totally benign and in almost all cases because of the default
argument, you wouldn't notice the API breakage. However, it's possible
to write code that works in NIO 2.0.0 but breaks in NIO 2.1.0. For
example

    let g: (Dummy) -> ByteToMessageHandler<Dummy> = ByteToMessageHandler<Dummy>.init
    let h: (Dummy) -> ByteToMessageHandler<Dummy> = ByteToMessageHandler<Dummy>.init(_:)

Modifications:

According to SemVer, this releases a patch that unbreaks the public API.

Result:

No API breakages (checked with `swift-api-digester` which also found the
breakage in the first place).
2019-05-17 17:20:43 +01:00
Johannes Weiss 0e37ada7cc B2MDError: nest PayloadTooLargeError (#998)
Motivation:

New public NIO types must be nested in existing ones or `NIO` prefixed.
This wasn't the case for ByteToMessageDecoderPayloadTooLargeError

Modifications:

nest PayloadTooLargeError into ByteToMessageDecoderError

Result:

we stick to our rules.
2019-05-09 15:29:52 +01:00
tomer doron e32a436466 optional max payload size for ByteToMessageDecoder (#957)
* optional max payload size for ByteToMessageDecoder

Motivation:

ByteToMessageDecoder aggregate data in memory as part of their normal operation. the ability to limit how much they aggregate is critical in many real-life applications

Modifications:

* add optional maximumBufferSize argument to ByteToMessageDecoder initializer
* test for buffer size when maximumBufferSize is set and throw ByteToMessageDecoderError.payloadTooLarge error

Result:

users can limit how much memory ByteToMessageDecoder takes and handle the exception on their end
2019-04-11 14:12:59 +01:00
Johannes Weiss fef050a83c B2MD: don't go to final state twice (#948)
Motivation:

B2MD (in debug mode) has some checking that we never move out of a final
state in the state machine. Both .done and .error are final states.
Previously however, the code set .done in a defer block which meant that
even if the decodeLoop throws (which should bring it to .error), it goes
via .done first. That's an error because .done is already a final state
so we shouldn't move to .error after that.

Modifications:

Only move to .done if there was no error. If there was an error, we
should go straight to .error, not via .done.

Result:

- more correct B2MD
- fewer crashes (in debug mode)
2019-04-05 14:44:40 +02:00
Johannes Weiss d55c582e5c improve documentation (#919)
Motivation:

Spotted a couple of issues with the documentation and fixed them.

Modifications:

- explicitly marked all `public class`es as `public final class` to get
  consistency in the Jazzy documentation.
- removed `public` extension methods of the `internal struct
  PriorityQueue` which Jazzy showed in the docs.
- improved the largely missing `EmbeddedChannel` and `EmbeddedEventLoop`
  documentation.
- clear up lies in the B2MD docs
- add some other missing docs

Result:

better docs makes happier users
2019-03-25 12:09:16 +02:00
Johannes Weiss 205f317fd9 B2MH: helpful message if re-added to pipeline (#903)
Motivation:

B2MH used to crash a nil variable if re-added to a pipeline.

Modifications:

provide good error message for the crash instead

Result:

- fixes #902
2019-03-14 14:05:54 +00:00
Johannes Weiss f1efb4a104 new http parser (#814)
Motivation:

The old HTTP parser tried to convert `http_parser` to a one-shot parser
by pausing it constantly. That was done to be resilient against
re-entrancy. But now, we have the new `ByteToMessageDecoder` which
protects against that so HTTP decoder can just become a real
`ByteToMessageDecoder`. It also serves as a great testbed for
`ByteToMessageDecoder`.

Modifications:

- make the HTTP decoder a `ByteToMessageDecoder`
- refactor the implementation

Result:

cleaner code, more use of `ByteToMessageDecoder`
2019-03-06 11:51:42 +00:00
Johannes Weiss 5fba8b7def B2MD: Stop parsing loop correctly on EOF (#875)
Motivation:

Introduced in #767 we would never interrupt a parsing loop unless the
decoder was removed, that's wrong and we need to interrupt also if we
seen `channelInactive` or inbound half closure.

Modifications:

interrupt the parsing loop on EOF.

Result:

more correct B2MD
2019-03-06 09:48:53 +00:00
Johannes Weiss eb524c72ad improve and simplify MessageToByteEncoder (#868)
Motivation:

MessageToByteEncoder was a protocol that similar to ByteToMessageDecoder
was pretty hard to hold correctly (override some methods but not
others). This PR brings MessageToByteEncoder more in line with the new
ByteToMessageDecoder.

Modifications:

- refactor `MessageToByteEncoder`
- introduce `MessageToByteHandler`, similar to `ByteToMessageHandler`

Result:

- better API, more similar to ByteToMessageDecoder
- fixes #864
2019-03-04 19:19:53 +00:00
Johannes Weiss 66b00ec637 B2MD: fast-path to reduce allocations (#854)
Motivation:

B2MD has an important fast path:
- everything was consumed
- no buffers collected during re-entrancy

In this case we should short-circuit and move on as quickly as possible.

Modifications:

If everything was consumed in `decode` and no buffers were collected in
re-entrancy, returning a buffer isn't actually useful, we should just
drop it and not risk taking an allocation for a useless `clear`.

Result:

faster & fewer allocs
2019-02-28 15:04:43 +00:00
Johannes Weiss d3862c0ecc B2MD: actually call decoderRemoved (#853)
Motivation:

decoderRemoved was never actually called...

Modifications:

call decoderRemoved on removal

Result:

more correct B2MD
2019-02-28 13:21:32 +00:00
Johannes Weiss a41280919e rename ctx to context (#842)
Motivation:

`ctx` was always an abbreviation was 'context` and in Swift we don't
really use abbreviations, so let's fix it.

Modifications:

- rename all instances of `ctx` to `context`

Result:

- fixes #483
2019-02-25 18:20:22 +00:00
Johannes Weiss 862533d089 B2MD: Don't deliver data after error (#837)
Motivation:

B2MD should never call any of the decode functions after the user has
thrown an error.

Modifications:

introduce an error state and make sure we don't call out if in error
state

Result:

B2MD more correct
2019-02-25 17:18:11 +00:00
Johannes Weiss b7f4e494f3 introduce WriteObservingByteToMessageDecoder (#834)
Motivation:

Some B2MDs need to observe writes.

Modifications:

Make those B2MD's B2MHs ChannelOutboundHandlers and let the B2MDs
observe the writes.

Result:

HTTPDecoder can be expressed as B2MD.
2019-02-25 14:25:31 +00:00
Johannes Weiss ff31b3135d improve B2MD EOF handling (#831)
Motivation:

Previously B2MDs didn't really have defined semantics regarding EOFs and
we didn't tell them if there was an EOF. Also `decodeLast` was optional
and all that was bad.

Modifications:

- require `decodeLast`
- add a `seenEOF: Bool` parameter to `decodeLast` which tells the
  decoder if an EOF has been seen

Result:

- clearer semantics
- more information
2019-02-21 14:14:50 +00:00
Johannes Weiss 1198931823 ByteBuffer: rename set(<type>:, ...) to set<Type>(...) (#812)
Motivation:

ByteBuffer methods like `set(string:)` never felt very Swift-like and
also didn't look the same as their counterparts like `getString(...)`.

Modifications:

- rename all `ByteBuffer.set/write(<type>:,...)` methods to
  `ByteBuffer.set/write<Type>(...)`
- polyfill the old spellings in `_NIO1APIShims`

Result:

code more Swift-like
2019-02-12 11:11:45 +00:00
Johannes Weiss d938264fcb better ChannelHandler removal API (#767)
Motivation:

If ChannelHandler removal worked correctly, it was often either by
accident or by intricate knowledge about the implementation of the
ChannelHandler that is to be removed. Especially when it comes to
re-entrancy it mostly didn't work correctly.

Modifications:

- introduce a `RemovableChannelHandler` API
- raise allocation limit per HTTP connection by 1
  (https://bugs.swift.org/browse/SR-9905)

Result:

Make things work by contruction rather than accident
2019-02-12 11:27:48 +01:00
Johannes Weiss 70c714a54d address Norman's B2MD feedback (#792)
Motivation:

Norman gave feedback on the new B2MD but after it was already merged.

Modifications:

address feedback

Result:

better code
2019-01-29 16:27:00 +00:00
Johannes Weiss 2ad1e21512 CircularBuffer.init: rename initialRingCapacity to initialCapacity
Motivation:

initialRingCapacity is not a great name, initialCapacity is much better

Modifications:

rename CircularBuffer(initialRingCapacity:) to initialCapacity

Result:

more consistent code
2019-01-29 12:29:04 +00:00
Johannes Weiss c4ff795dda remove ELP.succeed/fail's labels (#776)
Motivation:

EventLoopPromise.succeed/fail has extraneous labels that don't add
anything but noise.

Modifications:

remove extraneous labels

Result:

less noise in the code
2019-01-24 16:42:53 +00:00
Johannes Weiss cbc214938a first cut at B2MD v2 (#675)
Motivation:

Explain here the context, and why you're making that change.
What is the problem you're trying to solve.

Modifications:

Describe the modifications you've done.

Result:

After your change, what will change.
2018-12-10 16:23:12 +00:00
Norman Maurer 4cbafde292 Add better guards again re-entrance in ByteToMessageDecoder (#370)
Motivation:

When the ByteToMessageDecoder re-entranced in its decode(...) / decodeLast(...) methods it could be possible that the ordering of processing and so the seen bytes are mixed.

Modifications:

- Refresh the buffer (and take a slice)  on each loop iteration and update the cumulationBuffers indicies.

Result:

More robust code.
2018-05-08 18:29:55 +02:00
Matt Eaton 164b007e0e Fixed minor typos and updated force unwraps. (#273)
Motivation:

I wanted to make sure that I fixed the minor typos and that I addressed
the feedback that was provided to me in regards to force unwrapping.
I also wanted to make sure my error messages were accurate.

Modifications:

Fixed a few minor typos and updated the force unwrap error messages.

Result:

Fixed typos and guard statements with accurate error messages.
2018-04-04 12:57:31 +01:00
Matt Eaton 66e4fdfbba Fixed a few minor typos and some force unwraps (#259)
fix some typos, force unwraps and inconsistencies

Motivation:
Reviewing the code I found a few typos that hindered readability, some unnecessary force unwraps and inconsistencies in the codebase.

Modifications:

- The main file in NIOWebSocketServer now accepts command line arguments
and binds to a passed in host and port like the other server examples do.
- Minor typo fixes
- removed one force unwrap

Result:

- The result of my change will bring the NIOWebSocketServer more inline
with how the NIOHTTP1 and NIOChat servers examples are setup.
- less force unwrapping
- fewer typos

[message edited by @weissi]
2018-04-03 11:44:14 +01:00
Semen Zhydenko 73e89833fc Fixed typos in comments 2018-03-15 22:11:46 +09:00
adamnemecek 0bbed6d72f * removed trailing whitespace (#52) 2018-03-02 15:40:52 +09:00
Johannes Weiß 73a805c7f6 minor stylistic improvements (#36)
Motivation:

Sometimes we deviated from the style the Swift stdlib sets out for no
reason.

Modifications:

Fixed some stylistic deviations.

Result:

Looks more Swift-like.
2018-02-26 15:52:49 +00:00
Norman Maurer 8c2169663d Add docs for ByteToMessageDecoder and MessageToByteEncoder
Motivation:

We should provide docs for our provided codec extensions.

Modifications:

Add docs.

Result:

More clear for the user how to implement a custom codec.
2018-02-21 15:56:54 +01:00
Johannes Weiß 64eac6177c make inbound channel operations non-throwing
Motivation:

Except for blocking methods (which shouldn't be used on the event loop),
there aren't that many error cases that we should just send through the
pipeline automatically. Best proof for that is that nothing in the code
base did that. Therefore these methods shouldn't be throwing.

If the user does indeed want to catch and fire an error through the
pipeline that's still super simple `ctx.fireErrorCaught(error)`.

Modifications:

removed `throws` from the `ChannelInboundHandler` methods.

Result:

better
2018-02-19 18:10:21 +00:00
Cory Benfield 58e147f682 ByteToMessageDecoder should reclaim memory.
Motivation:

ByteToMessageDecoder implementations can potentially have usage
patterns that never reclaim memory. This is problematic when used
for a long time, as they can be holding on to large chunks of memory
they don't need.

Modifications:

Gave ByteToMessageDecoder a new protocol method,
shouldReclaimBytes(buffer:), and a default implementation.

Result:

ByteToMessageDecoders will not be able to hold on to more than 2kB
of unreachable memory under any circumstances, and will often hold
less memory than that.
2018-02-20 14:12:20 +00:00
Norman Maurer 273932ae36 Change ByteToMessageDecoder.decode and decodeLast to return an enum.
Motivation:

We used a bool to signal if we should continue decoding or not. Using an enum is more self-documenting.

Modifications:

Add DecodingState and use.

Result:

Code is more self-documenting.
2018-02-20 13:07:31 +01:00
Cory Benfield 6b999ad368 Remove quadratic buffering from ByteToMessageDecoder
Motivation:

Don't make the system fall over under slowloris-style attacks.

Modifications:

Remove the quadratic copy loop from ByteToMessageDecoder.

Result:

NIO goes from 🚂 to 🚄
2018-02-20 08:02:43 +00:00
Johannes Weiss eaa8ffaee4 remove redundant labels
Motivation:

Lots of our most important operations had redundant labels like

    func write(data: NIOAny)

the `data: ` label doesn't add anything meaningful and therefore it
should be removed.

Modifications:

removed lots of redundant labels

Result:

less redundant labels
2018-02-13 11:13:30 +00:00
Johannes Weiss eab7d314f3 make Channel non optional in ChannelHandlerContext 2018-01-30 12:03:51 +00:00
Johannes Weiss 9a5390495e improve channel pipeline
We now use two (doubly) linked lists (inbound & outbound) for the
ChannelPipeline. Previously we used two singly linked lists and an
array. That would give us the worst of the two worlds: Linear time
modifications and complicated code.
2018-01-05 18:17:34 +00:00
Cory Benfield 584fdb6507 ByteToMessageDecoder must propagate ChannelInactive 2017-12-12 10:49:12 +00:00
Johannes Weiß 33fe401322 rename Future/Promise to EventLoopFuture/EventLoopPromise 2017-11-21 17:41:36 +00:00
Max (Swift) Moiseev 198d6b447b Explicit access modifiers on members in extensions 2017-11-18 04:40:26 -08:00
Norman Maurer 9bb7f6727d Use correct types in default method
Motivation:

The default method we added for the MessageToByteEncoder has the incorrect parameter types and so is useless.

Modifications:

Fix type of parameter.

Result:

Default method match the protocol requirement.
2017-11-08 01:14:43 -08:00
Johannes Weiß 6173bee782 Split IOData into NIOAny & IOData (ByteBuffer | FileRegion) 2017-10-25 19:31:27 +01:00
Norman Maurer a9c9e0dc15 Http improve no copy
* Remove the need of the parseBuffer in the HTTPRequestDecoder and so remove a lot of memory copies.
2017-07-10 13:45:59 +02:00