From 7c7ccc3d973621b2fa7adfef10241ecc1f7a644d Mon Sep 17 00:00:00 2001 From: Andrey Smirnov Date: Mon, 28 Nov 2022 19:24:00 +0400 Subject: [PATCH] feat: introduce channel SendWithContext This is a frequent pattern in our codebase, so DRY it up. Signed-off-by: Andrey Smirnov --- .dockerignore | 8 ++++-- Dockerfile | 17 ++++++++++-- Makefile | 65 ++++++++++++++++++++++++++++++-------------- channel/send.go | 20 ++++++++++++++ channel/send_test.go | 28 +++++++++++++++++++ go.mod | 2 +- go.sum | 4 ++- 7 files changed, 116 insertions(+), 28 deletions(-) create mode 100644 channel/send.go create mode 100644 channel/send_test.go diff --git a/.dockerignore b/.dockerignore index 23fdfc1..30fb91f 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,13 +1,17 @@ # THIS FILE WAS AUTOMATICALLY GENERATED, PLEASE DO NOT EDIT. # -# Generated on 2022-09-21T14:24:31Z by kres 943ad34. +# Generated on 2022-11-28T15:27:14Z by kres 3ac53a8. -** +* +!channel !containers !maps !optional +!pair !slices !value +!xerrors +!xsync !go.mod !go.sum !.golangci.yml diff --git a/Dockerfile b/Dockerfile index 3c375d9..ad4ad53 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,7 +2,7 @@ # THIS FILE WAS AUTOMATICALLY GENERATED, PLEASE DO NOT EDIT. # -# Generated on 2022-09-21T14:34:32Z by kres latest. +# Generated on 2022-11-28T15:27:14Z by kres 3ac53a8. ARG TOOLCHAIN @@ -10,7 +10,7 @@ ARG TOOLCHAIN FROM scratch AS generate # runs markdownlint -FROM docker.io/node:18.9.0-alpine3.16 AS lint-markdown +FROM docker.io/node:19.0.1-alpine3.16 AS lint-markdown WORKDIR /src RUN npm i -g markdownlint-cli@0.32.2 RUN npm i sentences-per-line@0.2.1 @@ -25,7 +25,8 @@ RUN apk --update --no-cache add bash curl build-base protoc protobuf-dev # build tools FROM --platform=${BUILDPLATFORM} toolchain AS tools ENV GO111MODULE on -ENV CGO_ENABLED 0 +ARG CGO_ENABLED +ENV CGO_ENABLED ${CGO_ENABLED} ENV GOPATH /go ARG GOLANGCILINT_VERSION RUN go install github.com/golangci/golangci-lint/cmd/golangci-lint@${GOLANGCILINT_VERSION} \ @@ -33,6 +34,8 @@ RUN go install github.com/golangci/golangci-lint/cmd/golangci-lint@${GOLANGCILIN ARG GOFUMPT_VERSION RUN go install mvdan.cc/gofumpt@${GOFUMPT_VERSION} \ && mv /go/bin/gofumpt /bin/gofumpt +RUN go install golang.org/x/vuln/cmd/govulncheck@latest \ + && mv /go/bin/govulncheck /bin/govulncheck ARG GOIMPORTS_VERSION RUN go install golang.org/x/tools/cmd/goimports@${GOIMPORTS_VERSION} \ && mv /go/bin/goimports /bin/goimports @@ -47,11 +50,15 @@ COPY ./go.mod . COPY ./go.sum . RUN --mount=type=cache,target=/go/pkg go mod download RUN --mount=type=cache,target=/go/pkg go mod verify +COPY ./channel ./channel COPY ./containers ./containers COPY ./maps ./maps COPY ./optional ./optional +COPY ./pair ./pair COPY ./slices ./slices COPY ./value ./value +COPY ./xerrors ./xerrors +COPY ./xsync ./xsync RUN --mount=type=cache,target=/go/pkg go list -mod=readonly all >/dev/null # runs gofumpt @@ -68,6 +75,10 @@ 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 +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 ARG TESTPKGS diff --git a/Makefile b/Makefile index 494ce1e..b548ee5 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ # THIS FILE WAS AUTOMATICALLY GENERATED, PLEASE DO NOT EDIT. # -# Generated on 2022-09-21T14:24:31Z by kres 943ad34. +# Generated on 2022-11-28T14:51:36Z by kres 3ac53a8. # common variables @@ -8,18 +8,23 @@ SHA := $(shell git describe --match=none --always --abbrev=8 --dirty) TAG := $(shell git describe --tag --always --dirty) BRANCH := $(shell git rev-parse --abbrev-ref HEAD) ARTIFACTS := _out +WITH_DEBUG ?= false +WITH_RACE ?= false REGISTRY ?= ghcr.io USERNAME ?= siderolabs REGISTRY_AND_USERNAME ?= $(REGISTRY)/$(USERNAME) -GOLANGCILINT_VERSION ?= v1.49.0 -GOFUMPT_VERSION ?= v0.3.1 +GOLANGCILINT_VERSION ?= v1.50.1 +GOFUMPT_VERSION ?= v0.4.0 GO_VERSION ?= 1.19 -GOIMPORTS_VERSION ?= v0.1.12 +GOIMPORTS_VERSION ?= v0.3.0 PROTOBUF_GO_VERSION ?= 1.28.1 GRPC_GO_VERSION ?= 1.2.0 -GRPC_GATEWAY_VERSION ?= 2.11.3 +GRPC_GATEWAY_VERSION ?= 2.14.0 VTPROTOBUF_VERSION ?= 0.3.0 DEEPCOPY_VERSION ?= v0.5.5 +GO_BUILDFLAGS ?= +GO_LDFLAGS ?= +CGO_ENABLED ?= 0 TESTPKGS ?= ./... KRES_IMAGE ?= ghcr.io/siderolabs/kres:latest CONFORMANCE_IMAGE ?= ghcr.io/siderolabs/conform:latest @@ -35,21 +40,24 @@ COMMON_ARGS = --file=Dockerfile COMMON_ARGS += --progress=$(PROGRESS) COMMON_ARGS += --platform=$(PLATFORM) COMMON_ARGS += --push=$(PUSH) -COMMON_ARGS += --build-arg=ARTIFACTS=$(ARTIFACTS) -COMMON_ARGS += --build-arg=SHA=$(SHA) -COMMON_ARGS += --build-arg=TAG=$(TAG) -COMMON_ARGS += --build-arg=USERNAME=$(USERNAME) -COMMON_ARGS += --build-arg=REGISTRY=$(REGISTRY) -COMMON_ARGS += --build-arg=TOOLCHAIN=$(TOOLCHAIN) -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=TESTPKGS=$(TESTPKGS) +COMMON_ARGS += --build-arg=ARTIFACTS="$(ARTIFACTS)" +COMMON_ARGS += --build-arg=SHA="$(SHA)" +COMMON_ARGS += --build-arg=TAG="$(TAG)" +COMMON_ARGS += --build-arg=USERNAME="$(USERNAME)" +COMMON_ARGS += --build-arg=REGISTRY="$(REGISTRY)" +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=TESTPKGS="$(TESTPKGS)" TOOLCHAIN ?= docker.io/golang:1.19-alpine # help menu @@ -85,6 +93,18 @@ respectively. endef +ifneq (, $(filter $(WITH_RACE), t true TRUE y yes 1)) +GO_BUILDFLAGS += -race +CGO_ENABLED := 1 +GO_LDFLAGS += -linkmode=external -extldflags '-static' +endif + +ifneq (, $(filter $(WITH_DEBUG), t true TRUE y yes 1)) +GO_BUILDFLAGS += -tags sidero.debug +else +GO_LDFLAGS += -s -w +endif + all: unit-tests lint .PHONY: clean @@ -110,6 +130,9 @@ fmt: ## Formats the source code go install mvdan.cc/gofumpt@$(GOFUMPT_VERSION) && \ gofumpt -w ." +lint-govulncheck: ## Runs govulncheck linter. + @$(MAKE) target-$@ + lint-goimports: ## Runs goimports linter. @$(MAKE) target-$@ @@ -134,7 +157,7 @@ lint-markdown: ## Runs markdownlint. @$(MAKE) target-$@ .PHONY: lint -lint: lint-golangci-lint lint-gofumpt lint-goimports lint-markdown ## Run all linters for the project. +lint: lint-golangci-lint lint-gofumpt lint-govulncheck lint-goimports lint-markdown ## Run all linters for the project. .PHONY: rekres rekres: diff --git a/channel/send.go b/channel/send.go new file mode 100644 index 0000000..5c0e1cb --- /dev/null +++ b/channel/send.go @@ -0,0 +1,20 @@ +// 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 channel provides generic operations on channels. +package channel + +import "context" + +// SendWithContext tries to send a value to a channel which is aborted if the context is canceled. +// +// Function returns true if the value was sent, false if the context was canceled. +func SendWithContext[T any](ctx context.Context, ch chan<- T, val T) bool { + select { + case <-ctx.Done(): + return false + case ch <- val: + return true + } +} diff --git a/channel/send_test.go b/channel/send_test.go new file mode 100644 index 0000000..e2788e2 --- /dev/null +++ b/channel/send_test.go @@ -0,0 +1,28 @@ +// 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 channel_test + +import ( + "context" + "testing" + + "github.com/stretchr/testify/assert" + + "github.com/siderolabs/gen/channel" +) + +func TestSendWithContext(t *testing.T) { + ch := make(chan int, 1) + ctx, cancel := context.WithCancel(context.Background()) + t.Cleanup(cancel) + + assert.True(t, channel.SendWithContext(ctx, ch, 42)) + assert.Equal(t, 42, <-ch) + + assert.True(t, channel.SendWithContext(ctx, ch, 69)) + + cancel() + assert.False(t, channel.SendWithContext(ctx, ch, 33)) +} diff --git a/go.mod b/go.mod index ba84460..dcc87a2 100644 --- a/go.mod +++ b/go.mod @@ -2,7 +2,7 @@ module github.com/siderolabs/gen go 1.19 -require github.com/stretchr/testify v1.8.0 +require github.com/stretchr/testify v1.8.1 require ( github.com/davecgh/go-spew v1.1.1 // indirect diff --git a/go.sum b/go.sum index 5164829..2ec90f7 100644 --- a/go.sum +++ b/go.sum @@ -5,9 +5,11 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= +github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= 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.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=