Compare commits
No commits in common. "master" and "empty" have entirely different histories.
18 changed files with 2 additions and 1401 deletions
|
@ -1,21 +0,0 @@
|
||||||
name: DCO action
|
|
||||||
on: [pull_request]
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
dco:
|
|
||||||
name: DCO
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v3
|
|
||||||
with:
|
|
||||||
fetch-depth: 0
|
|
||||||
|
|
||||||
- name: Setup Go
|
|
||||||
uses: actions/setup-go@v3
|
|
||||||
with:
|
|
||||||
go-version: '1.23'
|
|
||||||
|
|
||||||
- name: Run commit format checker
|
|
||||||
uses: https://git.frostfs.info/TrueCloudLab/dco-go@v3
|
|
||||||
with:
|
|
||||||
from: 'origin/${{ github.event.pull_request.base.ref }}'
|
|
|
@ -1,25 +0,0 @@
|
||||||
name: Pre-commit hooks
|
|
||||||
on: [pull_request]
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
precommit:
|
|
||||||
name: Pre-commit
|
|
||||||
env:
|
|
||||||
# Skip pre-commit hooks which are executed by other actions.
|
|
||||||
SKIP: make-lint,go-staticcheck-repo-mod,go-unit-tests,gofumpt
|
|
||||||
runs-on: ubuntu-22.04
|
|
||||||
# If we use actions/setup-python from either Github or Gitea,
|
|
||||||
# the line above fails with a cryptic error about not being able to find python.
|
|
||||||
# So install everything manually.
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v3
|
|
||||||
- name: Set up Go
|
|
||||||
uses: actions/setup-go@v3
|
|
||||||
with:
|
|
||||||
go-version: 1.23
|
|
||||||
- name: Set up Python
|
|
||||||
run: |
|
|
||||||
apt update
|
|
||||||
apt install -y pre-commit
|
|
||||||
- name: Run pre-commit
|
|
||||||
run: pre-commit run --color=always --hook-stage manual --all-files
|
|
|
@ -1,111 +0,0 @@
|
||||||
name: Tests and linters
|
|
||||||
on: [pull_request]
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
lint:
|
|
||||||
name: Lint
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v3
|
|
||||||
|
|
||||||
- name: Set up Go
|
|
||||||
uses: actions/setup-go@v3
|
|
||||||
with:
|
|
||||||
go-version: '1.23'
|
|
||||||
cache: true
|
|
||||||
|
|
||||||
- name: Install linters
|
|
||||||
run: make lint-install
|
|
||||||
|
|
||||||
- name: Run linters
|
|
||||||
run: make lint
|
|
||||||
|
|
||||||
tests:
|
|
||||||
name: Tests
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
strategy:
|
|
||||||
matrix:
|
|
||||||
go_versions: [ '1.22', '1.23' ]
|
|
||||||
fail-fast: false
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v3
|
|
||||||
|
|
||||||
- name: Set up Go
|
|
||||||
uses: actions/setup-go@v3
|
|
||||||
with:
|
|
||||||
go-version: '${{ matrix.go_versions }}'
|
|
||||||
cache: true
|
|
||||||
|
|
||||||
- name: Run tests
|
|
||||||
run: make test
|
|
||||||
|
|
||||||
tests-race:
|
|
||||||
name: Tests with -race
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v3
|
|
||||||
|
|
||||||
- name: Set up Go
|
|
||||||
uses: actions/setup-go@v3
|
|
||||||
with:
|
|
||||||
go-version: '1.23'
|
|
||||||
cache: true
|
|
||||||
|
|
||||||
- name: Run tests
|
|
||||||
run: go test ./... -count=1 -race
|
|
||||||
|
|
||||||
staticcheck:
|
|
||||||
name: Staticcheck
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v3
|
|
||||||
|
|
||||||
- name: Set up Go
|
|
||||||
uses: actions/setup-go@v3
|
|
||||||
with:
|
|
||||||
go-version: '1.23'
|
|
||||||
cache: true
|
|
||||||
|
|
||||||
- name: Install staticcheck
|
|
||||||
run: make staticcheck-install
|
|
||||||
|
|
||||||
- name: Run staticcheck
|
|
||||||
run: make staticcheck-run
|
|
||||||
|
|
||||||
gopls:
|
|
||||||
name: gopls check
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v3
|
|
||||||
|
|
||||||
- name: Set up Go
|
|
||||||
uses: actions/setup-go@v3
|
|
||||||
with:
|
|
||||||
go-version: '1.23'
|
|
||||||
cache: true
|
|
||||||
|
|
||||||
- name: Install gopls
|
|
||||||
run: make gopls-install
|
|
||||||
|
|
||||||
- name: Run gopls
|
|
||||||
run: make gopls-run
|
|
||||||
|
|
||||||
fumpt:
|
|
||||||
name: Run gofumpt
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v3
|
|
||||||
|
|
||||||
- name: Set up Go
|
|
||||||
uses: actions/setup-go@v3
|
|
||||||
with:
|
|
||||||
go-version: '1.23'
|
|
||||||
cache: true
|
|
||||||
|
|
||||||
- name: Install gofumpt
|
|
||||||
run: make fumpt-install
|
|
||||||
|
|
||||||
- name: Run gofumpt
|
|
||||||
run: |
|
|
||||||
make fumpt
|
|
||||||
git diff --exit-code --quiet
|
|
|
@ -1,23 +0,0 @@
|
||||||
name: Vulncheck
|
|
||||||
on: [pull_request]
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
vulncheck:
|
|
||||||
name: Vulncheck
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v3
|
|
||||||
with:
|
|
||||||
fetch-depth: 0
|
|
||||||
|
|
||||||
- name: Setup Go
|
|
||||||
uses: actions/setup-go@v3
|
|
||||||
with:
|
|
||||||
go-version: '1.23'
|
|
||||||
check-latest: true
|
|
||||||
|
|
||||||
- name: Install govulncheck
|
|
||||||
run: go install golang.org/x/vuln/cmd/govulncheck@latest
|
|
||||||
|
|
||||||
- name: Run govulncheck
|
|
||||||
run: govulncheck ./...
|
|
1
.gitattributes
vendored
1
.gitattributes
vendored
|
@ -1 +0,0 @@
|
||||||
/go.sum -diff
|
|
22
.gitignore
vendored
22
.gitignore
vendored
|
@ -1,22 +0,0 @@
|
||||||
# IDE
|
|
||||||
.idea
|
|
||||||
.vscode
|
|
||||||
|
|
||||||
# Vendoring
|
|
||||||
vendor
|
|
||||||
|
|
||||||
# tempfiles
|
|
||||||
.DS_Store
|
|
||||||
*~
|
|
||||||
.cache
|
|
||||||
|
|
||||||
temp
|
|
||||||
tmp
|
|
||||||
|
|
||||||
# binary
|
|
||||||
bin/
|
|
||||||
release/
|
|
||||||
|
|
||||||
# coverage
|
|
||||||
coverage.txt
|
|
||||||
coverage.html
|
|
|
@ -1,75 +0,0 @@
|
||||||
# This file contains all available configuration options
|
|
||||||
# with their default values.
|
|
||||||
|
|
||||||
# options for analysis running
|
|
||||||
run:
|
|
||||||
# timeout for analysis, e.g. 30s, 5m, default is 1m
|
|
||||||
timeout: 20m
|
|
||||||
|
|
||||||
# include test files or not, default is true
|
|
||||||
tests: false
|
|
||||||
|
|
||||||
# output configuration options
|
|
||||||
output:
|
|
||||||
# colored-line-number|line-number|json|tab|checkstyle|code-climate, default is "colored-line-number"
|
|
||||||
formats:
|
|
||||||
- format: tab
|
|
||||||
|
|
||||||
# all available settings of specific linters
|
|
||||||
linters-settings:
|
|
||||||
exhaustive:
|
|
||||||
# indicates that switch statements are to be considered exhaustive if a
|
|
||||||
# 'default' case is present, even if all enum members aren't listed in the
|
|
||||||
# switch
|
|
||||||
default-signifies-exhaustive: true
|
|
||||||
govet:
|
|
||||||
# report about shadowed variables
|
|
||||||
check-shadowing: false
|
|
||||||
staticcheck:
|
|
||||||
checks: ["all", "-SA1019"] # TODO Enable SA1019 after deprecated warning are fixed.
|
|
||||||
funlen:
|
|
||||||
lines: 80 # default 60
|
|
||||||
statements: 60 # default 40
|
|
||||||
gocognit:
|
|
||||||
min-complexity: 40 # default 30
|
|
||||||
unused:
|
|
||||||
field-writes-are-uses: false
|
|
||||||
exported-fields-are-used: false
|
|
||||||
local-variables-are-used: false
|
|
||||||
|
|
||||||
linters:
|
|
||||||
enable:
|
|
||||||
# mandatory linters
|
|
||||||
- govet
|
|
||||||
- revive
|
|
||||||
|
|
||||||
# some default golangci-lint linters
|
|
||||||
- errcheck
|
|
||||||
- gosimple
|
|
||||||
- godot
|
|
||||||
- ineffassign
|
|
||||||
- staticcheck
|
|
||||||
- typecheck
|
|
||||||
- unused
|
|
||||||
|
|
||||||
# extra linters
|
|
||||||
- bidichk
|
|
||||||
- durationcheck
|
|
||||||
- exhaustive
|
|
||||||
- copyloopvar
|
|
||||||
- gofmt
|
|
||||||
- goimports
|
|
||||||
- misspell
|
|
||||||
- predeclared
|
|
||||||
- reassign
|
|
||||||
- whitespace
|
|
||||||
- containedctx
|
|
||||||
- funlen
|
|
||||||
- gocognit
|
|
||||||
- contextcheck
|
|
||||||
- importas
|
|
||||||
- perfsprint
|
|
||||||
- testifylint
|
|
||||||
- protogetter
|
|
||||||
disable-all: true
|
|
||||||
fast: false
|
|
|
@ -1,56 +0,0 @@
|
||||||
ci:
|
|
||||||
autofix_prs: false
|
|
||||||
|
|
||||||
repos:
|
|
||||||
- repo: https://github.com/pre-commit/pre-commit-hooks
|
|
||||||
rev: v4.5.0
|
|
||||||
hooks:
|
|
||||||
- id: check-added-large-files
|
|
||||||
- id: check-case-conflict
|
|
||||||
- id: check-executables-have-shebangs
|
|
||||||
- id: check-shebang-scripts-are-executable
|
|
||||||
- id: check-merge-conflict
|
|
||||||
- id: check-json
|
|
||||||
- id: check-xml
|
|
||||||
- id: check-yaml
|
|
||||||
- id: trailing-whitespace
|
|
||||||
args: [--markdown-linebreak-ext=md]
|
|
||||||
- id: end-of-file-fixer
|
|
||||||
exclude: "(.key|.svg)$"
|
|
||||||
|
|
||||||
- repo: https://github.com/shellcheck-py/shellcheck-py
|
|
||||||
rev: v0.9.0.6
|
|
||||||
hooks:
|
|
||||||
- id: shellcheck
|
|
||||||
|
|
||||||
- repo: local
|
|
||||||
hooks:
|
|
||||||
- id: make-lint
|
|
||||||
name: Run Make Lint
|
|
||||||
entry: make lint
|
|
||||||
language: system
|
|
||||||
pass_filenames: false
|
|
||||||
|
|
||||||
- repo: local
|
|
||||||
hooks:
|
|
||||||
- id: go-unit-tests
|
|
||||||
name: go unit tests
|
|
||||||
entry: make test GOFLAGS=''
|
|
||||||
pass_filenames: false
|
|
||||||
types: [go]
|
|
||||||
language: system
|
|
||||||
|
|
||||||
- repo: local
|
|
||||||
hooks:
|
|
||||||
- id: gofumpt
|
|
||||||
name: gofumpt
|
|
||||||
entry: make fumpt
|
|
||||||
pass_filenames: false
|
|
||||||
types: [go]
|
|
||||||
language: system
|
|
||||||
|
|
||||||
- repo: https://github.com/TekWizely/pre-commit-golang
|
|
||||||
rev: v1.0.0-rc.1
|
|
||||||
hooks:
|
|
||||||
- id: go-staticcheck-repo-mod
|
|
||||||
- id: go-mod-tidy
|
|
|
@ -1,3 +0,0 @@
|
||||||
.* @TrueCloudLab/storage-core-committers @TrueCloudLab/storage-core-developers
|
|
||||||
.forgejo/.* @potyarkin
|
|
||||||
Makefile @potyarkin
|
|
201
LICENSE
201
LICENSE
|
@ -1,201 +0,0 @@
|
||||||
Apache License
|
|
||||||
Version 2.0, January 2004
|
|
||||||
http://www.apache.org/licenses/
|
|
||||||
|
|
||||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
||||||
|
|
||||||
1. Definitions.
|
|
||||||
|
|
||||||
"License" shall mean the terms and conditions for use, reproduction,
|
|
||||||
and distribution as defined by Sections 1 through 9 of this document.
|
|
||||||
|
|
||||||
"Licensor" shall mean the copyright owner or entity authorized by
|
|
||||||
the copyright owner that is granting the License.
|
|
||||||
|
|
||||||
"Legal Entity" shall mean the union of the acting entity and all
|
|
||||||
other entities that control, are controlled by, or are under common
|
|
||||||
control with that entity. For the purposes of this definition,
|
|
||||||
"control" means (i) the power, direct or indirect, to cause the
|
|
||||||
direction or management of such entity, whether by contract or
|
|
||||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
||||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
||||||
|
|
||||||
"You" (or "Your") shall mean an individual or Legal Entity
|
|
||||||
exercising permissions granted by this License.
|
|
||||||
|
|
||||||
"Source" form shall mean the preferred form for making modifications,
|
|
||||||
including but not limited to software source code, documentation
|
|
||||||
source, and configuration files.
|
|
||||||
|
|
||||||
"Object" form shall mean any form resulting from mechanical
|
|
||||||
transformation or translation of a Source form, including but
|
|
||||||
not limited to compiled object code, generated documentation,
|
|
||||||
and conversions to other media types.
|
|
||||||
|
|
||||||
"Work" shall mean the work of authorship, whether in Source or
|
|
||||||
Object form, made available under the License, as indicated by a
|
|
||||||
copyright notice that is included in or attached to the work
|
|
||||||
(an example is provided in the Appendix below).
|
|
||||||
|
|
||||||
"Derivative Works" shall mean any work, whether in Source or Object
|
|
||||||
form, that is based on (or derived from) the Work and for which the
|
|
||||||
editorial revisions, annotations, elaborations, or other modifications
|
|
||||||
represent, as a whole, an original work of authorship. For the purposes
|
|
||||||
of this License, Derivative Works shall not include works that remain
|
|
||||||
separable from, or merely link (or bind by name) to the interfaces of,
|
|
||||||
the Work and Derivative Works thereof.
|
|
||||||
|
|
||||||
"Contribution" shall mean any work of authorship, including
|
|
||||||
the original version of the Work and any modifications or additions
|
|
||||||
to that Work or Derivative Works thereof, that is intentionally
|
|
||||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
||||||
or by an individual or Legal Entity authorized to submit on behalf of
|
|
||||||
the copyright owner. For the purposes of this definition, "submitted"
|
|
||||||
means any form of electronic, verbal, or written communication sent
|
|
||||||
to the Licensor or its representatives, including but not limited to
|
|
||||||
communication on electronic mailing lists, source code control systems,
|
|
||||||
and issue tracking systems that are managed by, or on behalf of, the
|
|
||||||
Licensor for the purpose of discussing and improving the Work, but
|
|
||||||
excluding communication that is conspicuously marked or otherwise
|
|
||||||
designated in writing by the copyright owner as "Not a Contribution."
|
|
||||||
|
|
||||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
||||||
on behalf of whom a Contribution has been received by Licensor and
|
|
||||||
subsequently incorporated within the Work.
|
|
||||||
|
|
||||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
||||||
this License, each Contributor hereby grants to You a perpetual,
|
|
||||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
||||||
copyright license to reproduce, prepare Derivative Works of,
|
|
||||||
publicly display, publicly perform, sublicense, and distribute the
|
|
||||||
Work and such Derivative Works in Source or Object form.
|
|
||||||
|
|
||||||
3. Grant of Patent License. Subject to the terms and conditions of
|
|
||||||
this License, each Contributor hereby grants to You a perpetual,
|
|
||||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
||||||
(except as stated in this section) patent license to make, have made,
|
|
||||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
||||||
where such license applies only to those patent claims licensable
|
|
||||||
by such Contributor that are necessarily infringed by their
|
|
||||||
Contribution(s) alone or by combination of their Contribution(s)
|
|
||||||
with the Work to which such Contribution(s) was submitted. If You
|
|
||||||
institute patent litigation against any entity (including a
|
|
||||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
||||||
or a Contribution incorporated within the Work constitutes direct
|
|
||||||
or contributory patent infringement, then any patent licenses
|
|
||||||
granted to You under this License for that Work shall terminate
|
|
||||||
as of the date such litigation is filed.
|
|
||||||
|
|
||||||
4. Redistribution. You may reproduce and distribute copies of the
|
|
||||||
Work or Derivative Works thereof in any medium, with or without
|
|
||||||
modifications, and in Source or Object form, provided that You
|
|
||||||
meet the following conditions:
|
|
||||||
|
|
||||||
(a) You must give any other recipients of the Work or
|
|
||||||
Derivative Works a copy of this License; and
|
|
||||||
|
|
||||||
(b) You must cause any modified files to carry prominent notices
|
|
||||||
stating that You changed the files; and
|
|
||||||
|
|
||||||
(c) You must retain, in the Source form of any Derivative Works
|
|
||||||
that You distribute, all copyright, patent, trademark, and
|
|
||||||
attribution notices from the Source form of the Work,
|
|
||||||
excluding those notices that do not pertain to any part of
|
|
||||||
the Derivative Works; and
|
|
||||||
|
|
||||||
(d) If the Work includes a "NOTICE" text file as part of its
|
|
||||||
distribution, then any Derivative Works that You distribute must
|
|
||||||
include a readable copy of the attribution notices contained
|
|
||||||
within such NOTICE file, excluding those notices that do not
|
|
||||||
pertain to any part of the Derivative Works, in at least one
|
|
||||||
of the following places: within a NOTICE text file distributed
|
|
||||||
as part of the Derivative Works; within the Source form or
|
|
||||||
documentation, if provided along with the Derivative Works; or,
|
|
||||||
within a display generated by the Derivative Works, if and
|
|
||||||
wherever such third-party notices normally appear. The contents
|
|
||||||
of the NOTICE file are for informational purposes only and
|
|
||||||
do not modify the License. You may add Your own attribution
|
|
||||||
notices within Derivative Works that You distribute, alongside
|
|
||||||
or as an addendum to the NOTICE text from the Work, provided
|
|
||||||
that such additional attribution notices cannot be construed
|
|
||||||
as modifying the License.
|
|
||||||
|
|
||||||
You may add Your own copyright statement to Your modifications and
|
|
||||||
may provide additional or different license terms and conditions
|
|
||||||
for use, reproduction, or distribution of Your modifications, or
|
|
||||||
for any such Derivative Works as a whole, provided Your use,
|
|
||||||
reproduction, and distribution of the Work otherwise complies with
|
|
||||||
the conditions stated in this License.
|
|
||||||
|
|
||||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
||||||
any Contribution intentionally submitted for inclusion in the Work
|
|
||||||
by You to the Licensor shall be under the terms and conditions of
|
|
||||||
this License, without any additional terms or conditions.
|
|
||||||
Notwithstanding the above, nothing herein shall supersede or modify
|
|
||||||
the terms of any separate license agreement you may have executed
|
|
||||||
with Licensor regarding such Contributions.
|
|
||||||
|
|
||||||
6. Trademarks. This License does not grant permission to use the trade
|
|
||||||
names, trademarks, service marks, or product names of the Licensor,
|
|
||||||
except as required for reasonable and customary use in describing the
|
|
||||||
origin of the Work and reproducing the content of the NOTICE file.
|
|
||||||
|
|
||||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
||||||
agreed to in writing, Licensor provides the Work (and each
|
|
||||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
||||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
||||||
implied, including, without limitation, any warranties or conditions
|
|
||||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
||||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
||||||
appropriateness of using or redistributing the Work and assume any
|
|
||||||
risks associated with Your exercise of permissions under this License.
|
|
||||||
|
|
||||||
8. Limitation of Liability. In no event and under no legal theory,
|
|
||||||
whether in tort (including negligence), contract, or otherwise,
|
|
||||||
unless required by applicable law (such as deliberate and grossly
|
|
||||||
negligent acts) or agreed to in writing, shall any Contributor be
|
|
||||||
liable to You for damages, including any direct, indirect, special,
|
|
||||||
incidental, or consequential damages of any character arising as a
|
|
||||||
result of this License or out of the use or inability to use the
|
|
||||||
Work (including but not limited to damages for loss of goodwill,
|
|
||||||
work stoppage, computer failure or malfunction, or any and all
|
|
||||||
other commercial damages or losses), even if such Contributor
|
|
||||||
has been advised of the possibility of such damages.
|
|
||||||
|
|
||||||
9. Accepting Warranty or Additional Liability. While redistributing
|
|
||||||
the Work or Derivative Works thereof, You may choose to offer,
|
|
||||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
||||||
or other liability obligations and/or rights consistent with this
|
|
||||||
License. However, in accepting such obligations, You may act only
|
|
||||||
on Your own behalf and on Your sole responsibility, not on behalf
|
|
||||||
of any other Contributor, and only if You agree to indemnify,
|
|
||||||
defend, and hold each Contributor harmless for any liability
|
|
||||||
incurred by, or claims asserted against, such Contributor by reason
|
|
||||||
of your accepting any such warranty or additional liability.
|
|
||||||
|
|
||||||
END OF TERMS AND CONDITIONS
|
|
||||||
|
|
||||||
APPENDIX: How to apply the Apache License to your work.
|
|
||||||
|
|
||||||
To apply the Apache License to your work, attach the following
|
|
||||||
boilerplate notice, with the fields enclosed by brackets "{}"
|
|
||||||
replaced with your own identifying information. (Don't include
|
|
||||||
the brackets!) The text should be enclosed in the appropriate
|
|
||||||
comment syntax for the file format. We also recommend that a
|
|
||||||
file or class name and description of purpose be included on the
|
|
||||||
same "printed page" as the copyright notice for easier
|
|
||||||
identification within third-party archives.
|
|
||||||
|
|
||||||
Copyright {yyyy} {name of copyright owner}
|
|
||||||
|
|
||||||
Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
you may not use this file except in compliance with the License.
|
|
||||||
You may obtain a copy of the License at
|
|
||||||
|
|
||||||
http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
|
|
||||||
Unless required by applicable law or agreed to in writing, software
|
|
||||||
distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
See the License for the specific language governing permissions and
|
|
||||||
limitations under the License.
|
|
82
Makefile
82
Makefile
|
@ -1,82 +0,0 @@
|
||||||
#!/usr/bin/make -f
|
|
||||||
|
|
||||||
STATICCHECK_VERSION ?= 2024.1.1
|
|
||||||
LINT_VERSION ?= 1.60.3
|
|
||||||
|
|
||||||
BIN = bin
|
|
||||||
OUTPUT_LINT_DIR ?= $(abspath $(BIN))/linters
|
|
||||||
LINT_DIR = $(OUTPUT_LINT_DIR)/golangci-lint-$(LINT_VERSION)
|
|
||||||
TMP_DIR := .cache
|
|
||||||
|
|
||||||
STATICCHECK_DIR ?= $(abspath $(BIN))/staticcheck
|
|
||||||
STATICCHECK_VERSION_DIR ?= $(STATICCHECK_DIR)/$(STATICCHECK_VERSION)
|
|
||||||
|
|
||||||
GOFUMPT_VERSION ?= v0.7.0
|
|
||||||
GOFUMPT_DIR ?= $(abspath $(BIN))/gofumpt
|
|
||||||
GOFUMPT_VERSION_DIR ?= $(GOFUMPT_DIR)/$(GOFUMPT_VERSION)
|
|
||||||
|
|
||||||
GOPLS_VERSION ?= v0.16.2
|
|
||||||
GOPLS_DIR ?= $(abspath $(BIN))/gopls
|
|
||||||
GOPLS_VERSION_DIR ?= $(GOPLS_DIR)/$(GOPLS_VERSION)
|
|
||||||
GOPLS_TEMP_FILE := $(shell mktemp)
|
|
||||||
|
|
||||||
test:
|
|
||||||
go test -count=1 -v ./...
|
|
||||||
|
|
||||||
# Install linters
|
|
||||||
lint-install:
|
|
||||||
@rm -rf $(OUTPUT_LINT_DIR)
|
|
||||||
@mkdir -p $(OUTPUT_LINT_DIR)
|
|
||||||
@CGO_ENABLED=1 GOBIN=$(LINT_DIR) go install -trimpath github.com/golangci/golangci-lint/cmd/golangci-lint@v$(LINT_VERSION)
|
|
||||||
|
|
||||||
# Run linters
|
|
||||||
lint:
|
|
||||||
@if [ ! -d "$(LINT_DIR)" ]; then \
|
|
||||||
make lint-install; \
|
|
||||||
fi
|
|
||||||
$(LINT_DIR)/golangci-lint run
|
|
||||||
|
|
||||||
# Install staticcheck
|
|
||||||
staticcheck-install:
|
|
||||||
@rm -rf $(STATICCHECK_DIR)
|
|
||||||
@mkdir -p $(STATICCHECK_DIR)
|
|
||||||
@GOBIN=$(STATICCHECK_VERSION_DIR) go install honnef.co/go/tools/cmd/staticcheck@$(STATICCHECK_VERSION)
|
|
||||||
|
|
||||||
# Run staticcheck
|
|
||||||
staticcheck-run:
|
|
||||||
@if [ ! -d "$(STATICCHECK_VERSION_DIR)" ]; then \
|
|
||||||
make staticcheck-install; \
|
|
||||||
fi
|
|
||||||
@$(STATICCHECK_VERSION_DIR)/staticcheck ./...
|
|
||||||
|
|
||||||
# Install gopls
|
|
||||||
gopls-install:
|
|
||||||
@rm -rf $(GOPLS_DIR)
|
|
||||||
@mkdir -p $(GOPLS_DIR)
|
|
||||||
@GOBIN=$(GOPLS_VERSION_DIR) go install golang.org/x/tools/gopls@$(GOPLS_VERSION)
|
|
||||||
|
|
||||||
# Run gopls
|
|
||||||
gopls-run:
|
|
||||||
@if [ ! -d "$(GOPLS_VERSION_DIR)" ]; then \
|
|
||||||
make gopls-install; \
|
|
||||||
fi
|
|
||||||
$(GOPLS_VERSION_DIR)/gopls check $(SOURCES) 2>&1 >$(GOPLS_TEMP_FILE)
|
|
||||||
@if [[ $$(wc -l < $(GOPLS_TEMP_FILE)) -ne 0 ]]; then \
|
|
||||||
cat $(GOPLS_TEMP_FILE); \
|
|
||||||
exit 1; \
|
|
||||||
fi
|
|
||||||
rm $(GOPLS_TEMP_FILE)
|
|
||||||
|
|
||||||
# Install gofumpt
|
|
||||||
fumpt-install:
|
|
||||||
@rm -rf $(GOFUMPT_DIR)
|
|
||||||
@mkdir -p $(GOFUMPT_DIR)
|
|
||||||
@GOBIN=$(GOFUMPT_VERSION_DIR) go install mvdan.cc/gofumpt@$(GOFUMPT_VERSION)
|
|
||||||
|
|
||||||
# Run gofumpt
|
|
||||||
fumpt:
|
|
||||||
@if [ ! -d "$(GOFUMPT_VERSION_DIR)" ]; then \
|
|
||||||
make fumpt-install; \
|
|
||||||
fi
|
|
||||||
@echo "⇒ Processing gofumpt check"
|
|
||||||
$(GOFUMPT_VERSION_DIR)/gofumpt -l -w .
|
|
74
README.md
74
README.md
|
@ -1,73 +1,3 @@
|
||||||
# Source-based routing in Golang
|
# WIP area: this repo is just a fork!
|
||||||
|
|
||||||
|
Useful things may be published only in [other branches](../../../branches)
|
||||||
Consider this routing table:
|
|
||||||
```
|
|
||||||
10.11.70.0/23 dev data0 proto kernel scope link src 10.11.70.42
|
|
||||||
10.11.70.0/23 dev data1 proto kernel scope link src 10.11.71.42
|
|
||||||
192.168.123.0/24 dev internal0 proto kernel scope link src 192.168.123.42
|
|
||||||
192.168.123.0/24 dev internal1 proto kernel scope link src 192.168.123.142
|
|
||||||
```
|
|
||||||
|
|
||||||
Simple `net.Dial` to either `10.11.70.42` or `10.11.71.42` will match the first subnet and be routed via data0.
|
|
||||||
This problems is usually solved by bonds.
|
|
||||||
But sometimes you need to invent a bicycle.
|
|
||||||
|
|
||||||
## Usage
|
|
||||||
|
|
||||||
```golang
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"net"
|
|
||||||
"net/netip"
|
|
||||||
|
|
||||||
"git.frostfs.info/TrueCloudLab/multinet"
|
|
||||||
)
|
|
||||||
|
|
||||||
d, err := multinet.NewDialer(Config{
|
|
||||||
Subnets: []Subnet{
|
|
||||||
{
|
|
||||||
Prefix: netip.MustParsePrefix("10.11.70.0/23"),
|
|
||||||
SourceIPs: []netip.Addr{
|
|
||||||
netip.MustParseAddr("10.11.70.42"),
|
|
||||||
netip.MustParseAddr("10.11.71.42"),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Prefix: netip.MustParsePrefix("192.168.123.0/24"),
|
|
||||||
SourceIPs: []netip.Addr{
|
|
||||||
netip.MustParseAddr("192.168.123.42"),
|
|
||||||
netip.MustParseAddr("192.168.123.142"),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
Balancer: multinet.BalancerTypeRoundRobin,
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
// handle error
|
|
||||||
}
|
|
||||||
|
|
||||||
conn, err := d.DialContext(ctx, "tcp", "10.11.70.42")
|
|
||||||
if err != nil {
|
|
||||||
// handle error
|
|
||||||
}
|
|
||||||
// do stuff
|
|
||||||
```
|
|
||||||
|
|
||||||
## License and copyright
|
|
||||||
|
|
||||||
Copyright 2023-2024 FrostFS contributors
|
|
||||||
|
|
||||||
```
|
|
||||||
Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
you may not use this file except in compliance with the License.
|
|
||||||
You may obtain a copy of the License at
|
|
||||||
|
|
||||||
http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
|
|
||||||
Unless required by applicable law or agreed to in writing, software
|
|
||||||
distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
See the License for the specific language governing permissions and
|
|
||||||
limitations under the License.
|
|
||||||
```
|
|
||||||
|
|
56
balancer.go
56
balancer.go
|
@ -1,56 +0,0 @@
|
||||||
package multinet
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"net"
|
|
||||||
"sync/atomic"
|
|
||||||
)
|
|
||||||
|
|
||||||
// BalancerType reperents the algorithm which is used to pick source address.
|
|
||||||
type BalancerType string
|
|
||||||
|
|
||||||
const (
|
|
||||||
// BalancerTypeNoop picks first address for which link is up.
|
|
||||||
BalancerTypeNoop BalancerType = ""
|
|
||||||
// BalancerTypeNoop implements simple round-robin between up links.
|
|
||||||
// It is not fair in case some links are down.
|
|
||||||
BalancerTypeRoundRobin BalancerType = "roundrobin"
|
|
||||||
)
|
|
||||||
|
|
||||||
var errNoSuitableNodeFound = errors.New("no suitale node found")
|
|
||||||
|
|
||||||
type balancer interface {
|
|
||||||
DialContext(ctx context.Context, s *Subnet, network, address string) (net.Conn, error)
|
|
||||||
}
|
|
||||||
|
|
||||||
type roundRobin struct {
|
|
||||||
d *dialer
|
|
||||||
i atomic.Uint32
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *roundRobin) DialContext(ctx context.Context, s *Subnet, network, address string) (net.Conn, error) {
|
|
||||||
next := int(r.i.Add(1))
|
|
||||||
for i := range s.SourceIPs {
|
|
||||||
ii := s.SourceIPs[(i+next)%len(s.SourceIPs)]
|
|
||||||
dd := r.d.dialer
|
|
||||||
dd.LocalAddr = &net.TCPAddr{IP: net.IP(ii.AsSlice())}
|
|
||||||
return r.d.dialContext(ctx, &dd, network, address)
|
|
||||||
}
|
|
||||||
return nil, fmt.Errorf("(*roundRobin).DialContext: %w", errNoSuitableNodeFound)
|
|
||||||
}
|
|
||||||
|
|
||||||
type firstEnabled struct {
|
|
||||||
d *dialer
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *firstEnabled) DialContext(ctx context.Context, s *Subnet, network, address string) (net.Conn, error) {
|
|
||||||
for i := range s.SourceIPs {
|
|
||||||
ii := s.SourceIPs[i]
|
|
||||||
dd := r.d.dialer
|
|
||||||
dd.LocalAddr = &net.TCPAddr{IP: net.IP(ii.AsSlice())}
|
|
||||||
return r.d.dialContext(ctx, &dd, network, address)
|
|
||||||
}
|
|
||||||
return nil, fmt.Errorf("(*firstEnabled).DialContext: %w", errNoSuitableNodeFound)
|
|
||||||
}
|
|
327
dialer.go
327
dialer.go
|
@ -1,327 +0,0 @@
|
||||||
package multinet
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"fmt"
|
|
||||||
"net"
|
|
||||||
"net/netip"
|
|
||||||
"sync"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
defaultFallbackDelay = 300 * time.Millisecond
|
|
||||||
)
|
|
||||||
|
|
||||||
// Dialer contains the single most important method from the net.Dialer.
|
|
||||||
type Dialer interface {
|
|
||||||
DialContext(ctx context.Context, network, address string) (net.Conn, error)
|
|
||||||
}
|
|
||||||
|
|
||||||
var (
|
|
||||||
_ Dialer = (*net.Dialer)(nil)
|
|
||||||
_ Dialer = (*dialer)(nil)
|
|
||||||
)
|
|
||||||
|
|
||||||
type dialer struct {
|
|
||||||
// Protects subnets field (recursively).
|
|
||||||
mtx sync.RWMutex
|
|
||||||
subnets []Subnet
|
|
||||||
// Default options for the net.Dialer.
|
|
||||||
dialer net.Dialer
|
|
||||||
// If true, allow to dial only configured subnets.
|
|
||||||
restrict bool
|
|
||||||
// Algorithm to which picks source address for each ip.
|
|
||||||
balancer balancer
|
|
||||||
// Overrides `net.Dialer.DialContext()` if specified.
|
|
||||||
customDialContext func(d *net.Dialer, ctx context.Context, network, address string) (net.Conn, error)
|
|
||||||
// Hostname resolver
|
|
||||||
resolver net.Resolver
|
|
||||||
// See Config.FallbackDelay description.
|
|
||||||
fallbackDelay time.Duration
|
|
||||||
// Event handler.
|
|
||||||
eh EventHandler
|
|
||||||
}
|
|
||||||
|
|
||||||
// Subnet represents a single subnet, possibly routable from multiple source IPs.
|
|
||||||
type Subnet struct {
|
|
||||||
Prefix netip.Prefix
|
|
||||||
SourceIPs []netip.Addr
|
|
||||||
}
|
|
||||||
|
|
||||||
type EventHandler interface {
|
|
||||||
DialPerformed(sourceIP net.Addr, network, address string, err error)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Config contains Multidialer configuration.
|
|
||||||
type Config struct {
|
|
||||||
// Routable subnets.
|
|
||||||
Subnets []Subnet
|
|
||||||
// If true, the only configurd subnets available through this dialer.
|
|
||||||
// Otherwise, a failback to the net.DefaultDialer.
|
|
||||||
Restrict bool
|
|
||||||
// Dialer contains default options for the net.Dialer to use.
|
|
||||||
// LocalAddr is overridden.
|
|
||||||
Dialer net.Dialer
|
|
||||||
// Balancer specifies algorithm used to pick source address.
|
|
||||||
Balancer BalancerType
|
|
||||||
// FallbackDelay specifies the length of time to wait before
|
|
||||||
// spawning a RFC 6555 Fast Fallback connection. That is, this
|
|
||||||
// is the amount of time to wait for IPv6 to succeed before
|
|
||||||
// assuming that IPv6 is misconfigured and falling back to
|
|
||||||
// IPv4.
|
|
||||||
//
|
|
||||||
// If zero, a default delay of 300ms is used.
|
|
||||||
// A negative value disables Fast Fallback support.
|
|
||||||
FallbackDelay time.Duration
|
|
||||||
// DialContext is custom DialContext function.
|
|
||||||
// If not specified, default implemenattion is used (`d.DialContext(ctx, network, address)`).
|
|
||||||
DialContext func(d *net.Dialer, ctx context.Context, network, address string) (net.Conn, error)
|
|
||||||
// EventHandler defines event handler.
|
|
||||||
EventHandler EventHandler
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewDialer ...
|
|
||||||
func NewDialer(c Config) (Dialer, error) {
|
|
||||||
var d dialer
|
|
||||||
d.subnets = c.Subnets
|
|
||||||
|
|
||||||
switch c.Balancer {
|
|
||||||
case BalancerTypeNoop:
|
|
||||||
d.balancer = &firstEnabled{d: &d}
|
|
||||||
case BalancerTypeRoundRobin:
|
|
||||||
d.balancer = &roundRobin{d: &d}
|
|
||||||
default:
|
|
||||||
return nil, fmt.Errorf("invalid balancer type: %s", c.Balancer)
|
|
||||||
}
|
|
||||||
|
|
||||||
d.restrict = c.Restrict
|
|
||||||
d.resolver.Dial = d.dialContextIP
|
|
||||||
d.fallbackDelay = c.FallbackDelay
|
|
||||||
if d.fallbackDelay == 0 {
|
|
||||||
d.fallbackDelay = defaultFallbackDelay
|
|
||||||
}
|
|
||||||
d.dialer = c.Dialer
|
|
||||||
if c.DialContext != nil {
|
|
||||||
d.customDialContext = c.DialContext
|
|
||||||
}
|
|
||||||
|
|
||||||
if c.EventHandler != nil {
|
|
||||||
d.eh = c.EventHandler
|
|
||||||
} else {
|
|
||||||
d.eh = noopEventHandler{}
|
|
||||||
}
|
|
||||||
|
|
||||||
return &d, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// DialContext implements the Dialer interface.
|
|
||||||
// Hostnames for address are currently not supported.
|
|
||||||
func (d *dialer) DialContext(ctx context.Context, network, address string) (net.Conn, error) {
|
|
||||||
addr, err := netip.ParseAddrPort(address)
|
|
||||||
if err != nil { // try resolve as hostname
|
|
||||||
return d.dialContextHostname(ctx, network, address)
|
|
||||||
}
|
|
||||||
return d.dialAddr(ctx, network, address, addr)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *dialer) dialContextIP(ctx context.Context, network, address string) (net.Conn, error) {
|
|
||||||
addr, err := netip.ParseAddrPort(address)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return d.dialAddr(ctx, network, address, addr)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *dialer) dialContextHostname(ctx context.Context, network, address string) (net.Conn, error) {
|
|
||||||
// https://github.com/golang/go/blob/release-branch.go1.21/src/net/dial.go#L488
|
|
||||||
addrPorts, err := d.resolveHostIPs(ctx, address, network)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
var primaries, fallbacks []netip.AddrPort
|
|
||||||
if d.fallbackDelay >= 0 && network == "tcp" {
|
|
||||||
primaries, fallbacks = splitByType(addrPorts)
|
|
||||||
} else {
|
|
||||||
primaries = addrPorts
|
|
||||||
}
|
|
||||||
|
|
||||||
return d.dialParallel(ctx, network, primaries, fallbacks)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *dialer) dialSerial(ctx context.Context, network string, addrs []netip.AddrPort) (net.Conn, error) {
|
|
||||||
var firstErr error
|
|
||||||
for _, addr := range addrs {
|
|
||||||
select {
|
|
||||||
case <-ctx.Done():
|
|
||||||
return nil, ctx.Err()
|
|
||||||
default:
|
|
||||||
}
|
|
||||||
|
|
||||||
conn, err := d.dialAddr(ctx, network, addr.String(), addr)
|
|
||||||
if err == nil {
|
|
||||||
return conn, nil
|
|
||||||
}
|
|
||||||
if firstErr == nil {
|
|
||||||
firstErr = err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil, fmt.Errorf("failed to connect any resolved address: %w", firstErr)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *dialer) dialParallel(ctx context.Context, network string, primaries, fallbacks []netip.AddrPort) (net.Conn, error) {
|
|
||||||
if len(fallbacks) == 0 {
|
|
||||||
return d.dialSerial(ctx, network, primaries)
|
|
||||||
}
|
|
||||||
|
|
||||||
returned := make(chan struct{})
|
|
||||||
defer close(returned)
|
|
||||||
|
|
||||||
type dialResult struct {
|
|
||||||
net.Conn
|
|
||||||
error
|
|
||||||
primary bool
|
|
||||||
done bool
|
|
||||||
}
|
|
||||||
results := make(chan dialResult)
|
|
||||||
|
|
||||||
dialerFunc := func(ctx context.Context, primary bool) {
|
|
||||||
addrs := primaries
|
|
||||||
if !primary {
|
|
||||||
addrs = fallbacks
|
|
||||||
}
|
|
||||||
c, err := d.dialSerial(ctx, network, addrs)
|
|
||||||
select {
|
|
||||||
case results <- dialResult{Conn: c, error: err, primary: primary, done: true}:
|
|
||||||
case <-returned:
|
|
||||||
if c != nil {
|
|
||||||
c.Close()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var primary, fallback dialResult
|
|
||||||
|
|
||||||
primaryCtx, primaryCancel := context.WithCancel(ctx)
|
|
||||||
defer primaryCancel()
|
|
||||||
go dialerFunc(primaryCtx, true)
|
|
||||||
|
|
||||||
fallbackTimer := time.NewTimer(d.fallbackDelay)
|
|
||||||
defer fallbackTimer.Stop()
|
|
||||||
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case <-fallbackTimer.C:
|
|
||||||
fallbackCtx, fallbackCancel := context.WithCancel(ctx)
|
|
||||||
defer fallbackCancel()
|
|
||||||
go dialerFunc(fallbackCtx, false)
|
|
||||||
|
|
||||||
case res := <-results:
|
|
||||||
if res.error == nil {
|
|
||||||
return res.Conn, nil
|
|
||||||
}
|
|
||||||
if res.primary {
|
|
||||||
primary = res
|
|
||||||
} else {
|
|
||||||
fallback = res
|
|
||||||
}
|
|
||||||
if primary.done && fallback.done {
|
|
||||||
return nil, primary.error
|
|
||||||
}
|
|
||||||
if res.primary && fallbackTimer.Stop() {
|
|
||||||
// If we were able to stop the timer, that means it
|
|
||||||
// was running (hadn't yet started the fallback), but
|
|
||||||
// we just got an error on the primary path, so start
|
|
||||||
// the fallback immediately (in 0 nanoseconds).
|
|
||||||
fallbackTimer.Reset(0)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *dialer) resolveHostIPs(ctx context.Context, address string, network string) ([]netip.AddrPort, error) {
|
|
||||||
host, port, err := net.SplitHostPort(address)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("invalid address format: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
portnum, err := d.resolver.LookupPort(ctx, network, port)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("invalid port format: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
ips, err := d.resolver.LookupHost(ctx, host)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to resolve host address: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(ips) == 0 {
|
|
||||||
return nil, fmt.Errorf("failed to resolve address for [%s]%s", network, address)
|
|
||||||
}
|
|
||||||
|
|
||||||
var ipAddrs []netip.Addr
|
|
||||||
for _, ip := range ips {
|
|
||||||
ipAddr, err := netip.ParseAddr(ip)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to parse ip address '%s': %w", ip, err)
|
|
||||||
}
|
|
||||||
ipAddrs = append(ipAddrs, ipAddr)
|
|
||||||
}
|
|
||||||
|
|
||||||
var addrPorts []netip.AddrPort
|
|
||||||
for _, ipAddr := range ipAddrs {
|
|
||||||
addrPorts = append(addrPorts, netip.AddrPortFrom(ipAddr, uint16(portnum)))
|
|
||||||
}
|
|
||||||
return addrPorts, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *dialer) dialAddr(ctx context.Context, network, address string, addr netip.AddrPort) (net.Conn, error) {
|
|
||||||
d.mtx.RLock()
|
|
||||||
defer d.mtx.RUnlock()
|
|
||||||
|
|
||||||
for i := range d.subnets {
|
|
||||||
if d.subnets[i].Prefix.Contains(addr.Addr()) {
|
|
||||||
return d.balancer.DialContext(ctx, &d.subnets[i], network, address)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if d.restrict {
|
|
||||||
return nil, fmt.Errorf("no suitable interface for: [%s]%s", network, address)
|
|
||||||
}
|
|
||||||
return d.dialContext(ctx, &d.dialer, network, address)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *dialer) dialContext(ctx context.Context, nd *net.Dialer, network, address string) (net.Conn, error) {
|
|
||||||
var conn net.Conn
|
|
||||||
var err error
|
|
||||||
if h := d.customDialContext; h != nil {
|
|
||||||
conn, err = h(nd, ctx, network, address)
|
|
||||||
} else {
|
|
||||||
conn, err = nd.DialContext(ctx, network, address)
|
|
||||||
}
|
|
||||||
d.eh.DialPerformed(nd.LocalAddr, network, address, err)
|
|
||||||
return conn, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// splitByType divides an address list into two categories:
|
|
||||||
// the first address, and any with same type, are returned as
|
|
||||||
// primaries, while addresses with the opposite type are returned
|
|
||||||
// as fallbacks.
|
|
||||||
func splitByType(addrs []netip.AddrPort) (primaries []netip.AddrPort, fallbacks []netip.AddrPort) {
|
|
||||||
var primaryLabel bool
|
|
||||||
for i, addr := range addrs {
|
|
||||||
label := addr.Addr().Is4()
|
|
||||||
if i == 0 || label == primaryLabel {
|
|
||||||
primaryLabel = label
|
|
||||||
primaries = append(primaries, addr)
|
|
||||||
} else {
|
|
||||||
fallbacks = append(fallbacks, addr)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
type noopEventHandler struct{}
|
|
||||||
|
|
||||||
func (s noopEventHandler) DialPerformed(net.Addr, string, string, error) {}
|
|
|
@ -1,261 +0,0 @@
|
||||||
package multinet
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"net"
|
|
||||||
"net/netip"
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/stretchr/testify/require"
|
|
||||||
"golang.org/x/net/dns/dnsmessage"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestHostnameResolveIPv4(t *testing.T) {
|
|
||||||
resolvedAddr := "10.11.12.180:8080"
|
|
||||||
resolved := false
|
|
||||||
d, err := NewDialer(Config{
|
|
||||||
Subnets: []Subnet{
|
|
||||||
{
|
|
||||||
Prefix: netip.MustParsePrefix("10.11.12.0/24"),
|
|
||||||
SourceIPs: []netip.Addr{
|
|
||||||
netip.MustParseAddr("10.11.12.101"),
|
|
||||||
netip.MustParseAddr("10.11.12.102"),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
DialContext: func(d *net.Dialer, ctx context.Context, network, address string) (net.Conn, error) {
|
|
||||||
if resolvedAddr == address {
|
|
||||||
resolved = true
|
|
||||||
return &testConnStub{}, nil
|
|
||||||
}
|
|
||||||
if network == "udp" {
|
|
||||||
return &testDnsConn{
|
|
||||||
wantName: "s01.storage.devnev.",
|
|
||||||
ipv4: []byte{10, 11, 12, 180},
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
panic("unexpected call")
|
|
||||||
},
|
|
||||||
})
|
|
||||||
require.NoError(t, err)
|
|
||||||
conn, err := d.DialContext(context.Background(), "tcp", "s01.storage.devnev:8080")
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.NoError(t, conn.Close())
|
|
||||||
require.True(t, resolved)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestHostnameResolveIPv6(t *testing.T) {
|
|
||||||
resolvedAddr := "[2001:db8:85a3:8d3:1319:8a2e:370:8195]:8080"
|
|
||||||
ipv6 := net.ParseIP("2001:db8:85a3:8d3:1319:8a2e:370:8195")
|
|
||||||
resolved := false
|
|
||||||
d, err := NewDialer(Config{
|
|
||||||
Subnets: []Subnet{
|
|
||||||
{
|
|
||||||
Prefix: netip.MustParsePrefix("2001:db8:85a3:8d3::/64"),
|
|
||||||
SourceIPs: []netip.Addr{
|
|
||||||
netip.MustParseAddr("2001:db8:85a3:8d3:1319:8a2e:370:7348"),
|
|
||||||
netip.MustParseAddr("2001:db8:85a3:8d3:1319:8a2e:370:8192"),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
DialContext: func(d *net.Dialer, ctx context.Context, network, address string) (net.Conn, error) {
|
|
||||||
if resolvedAddr == address {
|
|
||||||
resolved = true
|
|
||||||
return &testConnStub{}, nil
|
|
||||||
}
|
|
||||||
if network == "udp" {
|
|
||||||
return &testDnsConn{
|
|
||||||
wantName: "s01.storage.devnev.",
|
|
||||||
ipv6: ipv6,
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
panic("unexpected call")
|
|
||||||
},
|
|
||||||
})
|
|
||||||
require.NoError(t, err)
|
|
||||||
conn, err := d.DialContext(context.Background(), "tcp", "s01.storage.devnev:8080")
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.NoError(t, conn.Close())
|
|
||||||
require.True(t, resolved)
|
|
||||||
}
|
|
||||||
|
|
||||||
type testDnsConn struct {
|
|
||||||
wantName string
|
|
||||||
ipv4 []byte
|
|
||||||
ipv6 []byte
|
|
||||||
reqID uint16
|
|
||||||
requested bool
|
|
||||||
secondRead bool
|
|
||||||
questions []dnsmessage.Question
|
|
||||||
}
|
|
||||||
|
|
||||||
// Close implements net.Conn.
|
|
||||||
func (*testDnsConn) Close() error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// LocalAddr implements net.Conn.
|
|
||||||
func (*testDnsConn) LocalAddr() net.Addr {
|
|
||||||
panic("unimplemented")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Read implements net.Conn.
|
|
||||||
func (c *testDnsConn) Read(b []byte) (n int, err error) {
|
|
||||||
if c.secondRead {
|
|
||||||
data, err := c.data()
|
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
copy(b, data)
|
|
||||||
return len(data), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
data, err := c.data()
|
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
l := len(data)
|
|
||||||
b[0] = byte(l >> 8)
|
|
||||||
b[1] = byte(l)
|
|
||||||
c.secondRead = true
|
|
||||||
return 2, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *testDnsConn) data() ([]byte, error) {
|
|
||||||
if !c.requested {
|
|
||||||
builder := dnsmessage.NewBuilder(nil, dnsmessage.Header{RCode: dnsmessage.RCodeNameError})
|
|
||||||
res, err := builder.Finish()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return res, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
msg := dnsmessage.Message{
|
|
||||||
Header: dnsmessage.Header{Response: true, Authoritative: true, RCode: dnsmessage.RCodeSuccess, ID: c.reqID},
|
|
||||||
Questions: c.questions,
|
|
||||||
Answers: []dnsmessage.Resource{},
|
|
||||||
}
|
|
||||||
c.appendIPv4(&msg)
|
|
||||||
c.appendIPV6(&msg)
|
|
||||||
return msg.Pack()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *testDnsConn) appendIPv4(msg *dnsmessage.Message) {
|
|
||||||
if c.ipv4 != nil {
|
|
||||||
msg.Answers = append(msg.Answers, dnsmessage.Resource{
|
|
||||||
Header: dnsmessage.ResourceHeader{
|
|
||||||
Name: dnsmessage.MustNewName(c.wantName),
|
|
||||||
Type: dnsmessage.TypeA,
|
|
||||||
Class: dnsmessage.ClassINET,
|
|
||||||
},
|
|
||||||
Body: &dnsmessage.AResource{A: [4]byte(c.ipv4)},
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *testDnsConn) appendIPV6(msg *dnsmessage.Message) {
|
|
||||||
if c.ipv6 != nil {
|
|
||||||
msg.Answers = append(msg.Answers, dnsmessage.Resource{
|
|
||||||
Header: dnsmessage.ResourceHeader{
|
|
||||||
Name: dnsmessage.MustNewName(c.wantName),
|
|
||||||
Type: dnsmessage.TypeAAAA,
|
|
||||||
Class: dnsmessage.ClassINET,
|
|
||||||
},
|
|
||||||
Body: &dnsmessage.AAAAResource{AAAA: [16]byte(c.ipv6)},
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// RemoteAddr implements net.Conn.
|
|
||||||
func (*testDnsConn) RemoteAddr() net.Addr {
|
|
||||||
panic("unimplemented")
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetDeadline implements net.Conn.
|
|
||||||
func (*testDnsConn) SetDeadline(t time.Time) error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetReadDeadline implements net.Conn.
|
|
||||||
func (*testDnsConn) SetReadDeadline(t time.Time) error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetWriteDeadline implements net.Conn.
|
|
||||||
func (*testDnsConn) SetWriteDeadline(t time.Time) error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Write implements net.Conn.
|
|
||||||
func (c *testDnsConn) Write(b []byte) (n int, err error) {
|
|
||||||
var p dnsmessage.Parser
|
|
||||||
var h dnsmessage.Header
|
|
||||||
if h, err = p.Start(b[2:]); err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
|
|
||||||
c.questions, err = p.AllQuestions()
|
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, q := range c.questions {
|
|
||||||
qStr := q.Name.String()
|
|
||||||
if qStr != c.wantName {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
c.requested = true
|
|
||||||
c.reqID = h.ID
|
|
||||||
if err := p.SkipAllQuestions(); err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
return len(b), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
type testConnStub struct{}
|
|
||||||
|
|
||||||
// Close implements net.Conn.
|
|
||||||
func (*testConnStub) Close() error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// LocalAddr implements net.Conn.
|
|
||||||
func (*testConnStub) LocalAddr() net.Addr {
|
|
||||||
panic("unimplemented")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Read implements net.Conn.
|
|
||||||
func (*testConnStub) Read(b []byte) (n int, err error) {
|
|
||||||
panic("unimplemented")
|
|
||||||
}
|
|
||||||
|
|
||||||
// RemoteAddr implements net.Conn.
|
|
||||||
func (*testConnStub) RemoteAddr() net.Addr {
|
|
||||||
panic("unimplemented")
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetDeadline implements net.Conn.
|
|
||||||
func (*testConnStub) SetDeadline(t time.Time) error {
|
|
||||||
panic("unimplemented")
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetReadDeadline implements net.Conn.
|
|
||||||
func (*testConnStub) SetReadDeadline(t time.Time) error {
|
|
||||||
panic("unimplemented")
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetWriteDeadline implements net.Conn.
|
|
||||||
func (*testConnStub) SetWriteDeadline(t time.Time) error {
|
|
||||||
panic("unimplemented")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Write implements net.Conn.
|
|
||||||
func (*testConnStub) Write(b []byte) (n int, err error) {
|
|
||||||
panic("unimplemented")
|
|
||||||
}
|
|
|
@ -1,39 +0,0 @@
|
||||||
package multinet
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"net/netip"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/stretchr/testify/require"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestNoSourceIPs(t *testing.T) {
|
|
||||||
t.Run("noop balancer", func(t *testing.T) {
|
|
||||||
d, err := NewDialer(Config{
|
|
||||||
Subnets: []Subnet{
|
|
||||||
{
|
|
||||||
Prefix: netip.MustParsePrefix("10.11.12.0/24"),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
})
|
|
||||||
require.NoError(t, err)
|
|
||||||
conn, err := d.DialContext(context.Background(), "tcp", "10.11.12.254:8080")
|
|
||||||
require.ErrorIs(t, err, errNoSuitableNodeFound)
|
|
||||||
require.Nil(t, conn)
|
|
||||||
})
|
|
||||||
t.Run("round robin balancer", func(t *testing.T) {
|
|
||||||
d, err := NewDialer(Config{
|
|
||||||
Subnets: []Subnet{
|
|
||||||
{
|
|
||||||
Prefix: netip.MustParsePrefix("10.11.12.0/24"),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
Balancer: BalancerTypeRoundRobin,
|
|
||||||
})
|
|
||||||
require.NoError(t, err)
|
|
||||||
conn, err := d.DialContext(context.Background(), "tcp", "10.11.12.254:8080")
|
|
||||||
require.ErrorIs(t, err, errNoSuitableNodeFound)
|
|
||||||
require.Nil(t, conn)
|
|
||||||
})
|
|
||||||
}
|
|
14
go.mod
14
go.mod
|
@ -1,14 +0,0 @@
|
||||||
module git.frostfs.info/TrueCloudLab/multinet
|
|
||||||
|
|
||||||
go 1.22
|
|
||||||
|
|
||||||
require (
|
|
||||||
github.com/stretchr/testify v1.9.0
|
|
||||||
golang.org/x/net v0.26.0
|
|
||||||
)
|
|
||||||
|
|
||||||
require (
|
|
||||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
|
||||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
|
||||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
|
||||||
)
|
|
12
go.sum
12
go.sum
|
@ -1,12 +0,0 @@
|
||||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
|
||||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
|
||||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
|
||||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
|
||||||
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
|
|
||||||
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
|
||||||
golang.org/x/net v0.26.0 h1:soB7SVo0PWrY4vPW/+ay0jKDNScG2X9wFeYlXIvJsOQ=
|
|
||||||
golang.org/x/net v0.26.0/go.mod h1:5YKkiSynbBIh3p6iOc/vibscux0x38BZDkn8sCUPxHE=
|
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
|
||||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
|
||||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
|
Loading…
Add table
Reference in a new issue