Commit Graph

28 Commits

Author SHA1 Message Date
Oleksandr Redko f196617f55 chore: Use 'any' instead of 'interface{}' (#1666)
gofmt -w -r "interface{} -> any" -l .
2023-11-24 11:33:04 +01:00
Gusted 1834cecd7e Lazy load stackless functions (#1656)
- I noticed that fasthttp was taking up 1.8MB of heap memory, even
though it wasn't being used. This turned out to be the stackless
function: 1.80MB  github.com/valyala/fasthttp/stackless.NewFunc
- Lazy load the stackless functions with sync.Once, given this a simple
atomic read, it shouldn't affect performance for the fast-path (I
haven't seen benchmarks with compression enabled).
2023-11-12 16:36:57 +01:00
Tiago Peczenyj 4010b16eef Add support to fs.fs on serve static files (#1640)
* substitute *os.File by fs.File

* refactor error handling by using the new recommended form

* finish implementation

* substitute seek(offset,0) by seek(offset, io.SeekStart)

* add unit test

* use io.SeekStart on Seek method
2023-11-05 14:46:42 +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
Oleksandr Redko e15a810a98 docs: fix grammar issues and typos in comments (#1492) 2023-02-13 10:43:44 +08:00
Oleksandr Redko 5147cecec7 docs: replace links to golang.org with go.dev (#1489)
- Replace https://godoc.org with https://pkg.go.dev.
- Replace https://golang.org with https://go.dev.
- Replace https://golang.org/pkg with https://pkg.go.dev.
- Replace https://blog.golang.org with https://go.dev/blog.
- Use https://pkg.go.dev/golang.org/x/net/http2  instead of non-existing https://http2.golang.org/.
- Remove trailing slashes.
2023-02-11 19:59:44 +08:00
Aoang ea6052464e Add Go 1.19 Support (#1355)
* Update Go Version to Go1.19.x And add cache

* Fix CI Line endings

* Update test CI Go Version to Go1.19.x And add cache

* Update Gosec Security Scanner CI to securego/gosec@v2.12.0

* Format comment

Go 1.19 adds support for links, lists, and clearer headings in doc comments. As part of this change, gofmt now reformats doc comments to make their rendered meaning clearer. See “Go Doc Comments” for syntax details and descriptions of common mistakes now highlighted by gofmt. As another part of this change, the new package go/doc/comment provides parsing and reformatting of doc comments as well as support for rendering them to HTML, Markdown, and text.

ref: https://tip.golang.org/doc/go1.19
ref: https://tip.golang.org/doc/comment

* Fix doc structure
2022-08-14 11:31:57 +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
Mike Faraponov 0880335533 Update compress.go (#978)
Add compatibility with flate.Reader to reduce allocations of bufio.NewReader structs backed by default size slices.
2021-02-24 20:43:56 +01:00
Erik Dubbelboer 32793db72d Run golangci-lint using a Github Action 2019-11-16 18:09:28 +01:00
Erik Dubbelboer d4f0cf56d8 Remove fasthttp.ByteBuffer
As advertised in https://github.com/valyala/fasthttp/commit/b5f96d4b4120bb1e09c23ac32baf21a14da4a71d
2018-10-01 14:15:29 +08:00
Aliaksandr Valialkin 30e92af08f Limit heap memory usage when compressing high number of concurrent responses
Previously each concurrent compression could allocate huge compression state
with the size up to 1Mb each. So 10K concurrent connections could result in
10Gb of compression state in the heap.

This CL limits the number of compression states among concurrent requests
when {Append,Write}{Gzip,Deflate}* functions are called to O(GOMAXPROCS).
These functions are used by CompressHandler* for non-streaming responses,
i.e. it should cover the majority of use cases.

Memory usage for 10K concurrent connections that compress responses drops
from 10Gb to 200Mb after this CL.
2017-05-17 14:45:31 +03:00
Aliaksandr Valialkin f49c6b3f96 Reduce stack space usage when using response compression 2016-11-03 19:38:42 +02:00
Aliaksandr Valialkin 204fed9187 Revert "Use standard compress packages until https://github.com/klauspost/compress/issues/64 is fixed"
The original issue has been fixed, so reverting.

This reverts commit 2f4876aaf2.
2016-10-20 18:02:21 +03:00
Aliaksandr Valialkin 2f4876aaf2 Use standard compress packages until https://github.com/klauspost/compress/issues/64 is fixed 2016-10-20 15:30:15 +03:00
Aliaksandr Valialkin b131f05f50 Revert "Use standard compress/* packages instead of github.com/klauspost/compress/*"
According to @klauspost, his packages are still faster than the standard packages.
See https://github.com/valyala/fasthttp/commit/a98191f43560ea2ddbbf848c1c727daeed2e39ae#commitcomment-19409416
for details.

This reverts commit a98191f435.
2016-10-13 14:57:35 +03:00
Aliaksandr Valialkin a98191f435 Use standard compress/* packages instead of github.com/klauspost/compress/*
The reasons are:
- @klauspost optimized standard packages in go1.7+
- This removes an external dependency from fasthttp
2016-10-13 13:01:51 +03:00
Sebastian Schepens 889c65bae6 Added BodyInflate and WriteInflate to match BodyGunzip and WriteGunzip 2016-02-22 14:36:58 -03:00
Aliaksandr Valialkin 87628147e5 Use zero-alloc copy instead of io.Copy 2016-02-13 11:01:18 +02:00
Aliaksandr Valialkin 6210374fa8 Implement io.Writer in ByteBuffer and use it instead of bytes.Buffer in isFileCompressible 2016-02-11 19:36:43 +02:00
Aliaksandr Valialkin bb2a414fe8 Issues #14, #46: use @klauspost 's compress library instead of standard compress/*, since it provides better performance on average 2016-02-10 16:40:14 +02:00
Aliaksandr Valialkin 999348e2d4 Issue #46: use zlib instead of flate for deflate Content-Encoding 2016-02-08 16:51:03 +02:00
Aliaksandr Valialkin 52ddf98cfd Fixed golint warnings 2016-01-19 12:43:23 +02:00
Aliaksandr Valialkin a89d890c8c Added WriteGzip and WriteGunzip helper functions 2016-01-04 15:27:13 +02:00
Aliaksandr Valialkin 0e7c44160c Added AppendGzipBytesLevel for gzipping with desired compression level 2015-12-31 12:15:55 +02:00
Aliaksandr Valialkin 95456fa9d7 Added AppendGzipBytes and AppendGunzipBytes for simplifying gzipping/gunzipping request/response bodies 2015-12-31 12:09:21 +02:00
Aliaksandr Valialkin 95093e35cf Removed redundant Reset() calls on bufio.Reader/bufio.Writer/gzipWriter/flateWriter before returning them to the pool, since Reset() is immediately called after these instances are obtained from pool 2015-12-29 15:10:14 +02:00
Aliaksandr Valialkin 22c2acbb9d Optimization: reuse gzip/flate readers and writers. This should improve response body compression/decompression performance a bit 2015-12-28 18:58:05 +02:00