Skip to content

Commit

Permalink
chore: add 0.1ms tier and fix tiers
Browse files Browse the repository at this point in the history
- Add new <= 0.1m tier.
- Fix tier selection and add tests. Remove tier duplicate.
- Update deps, run rekres.

Signed-off-by: Dmitriy Matrenichev <[email protected]>
  • Loading branch information
DmitriyMV committed Jun 14, 2023
1 parent 5301800 commit 574126c
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 35 deletions.
4 changes: 2 additions & 2 deletions .drone.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
# THIS FILE WAS AUTOMATICALLY GENERATED, PLEASE DO NOT EDIT.
#
# Generated on 2023-05-08T18:28:37Z by kres latest.
# Generated on 2023-06-14T00:34:22Z by kres latest.

kind: pipeline
type: kubernetes
Expand Down Expand Up @@ -174,7 +174,7 @@ steps:

services:
- name: docker
image: docker:23.0-dind
image: docker:24.0-dind
entrypoint:
- dockerd
commands:
Expand Down
15 changes: 9 additions & 6 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# THIS FILE WAS AUTOMATICALLY GENERATED, PLEASE DO NOT EDIT.
#
# Generated on 2023-05-08T18:28:37Z by kres latest.
# Generated on 2023-06-14T00:34:22Z by kres latest.

# options for analysis running
run:
Expand Down Expand Up @@ -36,7 +36,7 @@ linters-settings:
lines: 60
statements: 40
gci:
local-prefixes: github.com/siderolabs/go-loadbalancer
local-prefixes: github.com/siderolabs/go-loadbalancer/
gocognit:
min-complexity: 30
ireturn:
Expand Down Expand Up @@ -65,7 +65,7 @@ linters-settings:
gofmt:
simplify: true
goimports:
local-prefixes: github.com/siderolabs/go-loadbalancer
local-prefixes: github.com/siderolabs/go-loadbalancer/
golint:
min-confidence: 0.8
gomnd:
Expand All @@ -74,9 +74,6 @@ linters-settings:
govet:
check-shadowing: true
enable-all: true
depguard:
list-type: blacklist
include-go-root: false
lll:
line-length: 200
tab-width: 4
Expand Down Expand Up @@ -118,6 +115,10 @@ linters-settings:
cyclop:
# the maximal code complexity to report
max-complexity: 20
# depguard:
# Main:
# deny:
# - github.com/OpenPeeDeeP/depguard # this is just an example

linters:
enable-all: true
Expand Down Expand Up @@ -145,6 +146,8 @@ linters:
- typecheck
- varnamelen
- wrapcheck
- depguard # Disabled because starting with golangci-lint 1.53.0 it doesn't allow denylist alone anymore
- tagalign
# abandoned linters for which golangci shows the warning that the repo is archived by the owner
- interfacer
- maligned
Expand Down
29 changes: 17 additions & 12 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

# THIS FILE WAS AUTOMATICALLY GENERATED, PLEASE DO NOT EDIT.
#
# Generated on 2023-05-17T09:08:38Z by kres latest.
# Generated on 2023-06-14T00:34:22Z by kres latest.

ARG TOOLCHAIN

# cleaned up specs and compiled versions
FROM scratch AS generate

# runs markdownlint
FROM docker.io/node:20.2.0-alpine3.17 AS lint-markdown
FROM docker.io/node:20.3.0-alpine3.18 AS lint-markdown
WORKDIR /src
RUN npm i -g [email protected]
RUN npm i [email protected]
Expand All @@ -28,26 +28,27 @@ ENV GO111MODULE on
ARG CGO_ENABLED
ENV CGO_ENABLED ${CGO_ENABLED}
ENV GOPATH /go
ARG DEEPCOPY_VERSION
RUN --mount=type=cache,target=/root/.cache/go-build --mount=type=cache,target=/go/pkg go install github.com/siderolabs/deep-copy@${DEEPCOPY_VERSION} \
&& mv /go/bin/deep-copy /bin/deep-copy
ARG GOLANGCILINT_VERSION
RUN --mount=type=cache,target=/root/.cache/go-build --mount=type=cache,target=/go/pkg go install github.com/golangci/golangci-lint/cmd/golangci-lint@${GOLANGCILINT_VERSION} \
&& mv /go/bin/golangci-lint /bin/golangci-lint
ARG GOFUMPT_VERSION
RUN go install mvdan.cc/gofumpt@${GOFUMPT_VERSION} \
&& mv /go/bin/gofumpt /bin/gofumpt
RUN --mount=type=cache,target=/root/.cache/go-build --mount=type=cache,target=/go/pkg go install golang.org/x/vuln/cmd/govulncheck@latest \
&& mv /go/bin/govulncheck /bin/govulncheck
ARG GOIMPORTS_VERSION
RUN --mount=type=cache,target=/root/.cache/go-build --mount=type=cache,target=/go/pkg go install golang.org/x/tools/cmd/goimports@${GOIMPORTS_VERSION} \
&& mv /go/bin/goimports /bin/goimports
ARG DEEPCOPY_VERSION
RUN --mount=type=cache,target=/root/.cache/go-build --mount=type=cache,target=/go/pkg go install github.com/siderolabs/deep-copy@${DEEPCOPY_VERSION} \
&& mv /go/bin/deep-copy /bin/deep-copy
ARG GOFUMPT_VERSION
RUN go install mvdan.cc/gofumpt@${GOFUMPT_VERSION} \
&& mv /go/bin/gofumpt /bin/gofumpt

# tools and sources
FROM tools AS base
WORKDIR /src
COPY ./go.mod .
COPY ./go.sum .
COPY go.mod go.mod
COPY go.sum go.sum
RUN cd .
RUN --mount=type=cache,target=/go/pkg go mod download
RUN --mount=type=cache,target=/go/pkg go mod verify
COPY ./controlplane ./controlplane
Expand All @@ -61,28 +62,32 @@ RUN FILES="$(gofumpt -l .)" && test -z "${FILES}" || (echo -e "Source code is no

# runs goimports
FROM base AS lint-goimports
RUN FILES="$(goimports -l -local github.com/siderolabs/go-loadbalancer .)" && test -z "${FILES}" || (echo -e "Source code is not formatted with 'goimports -w -local github.com/siderolabs/go-loadbalancer .':\n${FILES}"; exit 1)
RUN FILES="$(goimports -l -local github.com/siderolabs/go-loadbalancer/ .)" && test -z "${FILES}" || (echo -e "Source code is not formatted with 'goimports -w -local github.com/siderolabs/go-loadbalancer/ .':\n${FILES}"; exit 1)

# runs golangci-lint
FROM base AS lint-golangci-lint
WORKDIR /src
COPY .golangci.yml .
ENV GOGC 50
RUN --mount=type=cache,target=/root/.cache/go-build --mount=type=cache,target=/root/.cache/golangci-lint --mount=type=cache,target=/go/pkg golangci-lint run --config .golangci.yml

# runs govulncheck
FROM base AS lint-govulncheck
WORKDIR /src
RUN --mount=type=cache,target=/root/.cache/go-build --mount=type=cache,target=/go/pkg govulncheck ./...

# runs unit-tests with race detector
FROM base AS unit-tests-race
WORKDIR /src
ARG TESTPKGS
RUN --mount=type=cache,target=/root/.cache/go-build --mount=type=cache,target=/go/pkg --mount=type=cache,target=/tmp CGO_ENABLED=1 go test -v -race -count 1 ${TESTPKGS}

# runs unit-tests
FROM base AS unit-tests-run
WORKDIR /src
ARG TESTPKGS
RUN --mount=type=cache,target=/root/.cache/go-build --mount=type=cache,target=/go/pkg --mount=type=cache,target=/tmp go test -v -covermode=atomic -coverprofile=coverage.txt -coverpkg=${TESTPKGS} -count 1 ${TESTPKGS}

FROM scratch AS unit-tests
COPY --from=unit-tests-run /src/coverage.txt /coverage.txt
COPY --from=unit-tests-run /src/coverage.txt /coverage-unit-tests.txt

20 changes: 10 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# THIS FILE WAS AUTOMATICALLY GENERATED, PLEASE DO NOT EDIT.
#
# Generated on 2023-05-17T09:08:38Z by kres latest.
# Generated on 2023-06-14T00:34:22Z by kres latest.

# common variables

Expand All @@ -13,15 +13,15 @@ WITH_RACE ?= false
REGISTRY ?= ghcr.io
USERNAME ?= siderolabs
REGISTRY_AND_USERNAME ?= $(REGISTRY)/$(USERNAME)
GOLANGCILINT_VERSION ?= v1.52.2
GOFUMPT_VERSION ?= v0.5.0
GO_VERSION ?= 1.20
GOIMPORTS_VERSION ?= v0.9.1
PROTOBUF_GO_VERSION ?= 1.28.1
GRPC_GO_VERSION ?= 1.3.0
GRPC_GATEWAY_VERSION ?= 2.15.2
GRPC_GATEWAY_VERSION ?= 2.16.0
VTPROTOBUF_VERSION ?= 0.4.0
DEEPCOPY_VERSION ?= v0.5.5
GOLANGCILINT_VERSION ?= v1.53.2
GOFUMPT_VERSION ?= v0.5.0
GO_VERSION ?= 1.20
GOIMPORTS_VERSION ?= v0.9.3
GO_BUILDFLAGS ?=
GO_LDFLAGS ?=
CGO_ENABLED ?= 0
Expand Down Expand Up @@ -49,14 +49,14 @@ COMMON_ARGS += --build-arg=TOOLCHAIN="$(TOOLCHAIN)"
COMMON_ARGS += --build-arg=CGO_ENABLED="$(CGO_ENABLED)"
COMMON_ARGS += --build-arg=GO_BUILDFLAGS="$(GO_BUILDFLAGS)"
COMMON_ARGS += --build-arg=GO_LDFLAGS="$(GO_LDFLAGS)"
COMMON_ARGS += --build-arg=GOLANGCILINT_VERSION="$(GOLANGCILINT_VERSION)"
COMMON_ARGS += --build-arg=GOFUMPT_VERSION="$(GOFUMPT_VERSION)"
COMMON_ARGS += --build-arg=GOIMPORTS_VERSION="$(GOIMPORTS_VERSION)"
COMMON_ARGS += --build-arg=PROTOBUF_GO_VERSION="$(PROTOBUF_GO_VERSION)"
COMMON_ARGS += --build-arg=GRPC_GO_VERSION="$(GRPC_GO_VERSION)"
COMMON_ARGS += --build-arg=GRPC_GATEWAY_VERSION="$(GRPC_GATEWAY_VERSION)"
COMMON_ARGS += --build-arg=VTPROTOBUF_VERSION="$(VTPROTOBUF_VERSION)"
COMMON_ARGS += --build-arg=DEEPCOPY_VERSION="$(DEEPCOPY_VERSION)"
COMMON_ARGS += --build-arg=GOLANGCILINT_VERSION="$(GOLANGCILINT_VERSION)"
COMMON_ARGS += --build-arg=GOIMPORTS_VERSION="$(GOIMPORTS_VERSION)"
COMMON_ARGS += --build-arg=GOFUMPT_VERSION="$(GOFUMPT_VERSION)"
COMMON_ARGS += --build-arg=TESTPKGS="$(TESTPKGS)"
TOOLCHAIN ?= docker.io/golang:1.20-alpine

Expand Down Expand Up @@ -150,7 +150,7 @@ unit-tests-race: ## Performs unit tests with race detection enabled.

.PHONY: coverage
coverage: ## Upload coverage data to codecov.io.
bash -c "bash <(curl -s https://codecov.io/bash) -f $(ARTIFACTS)/coverage.txt -X fix"
bash -c "bash <(curl -s https://codecov.io/bash) -f $(ARTIFACTS)/coverage-unit-tests.txt -X fix"

.PHONY: lint-markdown
lint-markdown: ## Runs markdownlint.
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/siderolabs/gen v0.4.5
github.com/siderolabs/go-retry v0.3.2
github.com/siderolabs/tcpproxy v0.1.0
github.com/stretchr/testify v1.8.3
github.com/stretchr/testify v1.8.4
go.uber.org/goleak v1.2.1
go.uber.org/zap v1.24.0
)
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ github.com/siderolabs/tcpproxy v0.1.0 h1:IbkS9vRhjMOscc1US3M5P1RnsGKFgB6U5IzUk+4
github.com/siderolabs/tcpproxy v0.1.0/go.mod h1:onn6CPPj/w1UNqQ0U97oRPF0CqbrgEApYCw4P9IiCW8=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.8.3 h1:RP3t2pwF7cMEbC1dqtB6poj3niw/9gnV4Cjg5oW5gtY=
github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw=
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/goleak v1.2.1 h1:NBol2c7O1ZokfZ0LEU9K6Whx/KnwvepVetCUhtKja4A=
Expand Down
13 changes: 13 additions & 0 deletions loadbalancer/export_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

package loadbalancer

import (
"time"

"github.com/siderolabs/go-loadbalancer/upstream"
)

func CalcTier(err error, elapsed time.Duration) (upstream.Tier, error) { return calcTier(err, elapsed) }
3 changes: 2 additions & 1 deletion loadbalancer/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ func (upstream node) healthCheck(ctx context.Context) error {
}

var mins = []time.Duration{
0, time.Millisecond,
0,
time.Millisecond / 10,
time.Millisecond,
10 * time.Millisecond,
100 * time.Millisecond,
Expand Down
46 changes: 46 additions & 0 deletions loadbalancer/node_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

package loadbalancer_test

import (
"testing"
"time"

"github.com/siderolabs/go-loadbalancer/loadbalancer"
"github.com/siderolabs/go-loadbalancer/upstream"
)

func Test_calcTier(t *testing.T) {
t.Parallel()

tests := map[string]struct {
want upstream.Tier
elapsed time.Duration
}{
"Microsecond": {want: upstream.Tier(0), elapsed: time.Microsecond},
"100 Microseconds": {want: upstream.Tier(1), elapsed: 100 * time.Microsecond},
"Millisecond": {want: upstream.Tier(2), elapsed: time.Millisecond},
"10 Milliseconds": {want: upstream.Tier(3), elapsed: 10 * time.Millisecond},
"100 Milliseconds": {want: upstream.Tier(4), elapsed: 100 * time.Millisecond},
"1 Second": {want: upstream.Tier(5), elapsed: time.Second},
}
for name, tt := range tests {
tt := tt

t.Run(name, func(t *testing.T) {
t.Parallel()

got, err := loadbalancer.CalcTier(nil, tt.elapsed)
if err != nil {
t.Errorf("calcTier() error = %v", err)

return
}
if got != tt.want {
t.Errorf("calcTier() got = %v, want %v", got, tt.want)
}
})
}
}
2 changes: 1 addition & 1 deletion upstream/upstream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ func (suite *ListSuite) TestBalancing() {
upstream.WithScoreDeltas(-1, 1),
upstream.WithHealthcheckInterval(10*time.Microsecond),
upstream.WithHealthcheckTimeout(time.Hour),
upstream.WithTiers(0, 4, 1),
upstream.WithTiers(0, 5, 1),
)
suite.Require().NoError(lerr)

Expand Down

0 comments on commit 574126c

Please sign in to comment.