aboutsummaryrefslogtreecommitdiff
path: root/lib/websocket
AgeCommit message (Collapse)Author
11 daysall: apply go fixShulhan
2026-02-13lib/websocket: remove generated HTML filesShulhan
The generated HTML files is outdated and it is not referenced anywhere.
2026-01-03all: use SPDX license header formatShulhan
2025-02-04all: remove the nolint tagsShulhan
The "nolint" tag is used to ignore lines from being processed by golangci-lint. Since we are not using golangci-lint anymore, now and in the future, those lines can be removed.
2025-01-23all: use for-range with numericShulhan
Go 1.22 now support for-range on numeric value.
2025-01-23all: replace "interface{}" with "any"Shulhan
2025-01-22lib/bytes: replace Copy and Concat with standard libraryShulhan
Since Go 1.20, the standard bytes package have the Copy function. Since Go 1.22, the standard slices package have the Concat function.
2024-12-29all: merge package "lib/ints" and "lib/ints64" into "slices"Shulhan
Now that Go has type parameter, we can use it to use the same function that accept different types for working with slice of int, int64.
2024-03-06all: conform with linter gosec, ineffasign, and makezeroShulhan
Some of warnings from those linter are false positives, so we just annotated them.
2024-03-05all: comply with linter recommendations #3Shulhan
For HTTP server that use TLS, set the minimum TLS version and ReadHeaderTimeout to mitigate slowloris attack. For HTTP client or server that parameterize the use of InsecureSkipVerify, annotate the line with "nolint:gosec" to allow the code pass the check. Library that still use sha1, in example in DKIM and TOTP, skip the warnings by annotating the line with "nolint:gosec". A pointer variable now allocated their address before assigning its value. Any error that returned now wrapped using "%w". Also, replace error checking using [errors.Is] or [errors.As] instead of using equal or not-equal operators. In "lib/http", replace any usage of "math/rand" with "crypto/rand". Any call of [math/big.Rat.SetString] now annotated with "nolint:gosec" since its false positive, the issue has been fixed in Go >= 1.17.7. Any switch case that does not cover the rest of the possible values now handled by adding the cases or by replacing the "default" case with the rest of values.
2024-03-05all: comply with linter recommendations #2Shulhan
HTTP request now implicitly create request with context. Any false positive related to not closing HTTP response body has been annotated with "nolint:bodyclose". In the example code, use consistent "// Output:" comment format, by prefixing with single space. Any comment on code now also prefixing with single space. An error returned without variables now use [errors.New] instead of [fmt.Errorf]. Any error returned using [fmt.Errorf] now wrapped using "%w" instead of "%s". Also, replace error checking using [errors.Is] or [errors.As], instead of using equal/not-equal operator. Any statement like "x = x OP y" now replaced with "x OP= y". Also, swap statement is simplified using "x, y = y, x". Any switch statement with single case now replaced with if-condition. Any call to defer on function or program that call [os.Exit], now replaced by calling the deferred function directly. Any if-else condition now replaced with switch statement, if possible.
2024-03-02all: move the repository to "git.sr.ht/~shulhan/pakakeh.go"Shulhan
There are several reasons that why we move from github.com. First, related to the name of package. We accidentally name the package with "share" a common word in English that does not reflect the content of repository. By moving to other repository, we can rename it to better and unique name, in this "pakakeh.go". Pakakeh is Minang word for tools, and ".go" suffix indicate that the repository related to Go programming language. Second, supporting open source. The new repository is hosted under sourcehut.org, the founder is known to support open source, and all their services are licensed under AGPL, unlike GitHub that are closed sources. Third, regarding GitHub CoPilot. The GitHub Terms of Service [1], allow any public content that are hosted there granted them to parse the content. On one side, GitHub helps and flourish the open source, but on another side have an issues regarding scraping the copyleft license [2]. [1]: https://docs.github.com/en/site-policy/github-terms/github-terms-of-service#4-license-grant-to-us [2]: https://githubcopilotinvestigation.com
2024-02-15all: set unused parameter to "_"Shulhan
2023-12-13all: fix linter warnings reported by reviveShulhan
There are some reports that I disagree with revive, in example, code should not declare the type after variables. In my opinion, on some cases, declaring the type make the code more readable and explicit. Since I did not want to add new configuration file, we changes it and follow revive for now.
2023-09-14all: fix variable shadowing as reported by shadow toolShulhan
The shadow tool [1] report a variable where its name is declared twice or more, in different scope. [1] https://pkg.go.dev/golang.org/x/tools@v0.13.0/go/analysis/passes/shadow
2023-09-13lib/websocket: replace package "math/rand" with "crypto/rand"Shulhan
The rand.Seed has been deprecated since Go 1.20 (we use 1.20 in go.mod). The problem is if the user of this module is using Go tools < v1.20, there is no guarantee that the Seed is initialize randomly.
2023-09-11websocket/internal: add missing comment to package autobahnShulhan
2023-09-11websocket/examples: unexport type chatClient in cmd/clientShulhan
Since cmd package will not be available in documentation, there is no need to export type and methods in it.
2023-09-11websocket/examples: add missing commentsShulhan
While at it, realign struct Account to make the size reduced from 32 to 24 bytes (-8).
2023-09-11lib/websocket: suppress unused parameter using "_"Shulhan
This fix some linter warnings on dummy methods and handlers.
2023-07-03websocket/examples: enhance the server and clientShulhan
In the server, change the listen port to 9101 to prevent conflict with autobahn test suite. In the client, add command "chat" and "chatbot". The "chat" command run client chart as before. The "chatbot" command create client for all known accounts and send N messages continuously.
2023-07-03lib/websocket: stop goroutines when no queue received after N durationShulhan
When the goroutine for upgrade, reader, or pinger does not receive any input from its queue after N duration, stop it. Currently, N equal to ServerOptions ReadWriteTimeout. This allow unused goroutines released back to system, minimizing resources usage.
2023-07-01lib/websocket: reformat comments and documentationShulhan
2023-07-01lib/websocket: handle concurrent ping using goroutinesShulhan
The maximum goroutines is quarter of max queue. The new goroutine for pinger will be dispatched when no goroutine can consume the current processed connection.
2023-07-01lib/websocket: prefix the error with function or method namesShulhan
Adding prefix provide better way to locate and debug the error in the future.
2023-07-01lib/websocket: handle concurrent Server read using goroutinesShulhan
The Server now dispatch a goroutine to consume event from poll reader for each client connection that is ready to read. The maximum number of goroutine is defined in ServerOptions maxGoroutineReader, which currently set to 1024.
2023-07-01autobahn: print reports after testing server and clientShulhan
2023-07-01lib/websocket: handle concurrent upgrade using goroutineShulhan
The maxGoroutineUpgrader define maximum goroutines running at the same time to handle client upgrade. The new goroutine only dispatched when others are full, so it will run incrementally not all at once. Default to defServerMaxGoroutineUpgrader (128) if its not set.
2023-07-01lib/websocket: move the autobahn test to package internalShulhan
Using directory testdata that contains Go code seems like not good choices for future improvement, especially if we want to add shared code that can be imported by server and client later.
2023-06-29lib/websocket: add option to set read/write timeout on ServerShulhan
The ReadWriteTimeout define the maximum duration the server wait when receiving/sending packet from/to client before considering the connection as broken. Default read-write timeout is 30 seconds if not set. This changes affect the exported function Send and Recv by adding additional parameter timeout to both of them.
2023-06-18websocket/testdata: rewrite autobahn test using containerShulhan
Since the autobahn script can only run on Python 2, it become hard to setup and run the test on distro that does not provide Python 2 anymore. The autobahn repository recommend to use docker instead. When testing the server, we simplify it by using make task "test-server". The test-server task run our test server in background, and then run the autobahn fuzzingclient from container. Once the tests completed, we trigger the server to shutdown by sending text frame with payload "shutdown". When testing the client, we simplify it by using make task "test-client". The test-client task run the autobahn fuzzingserver and then we run our client. Once client finished, we trigger the server to generate the reports and cleanup the container.
2023-06-18lib/websocket: revert maxBuffer back to 1024Shulhan
In 25d09e2625f we increase the maxBuffer to 4096 to try increasing the performance when handling large payload. Turns out increasing this make the server cannot handle larger payload.
2023-06-18lib/websocket: call Quit when handshake contains close or invalid frameShulhan
If the HTTP handshake response contains trailing frame, handle it directly. If the frame is invalid or control close operation, call Quit directly to trigger the HandleQuit if its defined by user.
2023-06-04lib/websocket: increase the max buffer and queue for better throughputShulhan
The maxBuffer increased from 1024 to 4096 bytes. The reason that we use 1024 previously is related to MTU size and maximum payload in TCP (although its higher, 1460 bytes). The maxQueue increase from 128 to 4096.
2023-05-20all: remove any usage of debug.Value in all packagesShulhan
Using global debug value for all packages turns out is not a good idea.
2023-04-04lib/websocket: fix test on RootRouteAddShulhan
Instead of comparing the Sprintf value, compare the expected and got directly.
2023-02-22lib/websocket: replace math/rand.Read with crypto/rand.ReadShulhan
The math/rand will be deprecated in Go 1.20.
2022-11-19lib/websocket: fix test for the Go 1.20Shulhan
In the next Go release (1.20), parsing URL with invalid percent escape will not throw error anymore [1]. [1] https://github.com/golang/go/issues/56732
2022-10-10lib/websocket: cleanup the channel gracefulClose during CloseShulhan
When calling Close, we initialize the internal channel gracefulClose so the client can check the close response from server on another goroutine serve() and signal back to Close to continue the closing process. This channel is never closed and nil-ed again after Close which may cause resource leaks. While at it, use sendClose to minimize duplicate code.
2022-10-10lib/websocket: check for EAGAIN and EINTR when reading raw socketShulhan
This fix tests that sometimes fail when running with -count=X, where X > 1, $ go test -race -count=30 -timeout=30s ./lib/websocket Upon inspecting, when client sending larger payload, for example 65536 bytes, server sometimes only read half of them and return an error "resource temporarily unavailable" or "interrupted system call".
2022-10-10lib/websocket: replace handleInvalidData and BadRequest with sendCloseShulhan
Both of those functions actually call send control CLOSE frame and not exported. So, instead of duplicating it, we replace it with sendClose.
2022-10-10lib/websocket: fix possible data race on ClientShulhan
The Client have method send that check if the underlying connection (conn) has been closed or not. Since the conn can be closed anytime, for example server send to the control CLOSE frame: recv -> handleFrame -> handleClose -> Quit we need to guard the conn with Mutex before calling send to prevent data race.
2022-10-08lib/websocket: rewords some comment and package documentationShulhan
2022-10-02lib/websocket: remove global variables when testing rootRouteShulhan
This is to fix running test with -count=X, where X>1.
2022-09-15lib/websocket: fix possible race during testing ClientShulhan
During testing the Client we use the un-exported method send, while the test cases itself may close the connection and we did not guard it.
2022-07-29lib/websocket: reformat and paraphrase some commentsShulhan
2022-07-29lib/websocket: fix possible data race on client testShulhan
We only do lock on exported methods, but when doing testing we call the send method without lock which cause read and write on Client.conn at the same time.
2022-06-09lib/websocket: refactoring codeShulhan
Replace assignment from using ":=" to use variable declaration with type. Rationale: clarity and minimize duplicate temporary variables with the same type.
2022-06-09lib/websocket: realign all struct to minimize allocationsShulhan
Changes, * Client: from 176 to 144 (-32 bytes) * ClientManager: from 64 to 40 (-24 bytes) * Request: from 88 to 72 (-16 bytes) * Response: from 40 to 24 (-16 bytes) * route: from 48 to 32 (-16 bytes) * Server: from 72 to 64 (-8 bytes) * ServerOptions: from 104 to 96 (s-8 bytes) Plus other structs in the tests.
2022-06-09lib/websocket: fix benchmarkShulhan
On BenchmarkUpgrader, newHandshake will return nil if the HTTP request header is invalid, and if we pass this to Server.handleUpgrade it will cause panic. This commit fix this by checking error on newHandshake.