54 Commits

Author SHA1 Message Date
Erik Dubbelboer 4891fc5304 Update golangci-lint to v2 (#1980) 2025-03-25 06:40:55 +01:00
newacorn 74864cbda9 Revert "Using atomic instead of mutex and delete scratch slice" (#1846)
* Remove a redundant field and clarify the comments.

* Revert "Using atomic instead of mutex and delete scratch slice (#1833)"

This reverts commit 19c50cdc44.
2024-09-10 18:48:26 +02:00
Erik Dubbelboer c3050516d9 Fix lint and security issues
gosec was failing after the last update introduced some new checks.
2024-09-07 15:22:05 +02:00
NikoMalik 19c50cdc44 Using atomic instead of mutex and delete scratch slice (#1833)
* using atomic instead of mutex and delete scratch slice

* optimize struct

* fix default bool

* escape data race

* avoid the momentary change of wp.workersCount.

* removed unused tail (for now)

* little fixes

* fixed allocations

This option immediately exits the loop when the maximum number of vorkers is reached, rather than creating a new vorker if the limit is reached. This reduces the frequency of unnecessary operations and potential locks in sync.Pool

* Update for linter workerpool.go

Co-authored-by: Erik Dubbelboer <erik@dubbelboer.com>

* Update for lint#2 workerpool.go

Co-authored-by: Erik Dubbelboer <erik@dubbelboer.com>

* Update for lint#3 workerpool.go

Co-authored-by: Erik Dubbelboer <erik@dubbelboer.com>

* Update for lint#4 workerpool.go

* eliminating potential data races

---------

Co-authored-by: Erik Dubbelboer <erik@dubbelboer.com>
2024-08-25 17:03:05 +02:00
Juan Calderon-Perez 1fb3453165 Use Named Fields and Align Structures to Reduce Memory Usage (#1814)
* Use Named fields and Align Structures to Reduce Memory Usage

* Remove extra spaces
2024-08-02 22:26:52 +02:00
Kashiwa 86c7e844f4 Optimize struct field order to reduce memory usage (#1809)
1. Reduce RequestHeader from 368 bytes to 360 bytes
2. Reduce Request from 816 bytes to 800 bytes
3. Reduce Response from 432 bytes to 416 bytes
4. Reduce Client from 312 bytes to 288 bytes
5. Reduce HostClient from 416 bytes to 392 bytes
6. Reduce PipelineClient from 176 bytes to 168 bytes
7. Reduce pipelineConnClient from 216 bytes to 208 bytes
8. Reduce Cookie from 232 bytes to 224 bytes
9. Reduce FS from 184 bytes to 160 bytes
10. Reduce fsHandler from 168 bytes to 160 bytes
11. Reduce ResponseHeader from 328 bytes to 320 bytes
12. Reduce headerScanner from 128 bytes to 120 bytes
13. Reduce TCPDialer from 104 bytes to 96 bytes
14. Reduce workerPool from 152 btyes to 144 btyes
2024-07-20 19:20:26 +02:00
Oleksandr Redko f196617f55 chore: Use 'any' instead of 'interface{}' (#1666)
gofmt -w -r "interface{} -> any" -l .
2023-11-24 11:33:04 +01:00
Oleksandr Redko d3397c64ed Enable wastedassign, whitespace linters; fix issues (#1665) 2023-11-24 11:32:11 +01:00
Moritz Poldrack d0f2727a4d get rid of some panics (#1526)
* client: simplify (*HostClient).do()

Remove an allocation in favour of deferring a call to release the
response.

* client: remove panic in dialAddr

Return an error instead of panicking if the user supplied a nonsensical
DialFunc.

* compression: remove panic on invalid compression level

If a compression level exceeding gzip's boundaries is provided, fasthttp
will panic. Instead it would be better to handle this error for them by
limiting it to the minimum or maximum value, depending on the direction
the user has exceeded the limits.

Clamp the value of gzip to always be between gzip.BestSpeed and
gzip.BestCompression.

* peripconn: remove panic on negative connection count

When a negative count is reached when unregistering a connection, a
panic is caused even though data-integrity is not at risk.

Replace the panic() with a simple clamp on the value to ensure the
value does not exceed it's expected lower bounds.

References: #1504

* compress: remove error on failed nonblocking writes

Since there is no way of handling or even logging non-critical errors in
stateless non-blocking writecalls, just drop them and hope the user
notices and tries again.

* workerPool: remove panic on redundant Start and Stop calls

Instead of panicking for invalid behaviour, it's preferable to just turn
the function into a noop.

* http: remove panic on invalid form boundary

* http: remove panic on negative reads

Since bufio already panics on negative reads, it is not necessary to do
so as well. If the length is zero and for some reason no error is
returned, readBodyIdentity and appendBodyFixedSize now errors in these
cases.

Link: https://github.com/golang/go/blob/851f6fd61425c810959c7ab51e6dc86f8a63c970/src/bufio/bufio.go#L246

* fs: remove panic on negative reader count

When a negative count is reached when unregistering a reader, a panic is
thrown even though data-integrity is not at risk.

Replace the panic() with a simple clamp on the value to ensure the
value does not exceed it's expected lower bounds.

* server: remove panic in favour of a segfault

Panicking with "BUG: " obscures the error. As the segfault causes a
panic anyway, just let the chaos unfold.

* server: remove panic in favour of returning an error

Writing on a timed-out response is not endangering data integrity and
just fails.

* chore: add comments to all panics

* chore: fix minor typo
2023-03-30 03:38:28 +02:00
Erik Dubbelboer 7a5afddf5b Use %v for errors and %q for strings (#1262)
Mostly in tests.
2022-04-01 18:11:16 +02:00
Erik Dubbelboer f0b0cfe9b2 Don't log ErrBadTrailer by default 2022-02-23 17:12:07 +01:00
Andy Pan 9f11af2968 Make several optimizations to worker pool (#680)
* Use binary-search algorithm to speed up cleaning up workers

* Speed it up when iterating the slice of workerChan

* Use sync.Pool as a more canonical way

* Add benchmark test between binary-search and linear search

* Optimize range to the slice of workerChan, avoiding elements copy

* Perfect the benchmark of work pool

* Make binary-search code inline and remove benchmark test code
2019-10-28 02:27:31 +08:00
Kevin Burns f82a6460e9 Requests with incomplete bodies no longer cause log noise (#682)
* #660 Incorrect content length

* fix

* unexpected EOF is expected

* Prevent test from panicing should err ever be nil
2019-10-27 15:29:20 +08:00
Erik Dubbelboer 8dfc881b9e Added Server.nextProtos
This implementation allows user to handle crypto/tls.Config.NextProtos to use their own handlers for the negotiated TLS protos like HTTP/2.
Workerpool where changed to adapt WorkerFunc to another conns server with ServeConn type and contains the Server structure which creates to check configured protos.
2018-10-05 16:35:59 +08:00
Xu Qiaolun c88d0992b4 vendor ConnState type in fasthttp package 2018-08-30 15:20:32 +00:00
徐乔伦 69613e6feb add support for http.ConnState
issue #205
2018-08-30 15:20:32 +00:00
Erik Dubbelboer fbf4b2d9cd Do not log ErrSmallBuffer for request headers
fasthttp returns a 431 error when the request headers are too large.
Most other HTTP servers do not log an error when the request header
exceeds the limit. When serving HTTP requests directly to browsers,
it is normal to occasionally hit the limit due to broken browsers
or broken sites linking to your resource. These issues are generally
outside of your control in the same way the already ignored errors are.
2018-08-27 21:30:11 +00:00
Kirill Danshin ef10ed05a3 deprecate CoarseTime and replace it with a shortcut implementation
Signed-off-by: Kirill Danshin <k@guava.by>
2018-08-17 18:12:21 +03:00
Kirill Danshin 4c7269ec2a Revert "Remove CoarseTime"
This reverts commit f2ddaffc31.
2018-08-17 15:32:15 +03:00
Erik Dubbelboer f2ddaffc31 Remove CoarseTime
It is not clear why @valyala introduced this coarse time. Benchmarks on
different systems show that the speedup is no where enough to justify
the added code complexity and bugs it seems to have introduced.

Mac:
BenchmarkCoarseTimeNow-8   	2000000000	         2.49 ns/op
0 B/op	       0 allocs/op
BenchmarkTimeNow-8         	500000000	         3.14 ns/op
0 B/op	       0 allocs/op

Ubuntu:
BenchmarkCoarseTimeNow-4   	300000000	         6.74 ns/op
0 B/op	       0 allocs/op
BenchmarkTimeNow-4         	100000000	        15.9 ns/op
0 B/op	       0 allocs/op

This reverts commit https://github.com/erikdubbelboer/fasthttp/commit/6309f42188ecb28ccf8ac58442739cdb43d75d9e
and https://github.com/erikdubbelboer/fasthttp/commit/32c72cde80f0c591604f825586d6a4bbbb39d9c5.

See: https://github.com/valyala/fasthttp/issues/271,
https://github.com/valyala/fasthttp/pull/269 and
https://github.com/valyala/fasthttp/issues/261.
2018-08-17 17:15:41 +08:00
Aliaksandr Valialkin 32c72cde80 Export CoarseTimeNow and clarify that RequestCtx.Time() and RequestCtx.ConnTime() return time truncated to a second 2017-02-10 15:45:14 +02:00
Aliaksandr Valialkin 6309f42188 optimization: substitute time.Now() calls with coarse-grained time in hot paths 2017-02-09 23:59:06 +02:00
Aliaksandr Valialkin a2cc660e59 optimization: removed slow defer from worker pool handler and hijack handler 2016-07-18 17:23:38 +03:00
Aliaksandr Valialkin 886e541160 Removed 'recover-from-panic' band-aids.
All the panics must be handled by the user code.
2016-07-12 12:30:38 +03:00
Aliaksandr Valialkin a0377f758e workerpool: test cleaner 2016-03-31 14:56:39 +03:00
Aliaksandr Valialkin 9f20a62336 added workerpool tests 2016-03-31 14:41:27 +03:00
Aliaksandr Valialkin 4fa2d5db39 workerpool: bugfix: do not return workerChan to ready pool on panic 2016-03-31 12:37:46 +03:00
Aliaksandr Valialkin e64702d6f0 workerpool: removed a hack for recycling worker stacks, since Go runtime must be able to shrink big stacks when they aren't needed 2016-03-31 10:31:53 +03:00
Aliaksandr Valialkin 72352d7661 workerpool: limit the number of connections each worker may serve. This should reduce the total amount of memory occupied by worker stacks in the long term 2016-03-30 19:35:46 +03:00
Aliaksandr Valialkin 1f148c6c09 workerpool: properly clean scratch buffer for workerChans 2016-03-30 19:04:20 +03:00
Aliaksandr Valialkin 1d6b6a6608 workerpool: properly count workers 2016-03-30 18:45:57 +03:00
Aliaksandr Valialkin 92c779039c workerpool: clean workerChan slices in order to reduce the amount of work for GC 2016-03-30 18:11:40 +03:00
Aliaksandr Valialkin 8094a6e20f workerpool: reduced the time to hold the lock when cleaning unused worker goroutines 2016-03-30 16:40:16 +03:00
Aliaksandr Valialkin dbea792cf7 Scalability improvement: use distinct workerChan pool per each workerPool 2016-03-29 11:45:09 +03:00
Aliaksandr Valialkin 023e3807b8 workerPool optimization: determine the current time outside lock 2016-03-29 11:05:14 +03:00
Aliaksandr Valialkin e4c0cbe843 code prettifying 2016-03-02 16:10:38 +02:00
Aliaksandr Valialkin ed7ca4c631 Added LogAllErrors config parameter to Server, which allows logging the most frequent errors such as 'connection reset by peer', 'broken pipe' and 'i/o timeout'. By default such errors are suppressed 2016-02-19 12:01:27 +02:00
Aliaksandr Valialkin a9a9b08a84 Do not pollute server error log with 'i/o timeout' errors 2016-01-28 12:03:07 +02:00
Aliaksandr Valialkin 180e104034 Fixed misleading comments in workerpool 2015-12-10 12:05:36 +02:00
Aliaksandr Valialkin 3c903772f2 Avoid memory allocation when cleaning stale workers and connections 2015-12-02 08:24:55 +02:00
Aliaksandr Valialkin 235d4932da Check for stale workers and connections every 10 seconds instead of every second. This should reduce cleaners' overhead 2015-12-01 13:42:53 +02:00
Aliaksandr Valialkin a3965934a1 Simplify GC life by zeroing references to closed worker chans and client conns 2015-12-01 13:32:05 +02:00
Aliaksandr Valialkin f214dc327f Scalability improvement: do not move read workers' queue when cleaning old workers. This should reduce latencies when server serves multi-million concurrent connections 2015-11-30 11:26:39 +02:00
Aliaksandr Valialkin bc85e2b572 workerpool: immediately switch to connection processing if GOMAXPROCS=1. This improves single-threaded server performance by 1-2% 2015-11-26 12:38:18 +02:00
Aliaksandr Valialkin 45e1d5a313 fixed a typo 2015-11-24 13:59:51 +02:00
Aliaksandr Valialkin cd4ace5409 Close connection and release worker channel on panic in WorkerFunc 2015-11-24 13:40:39 +02:00
Aliaksandr Valialkin 146145240d Added RequestCtx.Hijack() for connections' hijacking 2015-11-23 19:41:43 +02:00
Aliaksandr Valialkin bb38369834 Suppress too chatty 'reset by peer' errors when serving clients, which unexpectedly close connections 2015-11-23 13:01:36 +02:00
Aliaksandr Valialkin 76f453b7fc Suppress too chatty 'broken pipe' errors when serving clients, which unexpectedly close connections 2015-11-23 12:57:37 +02:00
Aliaksandr Valialkin 6e4b32182e Fixed panic when WorkerFunc returns error and Conn is closed 2015-11-12 14:01:04 +02:00