Commit Graph

253 Commits

Author SHA1 Message Date
Eugene Kabanov 6525f4ce1d
Fix some warnings. (#310)
* Fix some warnings messages.

* More warning fixes.

* Address review comments.
Fix MacOS issues.

* More warning fixes.

* More Windows specific fixes.

* Fix macos and windows warnings.

* Fix warnings in timer.nim
Refactor to remove `result`.
Improve performance and behavior of timer to string procedure.
Add tests.
2022-11-02 08:09:15 +01:00
Tanguy 24146463a3
Token bucket (#279)
- A single sleepAsync per bucket
- Manual replenish with async
- Cancellation of consume

Co-authored-by: Jacek Sieka <jacek@status.im>
2022-11-02 08:03:19 +01:00
Eugene Kabanov 266e2c0ed2
HTTP client: Allow request connection management. (#323)
* Allow per-request connection management.
Fix NewConnectionAlways leak issue.

* Address review comment.
2022-10-13 10:18:13 +00:00
Eugene Kabanov be2352027e
Fix nested waitFor (IndexError defect crash) bug. (#309)
Add IndexError crash test for Linux/MacOS.
Add Sentinel version of fix.
Add GetQueuedCompletionStatusEx() support for Windows, which allows to capture more then one event in single `poll()` call.
2022-09-16 22:34:18 +02:00
Eugene Kabanov 15d7e0ebb7
Add optimized implementation for preferredContentType() which avoid sorting and double iteration. (#299)
Fix tests to use 80cpl.
2022-08-06 13:53:40 +03:00
Eugene Kabanov 939195626f
Use new Content-Type header value parser. (#302) 2022-08-05 19:59:26 +03:00
Eugene Kabanov 79c51914ae
Fix jigsaw test. (#301) 2022-07-30 23:16:30 +03:00
Tanguy 41b82cdea3
Add test for nested finally transform issue (#295) 2022-07-18 23:30:27 +02:00
Eugene Kabanov 377e197417
waitSignal() helper and tests for it. (#289)
* Add waitForSignal() implementation and tests.

* Fix compilation issues.

* Fix mistype.

* Rename to waitSignal().

* Fix Windows compilation issue.

* Re-export posix signals.

* Remove signal handler on continuation too.
2022-06-29 00:53:09 +03:00
Zahary Karadjov 84e32a3b69
Add high and low operators for Duration and Moment; Add Moment.epochSeconds and Moment.epochNanoSeconds 2022-06-28 16:47:59 +03:00
Eugene Kabanov f403b06de6
AsyncEventQueue (AsyncEventBus replacement). (#275)
* AsyncEventQueue (AsyncEventBus replacement) initial commit.

* Add closeWait() and remove assertion test.

* Fix "possible" memory leak.

* Add limits to AsyncEventQueue[T].
Add tests for AsyncEventQueue[T] limits.
Rebase AsyncSync errors to be children of AsyncError.

* Adopt AsyncEventQueue to garbage collect events on emit() too.
Add test from review comment.

* Remove AsyncEventBus test suite.

* Remove unneeded [T] usage.

* Optimize memory usage in 1m test.

* Lower number of events in test from 1m to 100k.

* Deprecate AsyncEventBus.
Add some GC debugging for tests.

* Fix mistype.

* One more attempt to fix crash.

* Add some echo debugging.

* More echo debugging.

* More closer debug echoes.

* Attempt to workaround crash place.

* More debugging echoes in exception handlers.

* Convert suspected test into async procedure.

* Make multiple consumers test async.

* Remove GC debugging.

* Disable added tests.

* Disable AsyncEventQueue tests to confirm that this is an issue.

* Enable all the tests.
2022-06-16 00:51:21 +03:00
Eugene Kabanov 623681e212
Add nimRawSetjmp define to config.nims (#285)
* Add nimRawSetjmp define to config.nims

* Add config to tests too.
2022-06-14 01:02:33 +03:00
Tanguy c5894bae1b
AsyncEventBus: fix multiple listeners (#271)
Co-authored-by: dbrignoli <dbrignoli@audioscience.com>
2022-05-20 11:57:37 +02:00
Tanguy ae19d5b6f0
Handle multiple childs for opensymchoice (#266)
And more tests. Follow up of #261
2022-05-05 11:05:04 +02:00
Jacek Sieka bb4c3298f5
fix crash after reading from fd > 1024 (#267)
The socket selector holds a `seq` of per-descriptor data. When a reader
is registered, a pointer to a seq item is stored - when the `seq` grows,
this pointer becomes dangling and causes crashes like
https://github.com/status-im/nimbus-eth2/issues/3521.

It turns out that there already exist two mechanisms for passing user
data around - this PR simply removes one of them, saving on memory usage
and removing the need to store pointers to the `seq` data that become
dangling on resize.
2022-04-11 11:56:30 +02:00
Tanguy 3621143c89
reenable auth test with other server 2022-03-30 17:09:43 +02:00
Tanguy 0bd9238409
Skip failing test to restore CI 2022-03-30 16:56:04 +02:00
Tanguy fc65ff8c5b
Cleanup return type open syms (#261) 2022-03-30 16:13:58 +03:00
Emil 1f37dac810 Refactor `preferredContentType` proc & add tests
Fix of the following bug:
In case of multiple accept headers with same preference
`preferredContentType` used to select the first match within content types
provided by the application. For example, if user specifies accept headers
`application/octet-stream, application/json` and application provides
`application/json, application/octet-stream`, `application/octet-stream`
will be returned, and that is a bug.

Now the procedure returns the most suitable match according
to the application preferences.
2022-01-25 16:05:36 +02:00
Eugene Kabanov 519ca463df
Fix transport.write() unable to send data through OS pipes. (#256)
* Fix transport.write() unable to send data through pipe.
Add test for pipes.

* Fix flaky test.

* Add workaround for Nim's issue #19425.
2022-01-20 18:38:41 +02:00
Jacek Sieka 0fc82049ac enable --styleCheck:usages 2021-12-27 17:33:25 +02:00
Jacek Sieka c25fa1f6cd
posix: fast path for write (#244)
When `write` is called on a `StreamTransport`, the current sequence of
operations is:

* copy data to queue
* register for "write" event notification
* return unfinished future to `write` caller
* wait for "write" notification (in `poll`)
* perform one `send`
* wait for notification again if there's more data to write
* complete the future

In this PR, we introduce a fast path for writing:

* If the queue is empty, try to send as much data as possible
  * If all data is sent, return completed future without `poll` round
  * If there's more data to send than can be sent in one go, add the
rest to queue
* If the queue is not empty, enqueue as above
* When notified that write is possible, keep writing until OS buffer is
full before waiting for event again

The fast path provides significant performance benefits when there are
many small writes, such as when sending gossip to many peers, by
avoiding the poll loop and data copy on each send.

Also fixes an issue where the socket would not be removed from the
writer set if there were pending writes on close.
2021-12-08 11:35:27 +01:00
cheatfate 661eae5732
Fix identifiers to be `uint` instead of `uint64`. 2021-10-21 17:46:06 +03:00
Eugene Kabanov 3bc0bc36f7
Change Future identifier type from `int` to `uint`. (#228)
* Change Future identifier type from `int` to `uint64`.

* Fix compilation error and tests.

* Add integer overflow test, to avoid confusion with next Nim versions, we expect that unsigned integers do not raise any exceptions if overflow happens.

* Switch from `uint64` to `uint` type.

* Add `uint` overflow tests.
2021-10-21 17:22:11 +03:00
Eugene Kabanov 80102a3b6a
Eventbus implementation (#214)
* Initial commit.

* Move AsyncEventBus implementation to asyncsync.nim
Add subscriptAll(), waitAllEvents() primitives.
Add emitWait() primitive.
Add emitter source location implementation.
Add tests.
2021-09-15 16:55:15 +03:00
Eugene Kabanov bbbcb55493
Server-side events implementation. (#222)
Fix keep-alive issue.
Refactor some prepareXXX routines.
2021-09-14 20:32:58 +03:00
Eugene Kabanov 5034f0a5a6
Fix issue #219 (#220)
* Address issue #219 and add tests for it.
Some cosmetic refactoring.

* Fix *nix tests.
2021-09-05 00:53:27 +03:00
Eugene Kabanov ef2430d08d
Add `Accept` header handling to httpserver.nim. (#211)
* Add `Accept` header handling to httpserver.nim.
Add simple test suite.
Bump version to 3.0.6.

* Fix compilation error.
2021-07-28 17:08:38 +03:00
Eugene Kabanov 15137f71c3
Basic authorization implementation for HTTP client. (#204)
* Basic authorization implementation for HTTP client.
Add tests for basic authorization.

* Bump chronos version to 3.0.5.
2021-06-29 02:38:08 +03:00
Jacek Sieka 252e5d0d50
simplify future tests (#196) 2021-06-04 12:28:04 +02:00
Eugene Kabanov 7ccb170f7a
Enable comma as array delimiter and adding tests. (#191)
* Enable comma as array delimiter and adding tests.

* Bump version to 3.0.4.
2021-05-17 22:39:24 +03:00
Eugene Kabanov be184a815c
Httpclient (#182)
* Initial commit.

* Some refactoring.

* Allow boundstream to accept uint64.
Fix httpserver and asyncstream tests to follow new uint64 requirement.

* send() and getBodyBytes() implementations.

* Add closeWait for response and request.
Refactor finish/close flow.

* Changes in state machine
Add first test.

* Missing test file.

* Fixed tests
Add http leaking trackers and tests.

* Some fixes in multipart.
Fix automatic Content-Length header for requests with body.
Fix getBodyBytes() assertions.
Merging tests to main suite.

* Post rebase fixes.

* Fix tests big message generation.

* Fix response state management and leaks for getBodyXXX() procedures.

* Add redirection support to client and server.
Add fetch(url) procedure with redirection support.
Add tests for redirection.
2021-05-10 10:26:36 +03:00
Eugene Kabanov d270dba8a3
Fix unused warnings, result, asyncCheck and 80 cpl (#185)
* Fix sources to follow 80 characters per line.
Fix unused compilation warnings.
Refactor (remove result) handles.nim.
Fix tests to use asyncSpawn instead of asyncCheck.

* Fix handles for Unix platforms.
2021-05-07 23:52:24 +03:00
Eugene Kabanov fed6b0ac92
Restore functionality of zero-sized bounded reader/writer streams. (#184)
* Restore functionality of zero-sized bounded reader/writer streams.
Adding tests for it.

* run build_nim.sh unconditionally

Co-authored-by: Ștefan Talpalaru <stefantalpalaru@yahoo.com>
2021-05-07 18:52:44 +03:00
Eugene Kabanov 39f4060e07
Refactor and optimization of BoundedStream. (#180)
Documentation for BoundedStream.
Fix HttpServer bounding.
2021-04-26 14:05:37 +03:00
Eugene Kabanov 833d968782
Fix upload() issue and adding tests. (#179) 2021-04-24 20:32:21 +03:00
Eugene Kabanov e6ba00cb81
Fix reading could stuck on very big chunked headers and tests. (#178) 2021-04-22 15:32:28 +03:00
Eugene Kabanov aab1e30a72
Refactor common.nim and add more resolve procedures. (#177)
* Refactor common.nim to remove `result` usage.
Fix comparison of TransportAddress issue.
Add resolveTAddress procedures for both IPv4 and IPv6 addresses.
Fix tests.

* Bump version to 3.0.2.
2021-04-10 00:39:54 +03:00
Jacek Sieka 4abd7a5645
exception tracking (#166)
* exception tracking

This PR adds minimal exception tracking to chronos, moving the goalpost
one step further.

In particular, it becomes invalid to raise exceptions from `callSoon`
callbacks: this is critical for writing correct error handling because
there's no reasonable way that a user of chronos can possibly _reason_
about exceptions coming out of there: the event loop will be in an
indeterminite state when the loop is executing an _random_ callback.

As expected, there are several issues in the error handling of chronos:
in particular, it will end up in an inconsistent internal state whenever
the selector loop operations fail, because the internal state update
functions are not written in an exception-safe way. This PR turns this
into a Defect, which probably is not the optimal way of handling things
- expect more work to be done here.

Some API have no way of reporting back errors to callers - for example,
when something fails in the accept loop, there's not much it can do, and
no way to report it back to the user of the API - this has been fixed
with the new accept flow - the old one should be deprecated.

Finally, there is information loss in the API: in composite operations
like `poll` and `waitFor` there's no way to differentiate internal
errors from user-level errors originating from callbacks.

* store `CatchableError` in future
* annotate proc's with correct raises information
* `selectors2` to avoid non-CatchableError IOSelectorsException
* `$` should never raise
* remove unnecessary gcsafe annotations
* fix exceptions leaking out of timer waits
* fix some imports
* functions must signal raising the union of all exceptions across all
platforms to enable cross-platform code
* switch to unittest2
* add `selectors2` which supercedes the std library version and fixes
several exception handling issues in there

* fixes

* docs, platform-independent eh specifiers for some functions

* add feature flag for strict exception mode

also bump version to 3.0.0 - _most_ existing code should be compatible
with this version of exception handling but some things might need
fixing - callbacks, existing raises specifications etc.

* fix AsyncCheck for non-void T
2021-03-24 10:08:33 +01:00
Eugene Kabanov c8eefb9382
Split HTTPS and HTTP servers. (#165)
* Split HTTPS and HTTP servers.

* Fix review commens
2021-03-17 15:40:40 +02:00
Eugene Kabanov f774644129
Fix integer decoding overflow issue. (#163)
Switch to stew.base10 procedures.
Adjust tests to follow new behavior.
Bump version to 2.6.1.
2021-03-06 00:22:32 +02:00
cheatfate d6a39e46e1
Fix tests. 2021-02-21 17:34:55 +02:00
Eugene Kabanov d49e0a9c47
Add drop() implementation (was missed) to httpserver and fix transport leak. (#158)
* Add drop() implementation (was missed).
Add tests for drop().
Fix transport leak because of drop().

* Fix future leak.
2021-02-19 14:07:20 +02:00
cheatfate 25688cd0aa Add hexValue tests. 2021-02-18 22:16:04 +02:00
cheatfate eb81018d02 Address review comments and fix issues found.
Adding more tests.
2021-02-18 22:16:04 +02:00
cheatfate fc0d1bcb43 Address review comments. 2021-02-18 22:16:04 +02:00
cheatfate 3d74c5cdd2 Add tests for getContentEncoding() and getTransferEncoding(). 2021-02-18 22:16:04 +02:00
cheatfate 4406ab7072 Add positive decimal integer parsing tests. 2021-02-18 22:16:04 +02:00
cheatfate 3495122867 Fix getMultipartBoundary() issues and add tests for it. 2021-02-18 22:16:04 +02:00
cheatfate 9b48496513 Fix compilation error with nimcrypto/utils import. 2021-02-18 22:16:04 +02:00
cheatfate 3569e4d553 testutils must be last executed test. 2021-02-18 22:16:04 +02:00
cheatfate 3e9ffae407 Properly fix case when request body size exceeds maximum allowed size. 2021-02-18 22:16:04 +02:00
cheatfate 970e5641d7 Add less strict rules for BoundStream reader/writer. 2021-02-18 22:16:04 +02:00
cheatfate d43a9cb92d HttpServer now supports TLS.
Some TLSStream fixes to properly support EOF.
Some HttpServer to properly support TLS handshake problems.
HttpServer test suite for HTTPS.
2021-02-18 22:16:04 +02:00
cheatfate 1a3e9162a4 Fix multipart end of message handling.
Add apps.nim.
Change copyrights dates.
Add httpserver tests to test suite.
2021-02-18 22:16:04 +02:00
cheatfate 0b396c34d8 Add newline. 2021-02-18 22:16:04 +02:00
cheatfate 2defc4b822 Add sequence of bytes as boundary to BoundStream and tests. 2021-02-18 22:16:04 +02:00
cheatfate 13eddf382d Simplification and fixes for TLSStream state machine. 2021-02-18 22:16:04 +02:00
cheatfate e8d2a3ca0a Attempt #5. 2021-02-18 22:16:04 +02:00
cheatfate 49fd70f504 Attempt #4. 2021-02-18 22:16:04 +02:00
cheatfate bb176ba574 Investigation of Linux freezes #1. 2021-02-18 22:16:04 +02:00
cheatfate ac8b11d6ca close() procedure should not raise, otherwise its impossible to cleanup. 2021-02-18 22:16:04 +02:00
cheatfate 0cb6840f03 Big refactoring of AsyncStreams.
1. Implement all read() primitives using readLoop() like it was done in streams.
2. Fix readLine() bug.
3. Add readMessage() primitive.
4. Fixing exception hierarchy, handling code and simplification of (break/continue + exception).
5. Fix TLSStream closure procedure.
6. Add BoundedStream stream and tests.
7. Remove `result` usage from the code.
2021-02-18 22:16:04 +02:00
cheatfate 46c0bf3c5a
Fix define to skip flaky test on MacOS. 2020-11-27 03:50:38 +02:00
Eugene Kabanov bca5559c6a
Race() call (#142)
* Add `race` procedure call which extends `one` with FutureBase.

* Fix race() and add test procedures.
2020-11-27 00:50:55 +02:00
Eugene Kabanov ac9b3e304f
Fix deadlock for pending write() calls on transport close. (#139)
Add tests for read() and write() deadlocks.
2020-11-18 11:30:33 +02:00
cheatfate 493cb1dbfd Fix behavior of wait() and withTimeout() calls to cancel and wait for result of cancelled Future[T].
Add tests.
2020-11-17 19:28:52 +02:00
Eugene Kabanov b5915ecd29
Small fixes (#134)
* Fix cancellation behavior.
Fix some compilation warnings.

* Fix cancelAndWait() and add proper documentation.

* Add completed(Future) and done(Future) calls to check if Future[T] completed without an error.

* Add stepsAsync() and tests.

* Fix comments.

* Fix new primitive comment, to avoid usage for task switches.
2020-11-13 14:22:58 +02:00
Eugene Kabanov d3018ae908
Fix TLSStream SSL errors while in handshake could stuck connection. (#133)
Fix expired SSL certificate in tests.
2020-10-13 02:12:52 +03:00
Eugene Kabanov 2134980744
Fix AsyncLock.locked flag to be consistent. (#129)
* Fix `locked` flag to be more consistent.
Refactor AsyncLock to not use `result`.
Add test for `locked` flag.

* Fixes.

* Fix imports.

* Fix multiple release() without scheduler.
Add more tests.

* Fix review comments.
2020-09-10 23:28:20 +03:00
Jacek Sieka 483054cda6
small fixes (#127)
* small fixes

* more efficient codegen for nil check (much less code)
* release futures earlier in AsyncEvent
* release finished future earlier in AsyncQueue
* avoid searches for futures (deque variant unused / broken)
* avoid catching defects

* Fix AsyncEvent test, because of optimization.

* delete fix

* avoid seq allocs

* Keep style consistent with other code.
Refactor AsyncEvent and AsyncQueue to not use `result` keyword.

Co-authored-by: cheatfate <eugene.kabanov@status.im>
2020-09-10 11:39:10 +03:00
Eugene Kabanov 126ea4bc56
Fix newFuture[T] location source for generated async procedures. (#125)
Add tests to for locations.
2020-09-03 14:37:53 +03:00
Eugene Kabanov a5442edfc0
Add asyncSpawn() procedure and tests. (#123)
Deprecated asyncDiscard() procedure.
Bump version to 2.5.2.
2020-09-01 21:41:18 +03:00
Zahary Karadjov 03f4a26829 Revert "yieldAsync() (#120)"
This reverts commit 284d677815.

There is no rough consensus for how this should be implemented and
whether it's needed indeed - a use case that cannot be solved with
a queue or a lock/event should be identified before pursuing this
functionality.
2020-08-16 01:48:25 +03:00
Ștefan Talpalaru 284d677815
yieldAsync() (#120) 2020-08-10 15:31:21 +02:00
cheatfate 3968f09ae1
Skip flaky test on MacOS. 2020-07-17 09:38:58 +03:00
Jacek Sieka f856c885fa
fix endian conversion issues (#82)
* fixes call to `bigEndian32` on a uint64 which breaks on big endian
platforms
* prefer endians2 for less and safer code
2020-07-12 18:22:47 +02:00
Eugene Kabanov ce6e7d17b1
Make Future tracking and stack traces optional (#108)
* Make Future tracking optional via -d:chronosDutureTracking compilation flag.
* Stack traces is now optional, use -d:chronosStackTraces.
* Fix mistypes and add test for chronosStackTrace option.
2020-07-08 19:48:01 +03:00
Eugene Kabanov 5629b3c41f
[WIP] Zero-cost unattended Future[T] tracking mechanism. (#106)
* Zero-cost unattended Future[T] tracking mechanism with tests and tracking of test suite.
2020-07-06 09:33:13 +03:00
Eugene Kabanov 16ed169f25
Fix cancellation race when low level futures are already completed, while cancellation process is pending. (#107)
Added test.
2020-07-03 15:03:59 +03:00
Eugene Kabanov 319e2bfc09
Fix Nim's issue #13899 using #14723 and add tests. (#104) 2020-06-24 13:03:36 +03:00
Eugene Kabanov 02b8da986b
Add accept() call (#103)
* Add accept() call and tests.
* Fix rare fd leaks on Windows.
* Fix compilation warnings.
* Add fd leak test.
* Bump version to 2.4.0.
2020-06-24 11:21:52 +03:00
Eugene Kabanov 2ecc5500c2
Undeprecate `or` operation. (#93)
* Undeprecate `or` operation.
Fix `or` for already finished futures.
Add tests.

* Bump version to 2.3.9.
2020-04-21 07:07:49 +03:00
Eugene Kabanov 3d745a4b0c
Fix Nim's issue #13889 https://github.com/nim-lang/Nim/issues/13889. (#90) 2020-04-06 15:49:09 +03:00
Eugene Kabanov 4e2810cfe0
Fix issue with allFinished(), allFutures(), one() behavior when Futures passed are already finished. (#89)
Added test.
2020-04-06 13:56:24 +03:00
Araq f3827a13d1 prepare nim-chronos for Nim version 1.2 2020-03-23 20:17:29 +02:00
Jacek Sieka d8f8e3d9fc
readMsg branch by @arnetheduck with some changes. (#83)
Co-authored-by: Eugene Kabanov <ka@hardcore.kiev.ua>
2020-03-05 10:59:10 +01:00
cheatfate 64583b4269
Fix Windows datagram's AnyAddress issue.
Add test for datagram's AnyAddress.
Bump version to 2.3.6.
2020-02-25 23:50:39 +02:00
cheatfate 80351cb928
Fix #73. 2020-02-12 22:54:05 +02:00
cheatfate e34857364e
Attempt to fix #64. 2020-01-28 12:47:38 +02:00
cheatfate 74700fdcab Fix bug cancellation handlers not called in wait() and withTimeout().
Fix double completion bug because of callback race.
Fix deprecation warnings.
Rename some internal procedures.
Bump version to 2.3.5.
2020-01-27 22:32:08 +02:00
cheatfate 943a961201
Enable --threads:on testing. 2019-11-01 06:42:32 +02:00
cheatfate fb00b20cfa
Fix all compilation warnings for 1.0.2.
Fix all related problems.
2019-10-29 23:19:41 +02:00
cheatfate 5f1391f39f
Fix unused imports. 2019-10-24 16:21:31 +03:00
cheatfate 29d9274e03
Fix some compilation warnings. 2019-10-24 16:01:57 +03:00
Yuriy Glukhov a291f26c82
Merge pull request #55 from status-im/tlsstream
TLS async stream.
2019-10-22 13:32:36 +03:00
cheatfate d008fa2087
Fix make serverName mandatory and check for empty serverName. 2019-10-16 09:07:46 +03:00
cheatfate a92ad6d2d2
Add TLS inbound stream.
Fix some review comments.
2019-10-16 09:01:52 +03:00
cheatfate 3b8874a9e8
Fix issue with Windows connect(0.0.0.0). 2019-10-09 15:12:19 +03:00
cheatfate c27624cfc0
Add TlsStream with client-only connections. 2019-10-08 18:46:27 +03:00