diff --git a/cmd/daprd/main.go b/cmd/daprd/main.go index e6c2fecd440..eb7b41bff5a 100644 --- a/cmd/daprd/main.go +++ b/cmd/daprd/main.go @@ -35,14 +35,13 @@ import ( secretstoresLoader "github.com/dapr/dapr/pkg/components/secretstores" stateLoader "github.com/dapr/dapr/pkg/components/state" workflowsLoader "github.com/dapr/dapr/pkg/components/workflows" - "github.com/dapr/dapr/pkg/concurrency" "github.com/dapr/dapr/pkg/modes" + "github.com/dapr/dapr/pkg/runtime" "github.com/dapr/dapr/pkg/runtime/registry" "github.com/dapr/dapr/pkg/security" - "github.com/dapr/dapr/pkg/signals" - - "github.com/dapr/dapr/pkg/runtime" + "github.com/dapr/kit/concurrency" "github.com/dapr/kit/logger" + "github.com/dapr/kit/signals" ) var ( diff --git a/cmd/injector/main.go b/cmd/injector/main.go index 4a38d562b35..02ef96669f1 100644 --- a/cmd/injector/main.go +++ b/cmd/injector/main.go @@ -24,16 +24,16 @@ import ( "github.com/dapr/dapr/cmd/injector/options" "github.com/dapr/dapr/pkg/buildinfo" scheme "github.com/dapr/dapr/pkg/client/clientset/versioned" - "github.com/dapr/dapr/pkg/concurrency" "github.com/dapr/dapr/pkg/health" "github.com/dapr/dapr/pkg/injector/sentry" "github.com/dapr/dapr/pkg/injector/service" "github.com/dapr/dapr/pkg/metrics" "github.com/dapr/dapr/pkg/modes" "github.com/dapr/dapr/pkg/security" - "github.com/dapr/dapr/pkg/signals" "github.com/dapr/dapr/utils" + "github.com/dapr/kit/concurrency" "github.com/dapr/kit/logger" + "github.com/dapr/kit/signals" ) var log = logger.NewLogger("dapr.injector") diff --git a/cmd/operator/main.go b/cmd/operator/main.go index f7d3c8b4a4d..3d72a32acc0 100644 --- a/cmd/operator/main.go +++ b/cmd/operator/main.go @@ -16,12 +16,12 @@ package main import ( "github.com/dapr/dapr/cmd/operator/options" "github.com/dapr/dapr/pkg/buildinfo" - "github.com/dapr/dapr/pkg/concurrency" "github.com/dapr/dapr/pkg/metrics" "github.com/dapr/dapr/pkg/operator" "github.com/dapr/dapr/pkg/operator/monitoring" - "github.com/dapr/dapr/pkg/signals" + "github.com/dapr/kit/concurrency" "github.com/dapr/kit/logger" + "github.com/dapr/kit/signals" ) var log = logger.NewLogger("dapr.operator") diff --git a/cmd/placement/main.go b/cmd/placement/main.go index 6c2ce01a7ce..5dc6a6fbe03 100644 --- a/cmd/placement/main.go +++ b/cmd/placement/main.go @@ -20,7 +20,6 @@ import ( "github.com/dapr/dapr/cmd/placement/options" "github.com/dapr/dapr/pkg/buildinfo" - "github.com/dapr/dapr/pkg/concurrency" "github.com/dapr/dapr/pkg/health" "github.com/dapr/dapr/pkg/metrics" "github.com/dapr/dapr/pkg/modes" @@ -29,8 +28,9 @@ import ( "github.com/dapr/dapr/pkg/placement/monitoring" "github.com/dapr/dapr/pkg/placement/raft" "github.com/dapr/dapr/pkg/security" - "github.com/dapr/dapr/pkg/signals" + "github.com/dapr/kit/concurrency" "github.com/dapr/kit/logger" + "github.com/dapr/kit/signals" ) var log = logger.NewLogger("dapr.placement") diff --git a/cmd/placement/options/options.go b/cmd/placement/options/options.go index 2d83ef281d1..e340e90697e 100644 --- a/cmd/placement/options/options.go +++ b/cmd/placement/options/options.go @@ -23,8 +23,8 @@ import ( "github.com/dapr/dapr/pkg/modes" "github.com/dapr/dapr/pkg/placement/raft" "github.com/dapr/dapr/pkg/security" - "github.com/dapr/dapr/utils" "github.com/dapr/kit/logger" + "github.com/dapr/kit/utils" ) const ( diff --git a/cmd/sentry/main.go b/cmd/sentry/main.go index 8540b2ba39f..36b71b90967 100644 --- a/cmd/sentry/main.go +++ b/cmd/sentry/main.go @@ -21,16 +21,16 @@ import ( "github.com/dapr/dapr/cmd/sentry/options" "github.com/dapr/dapr/pkg/buildinfo" - "github.com/dapr/dapr/pkg/concurrency" "github.com/dapr/dapr/pkg/health" "github.com/dapr/dapr/pkg/metrics" "github.com/dapr/dapr/pkg/sentry" "github.com/dapr/dapr/pkg/sentry/config" "github.com/dapr/dapr/pkg/sentry/monitoring" - "github.com/dapr/dapr/pkg/signals" "github.com/dapr/dapr/utils" + "github.com/dapr/kit/concurrency" "github.com/dapr/kit/fswatcher" "github.com/dapr/kit/logger" + "github.com/dapr/kit/signals" ) var log = logger.NewLogger("dapr.sentry") diff --git a/go.mod b/go.mod index 8cc046b7b90..8efe063b252 100644 --- a/go.mod +++ b/go.mod @@ -10,7 +10,7 @@ require ( github.com/cenkalti/backoff/v4 v4.2.1 github.com/cloudevents/sdk-go/v2 v2.14.0 github.com/dapr/components-contrib v1.12.0-rc.4.0.20231009175401-9f2cc5c158bb - github.com/dapr/kit v0.12.1 + github.com/dapr/kit v0.12.2-0.20231031211530-0e1fd37fc4b3 github.com/evanphx/json-patch/v5 v5.7.0 github.com/go-chi/chi/v5 v5.0.10 github.com/go-chi/cors v1.2.1 @@ -33,7 +33,7 @@ require ( github.com/jackc/pgx/v5 v5.4.3 github.com/jhump/protoreflect v1.15.2 github.com/kelseyhightower/envconfig v1.4.0 - github.com/lestrrat-go/jwx/v2 v2.0.13 + github.com/lestrrat-go/jwx/v2 v2.0.15 github.com/microsoft/durabletask-go v0.3.1 github.com/mitchellh/mapstructure v1.5.1-0.20220423185008-bf980b35cac4 github.com/phayes/freeport v0.0.0-20220201140144-74d24b5ae9f5 @@ -45,7 +45,6 @@ require ( github.com/spf13/cast v1.5.1 github.com/spiffe/go-spiffe/v2 v2.1.6 github.com/stretchr/testify v1.8.4 - github.com/tidwall/transform v0.0.0-20201103190739-32f242e2dbde github.com/valyala/fasthttp v1.49.0 go.mongodb.org/mongo-driver v1.12.1 go.opencensus.io v0.24.0 @@ -58,12 +57,12 @@ require ( go.opentelemetry.io/otel/trace v1.16.0 go.uber.org/automaxprocs v1.5.3 go.uber.org/ratelimit v0.3.0 - golang.org/x/crypto v0.13.0 - golang.org/x/exp v0.0.0-20230905200255-921286631fa9 - golang.org/x/net v0.15.0 - golang.org/x/sync v0.3.0 + golang.org/x/crypto v0.14.0 + golang.org/x/exp v0.0.0-20231006140011-7918f672742d + golang.org/x/net v0.16.0 + golang.org/x/sync v0.4.0 google.golang.org/genproto/googleapis/api v0.0.0-20230803162519-f966b187b2e5 - google.golang.org/genproto/googleapis/rpc v0.0.0-20230807174057-1744710a1577 + google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d google.golang.org/grpc v1.57.0 google.golang.org/protobuf v1.31.0 gopkg.in/yaml.v3 v3.0.1 @@ -192,7 +191,7 @@ require ( github.com/evanphx/json-patch v5.6.0+incompatible // indirect github.com/fasthttp-contrib/sessions v0.0.0-20160905201309-74f6ac73d5d5 // indirect github.com/fatih/color v1.15.0 // indirect - github.com/fsnotify/fsnotify v1.6.0 // indirect + github.com/fsnotify/fsnotify v1.7.0 // indirect github.com/ghodss/yaml v1.0.1-0.20190212211648-25d852aebe32 // indirect github.com/go-errors/errors v1.4.2 // indirect github.com/go-ini/ini v1.67.0 // indirect @@ -355,6 +354,7 @@ require ( github.com/tidwall/gjson v1.13.0 // indirect github.com/tidwall/match v1.1.1 // indirect github.com/tidwall/pretty v1.2.1 // indirect + github.com/tidwall/transform v0.0.0-20201103190739-32f242e2dbde // indirect github.com/tjfoc/gmsm v1.3.2 // indirect github.com/tklauser/go-sysconf v0.3.10 // indirect github.com/tklauser/numcpus v0.4.0 // indirect @@ -382,13 +382,13 @@ require ( go.uber.org/multierr v1.11.0 // indirect go.uber.org/zap v1.24.0 // indirect golang.org/x/arch v0.3.0 // indirect - golang.org/x/mod v0.12.0 // indirect + golang.org/x/mod v0.13.0 // indirect golang.org/x/oauth2 v0.12.0 // indirect - golang.org/x/sys v0.12.0 // indirect - golang.org/x/term v0.12.0 // indirect + golang.org/x/sys v0.13.0 // indirect + golang.org/x/term v0.13.0 // indirect golang.org/x/text v0.13.0 // indirect golang.org/x/time v0.3.0 // indirect - golang.org/x/tools v0.13.0 // indirect + golang.org/x/tools v0.14.0 // indirect golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect gomodules.xyz/jsonpatch/v2 v2.2.0 // indirect google.golang.org/api v0.138.0 // indirect diff --git a/go.sum b/go.sum index ec4192997f3..5d9e0a74a9d 100644 --- a/go.sum +++ b/go.sum @@ -393,8 +393,8 @@ github.com/danieljoos/wincred v1.1.2 h1:QLdCxFs1/Yl4zduvBdcHB8goaYk9RARS2SgLLRuA github.com/danieljoos/wincred v1.1.2/go.mod h1:GijpziifJoIBfYh+S7BbkdUTU4LfM+QnGqR5Vl2tAx0= github.com/dapr/components-contrib v1.12.0-rc.4.0.20231009175401-9f2cc5c158bb h1:A1A1A5IvxSTzKFlT/ZPVFpp9rOWqzd/yjzyNRxlbuHY= github.com/dapr/components-contrib v1.12.0-rc.4.0.20231009175401-9f2cc5c158bb/go.mod h1:SWa5Za44IumX+GeTVZvHe9McfpC8ay7/KvGVKHDu97Q= -github.com/dapr/kit v0.12.1 h1:XT0CJQQaKRYSzIzZo15O1PAHGUrMGoAavdFRcNVZ+UE= -github.com/dapr/kit v0.12.1/go.mod h1:eNYjsudq3Ij0x8CLWsPturHor56sZRNu5tk2hUiJT80= +github.com/dapr/kit v0.12.2-0.20231031211530-0e1fd37fc4b3 h1:xsmVK3YOKRMOcaxqo50Ce0apQzq+LzAfWuFapQuu8Ro= +github.com/dapr/kit v0.12.2-0.20231031211530-0e1fd37fc4b3/go.mod h1:c3Z78F+h7UYtb0LmpzJNC/ChT240ycDJFViRUztdpoo= github.com/dave/jennifer v1.4.0/go.mod h1:fIb+770HOpJ2fmN9EPPKOqm1vMGhB+TwXKMZhrIygKg= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= @@ -502,8 +502,8 @@ github.com/frankban/quicktest v1.14.4 h1:g2rn0vABPOOXmZUj+vbmUp0lPoXEMuhTpIluN0X github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= github.com/fsnotify/fsnotify v1.5.4/go.mod h1:OVB6XrOHzAwXMpEM7uPOzcehqUV2UqJxmVXmkdnm1bU= -github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY= -github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw= +github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA= +github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM= github.com/gavv/httpexpect v2.0.0+incompatible h1:1X9kcRshkSKEjNJJxX9Y9mQ5BRfbxU5kORdjhlA1yX8= github.com/getkin/kin-openapi v0.94.0/go.mod h1:LWZfzOd7PRy8GJ1dJ6mCU6tNdSfOwRac1BUPam4aw6Q= github.com/getsentry/raven-go v0.2.0/go.mod h1:KungGk8q33+aIAZUIVWZDr2OfAEBsO49PX4NzFV5kcQ= @@ -1030,8 +1030,8 @@ github.com/lestrrat-go/iter v1.0.1/go.mod h1:zIdgO1mRKhn8l9vrZJZz9TUMMFbQbLeTsbq github.com/lestrrat-go/iter v1.0.2 h1:gMXo1q4c2pHmC3dn8LzRhJfP1ceCbgSiT9lUydIzltI= github.com/lestrrat-go/iter v1.0.2/go.mod h1:Momfcq3AnRlRjI5b5O8/G5/BvpzrhoFTZcn06fEOPt4= github.com/lestrrat-go/jwx v1.2.24/go.mod h1:zoNuZymNl5lgdcu6P7K6ie2QRll5HVfF4xwxBBK1NxY= -github.com/lestrrat-go/jwx/v2 v2.0.13 h1:XdxzJbudGaHEoNmyJACAT8aFCB+DmviiaiMoZwuJoUo= -github.com/lestrrat-go/jwx/v2 v2.0.13/go.mod h1:UzXMzcV99p9/xe1JsIb336NJDGXLsleR+Qj3ucEDtfI= +github.com/lestrrat-go/jwx/v2 v2.0.15 h1:XvR2lQdX+mZechmqWxqQb2foU3hgAn5+Rj0ICa0I6sU= +github.com/lestrrat-go/jwx/v2 v2.0.15/go.mod h1:jBHyESp4e7QxfERM0UKkQ80/94paqNIEcdEfiUYz5zE= github.com/lestrrat-go/option v1.0.0/go.mod h1:5ZHFbivi4xwXxhxY9XHDe2FHo6/Z7WWmtT7T5nBBp3I= github.com/lestrrat-go/option v1.0.1 h1:oAzP2fvZGQKWkvHa1/SAcFolBEca1oN+mQ7eooNBEYU= github.com/lestrrat-go/option v1.0.1/go.mod h1:5ZHFbivi4xwXxhxY9XHDe2FHo6/Z7WWmtT7T5nBBp3I= @@ -1682,8 +1682,8 @@ golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0 golang.org/x/crypto v0.1.0/go.mod h1:RecgLatLF4+eUMCP1PoPZQb+cVrJcOPbHkTkbkB9sbw= golang.org/x/crypto v0.6.0/go.mod h1:OFC/31mSvZgRz0V1QTNCzfAI1aIRzbiufJtkMIlEp58= golang.org/x/crypto v0.10.0/go.mod h1:o4eNf7Ede1fv+hwOwZsTHl9EsPFO6q6ZvYR8vYfY45I= -golang.org/x/crypto v0.13.0 h1:mvySKfSWJ+UKUii46M40LOvyWfN0s2U+46/jDd0e6Ck= -golang.org/x/crypto v0.13.0/go.mod h1:y6Z2r+Rw4iayiXXAIxJIDAJ1zMW4yaTpebo8fPOliYc= +golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc= +golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4= golang.org/x/exp v0.0.0-20180321215751-8460e604b9de/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20180807140117-3d87b88a115f/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= @@ -1699,8 +1699,8 @@ golang.org/x/exp v0.0.0-20200119233911-0405dc783f0a/go.mod h1:2RIsYlXP63K8oxa1u0 golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EHIKF9dgMWnmCNThgcyBT1FY9mM= golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU= golang.org/x/exp v0.0.0-20200331195152-e8c3332aa8e5/go.mod h1:4M0jN8W1tt0AVLNr8HDosyJCDCDuyL9N9+3m7wDWgKw= -golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g= -golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k= +golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI= +golang.org/x/exp v0.0.0-20231006140011-7918f672742d/go.mod h1:ldy0pHrwJyGW56pPQzzkH36rKxoZW1tw7ZJpeKx+hdo= golang.org/x/image v0.0.0-20180708004352-c73c2afc3b81/go.mod h1:ux5Hcp/YLpHSI86hEcLt0YII63i6oz57MZXIpbrjZUs= golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js= golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= @@ -1742,8 +1742,8 @@ golang.org/x/mod v0.6.0/go.mod h1:4mET923SAdbXp2ki8ey+zGs1SLqsuM2Y0uvdZR/fUNI= golang.org/x/mod v0.7.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/mod v0.9.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= -golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc= -golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= +golang.org/x/mod v0.13.0 h1:I/DsJXRlw/8l/0c24sM9yb0T4z9liZTduXvdAWYiysY= +golang.org/x/mod v0.13.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= golang.org/x/net v0.0.0-20180530234432-1e491301e022/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= @@ -1813,8 +1813,8 @@ golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc= golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg= golang.org/x/net v0.11.0/go.mod h1:2L/ixqYpgIVXmeoSA/4Lu7BzTG4KIyPIryS4IsOd1oQ= -golang.org/x/net v0.15.0 h1:ugBLEUaxABaB5AJqW9enI0ACdci2RUd4eP51NTBvuJ8= -golang.org/x/net v0.15.0/go.mod h1:idbUs1IY1+zTqbi8yxTbhexhEEk5ur9LInksu6HrEpk= +golang.org/x/net v0.16.0 h1:7eBu7KsSvFDtSXUIDbh3aqlK4DPsZ1rByC8PFfBThos= +golang.org/x/net v0.16.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= @@ -1839,8 +1839,8 @@ golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sync v0.0.0-20220601150217-0de741cfad7f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E= -golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y= +golang.org/x/sync v0.4.0 h1:zxkM55ReGkDlKSM+Fu41A+zmbZuaPVbGMzvvdUPznYQ= +golang.org/x/sync v0.4.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y= golang.org/x/sys v0.0.0-20180823144017-11551d06cbcc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180828065106-d99a578cf41b/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= @@ -1946,7 +1946,6 @@ golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220817070843-5a390386f1f2/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= @@ -1955,8 +1954,8 @@ golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.9.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o= -golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE= +golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/term v0.1.0/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= @@ -1967,8 +1966,8 @@ golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= golang.org/x/term v0.6.0/go.mod h1:m6U89DPEgQRMq3DNkDClhWw02AUbt2daBVO4cn4Hv9U= golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo= golang.org/x/term v0.9.0/go.mod h1:M6DEAAIenWoTxdKrOltXcmDY3rSplQUkrvaDU5FcQyo= -golang.org/x/term v0.12.0 h1:/ZfYdc3zq+q02Rv9vGqTeSItdzZTSNDmfTi0mBAuidU= -golang.org/x/term v0.12.0/go.mod h1:owVbMEjm3cBLCHdkQu9b1opXd4ETQWc3BhuQGKgXgvU= +golang.org/x/term v0.13.0 h1:bb+I9cTfFazGW51MZqBVmZy7+JEJMouUHTUSKVQLBek= +golang.org/x/term v0.13.0/go.mod h1:LTmsnFJwVN6bCy1rVCoS+qHT1HhALEFxKncY3WNNh4U= golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= @@ -2072,8 +2071,8 @@ golang.org/x/tools v0.2.0/go.mod h1:y4OqIKeOV/fWJetJ8bXPU1sEVniLMIyDAZWeHdV+NTA= golang.org/x/tools v0.4.0/go.mod h1:UE5sM2OK9E/d67R0ANs2xJizIymRP5gJU295PvKXxjQ= golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= golang.org/x/tools v0.7.0/go.mod h1:4pg6aUX35JBAogB10C9AtvVL+qowtN4pT3CGSQex14s= -golang.org/x/tools v0.13.0 h1:Iey4qkscZuv0VvIt8E0neZjtPVQFSc870HQ448QgEmQ= -golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58= +golang.org/x/tools v0.14.0 h1:jvNa2pY0M4r62jkRQ6RwEZZyPcymeL9XZMLBbV7U2nc= +golang.org/x/tools v0.14.0/go.mod h1:uYBEerGOWcJyEORxN+Ek8+TT266gXkNlHdJBwexUsBg= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= @@ -2164,8 +2163,8 @@ google.golang.org/genproto v0.0.0-20230821184602-ccc8af3d0e93 h1:zv6ieVm8jNcN33A google.golang.org/genproto v0.0.0-20230821184602-ccc8af3d0e93/go.mod h1:yZTlhN0tQnXo3h00fuXNCxJdLdIdnVFVBaRJ5LWBbw4= google.golang.org/genproto/googleapis/api v0.0.0-20230803162519-f966b187b2e5 h1:nIgk/EEq3/YlnmVVXVnm14rC2oxgs1o0ong4sD/rd44= google.golang.org/genproto/googleapis/api v0.0.0-20230803162519-f966b187b2e5/go.mod h1:5DZzOUPCLYL3mNkQ0ms0F3EuUNZ7py1Bqeq6sxzI7/Q= -google.golang.org/genproto/googleapis/rpc v0.0.0-20230807174057-1744710a1577 h1:wukfNtZmZUurLN/atp2hiIeTKn7QJWIQdHzqmsOnAOk= -google.golang.org/genproto/googleapis/rpc v0.0.0-20230807174057-1744710a1577/go.mod h1:+Bk1OCOj40wS2hwAMA+aCW9ypzm63QTBBHp6lQ3p+9M= +google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d h1:uvYuEyMHKNt+lT4K3bN6fGswmK8qSvcreM3BwjDh+y4= +google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d/go.mod h1:+Bk1OCOj40wS2hwAMA+aCW9ypzm63QTBBHp6lQ3p+9M= google.golang.org/grpc v1.8.0/go.mod h1:yo6s7OP7yaDglbqo1J04qKzAhqBH6lvTonzMVmEdcZw= google.golang.org/grpc v1.14.0/go.mod h1:yo6s7OP7yaDglbqo1J04qKzAhqBH6lvTonzMVmEdcZw= google.golang.org/grpc v1.17.0/go.mod h1:6QZJwpn2B+Zp71q/5VxRsJ6NXXVCE5NRUHRo+f3cWCs= diff --git a/pkg/channel/grpc/grpc_channel_test.go b/pkg/channel/grpc/grpc_channel_test.go index d58329a51fe..223dbe5e90c 100644 --- a/pkg/channel/grpc/grpc_channel_test.go +++ b/pkg/channel/grpc/grpc_channel_test.go @@ -36,7 +36,7 @@ import ( invokev1 "github.com/dapr/dapr/pkg/messaging/v1" runtimev1pb "github.com/dapr/dapr/pkg/proto/runtime/v1" securityConsts "github.com/dapr/dapr/pkg/security/consts" - "github.com/dapr/dapr/utils/streams" + daprt "github.com/dapr/dapr/pkg/testing" ) // TODO: Add APIVersion testing @@ -124,7 +124,7 @@ func TestInvokeMethod(t *testing.T) { t.Run("request body stream errors", func(t *testing.T) { req := invokev1.NewInvokeMethodRequest("method"). WithHTTPExtension(http.MethodPost, "param1=val1¶m2=val2"). - WithRawData(&streams.ErrorReader{}) + WithRawData(&daprt.ErrorReader{}) defer req.Close() response, err := c.InvokeMethod(ctx, req, "") diff --git a/pkg/channel/http/http_channel.go b/pkg/channel/http/http_channel.go index 2380c42ebd9..057520a7999 100644 --- a/pkg/channel/http/http_channel.go +++ b/pkg/channel/http/http_channel.go @@ -41,7 +41,7 @@ import ( "github.com/dapr/dapr/pkg/runtime/compstore" "github.com/dapr/dapr/pkg/security" securityConsts "github.com/dapr/dapr/pkg/security/consts" - streamutils "github.com/dapr/dapr/utils/streams" + streamutils "github.com/dapr/kit/streams" ) const ( diff --git a/pkg/components/disk_manifest_loader.go b/pkg/components/disk_manifest_loader.go index 591aa596454..3088b31bcbf 100644 --- a/pkg/components/disk_manifest_loader.go +++ b/pkg/components/disk_manifest_loader.go @@ -21,11 +21,11 @@ import ( "path/filepath" "strings" - "github.com/dapr/dapr/utils" - "k8s.io/apimachinery/pkg/api/validation/path" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "sigs.k8s.io/yaml" + + "github.com/dapr/kit/utils" ) const yamlSeparator = "\n---" diff --git a/pkg/concurrency/closer.go b/pkg/concurrency/closer.go deleted file mode 100644 index e83f109e4d5..00000000000 --- a/pkg/concurrency/closer.go +++ /dev/null @@ -1,218 +0,0 @@ -/* -Copyright 2021 The Dapr Authors -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. -*/ - -package concurrency - -import ( - "context" - "errors" - "fmt" - "io" - "sync/atomic" - "time" - - "k8s.io/utils/clock" - - "github.com/dapr/kit/logger" -) - -var ( - ErrManagerAlreadyClosed = errors.New("runner manager already closed") - - log = logger.NewLogger("dapr.concurrency.closer") -) - -// RunnerCloserManager is a RunnerManager that also implements Closing of the -// added closers once the main runners are done. -type RunnerCloserManager struct { - // mngr implements the main RunnerManager. - mngr *RunnerManager - - // closers are the closers to be closed once the main runners are done. - closers []func() error - - // retErr is the error returned by the main runners and closers. Used to - // return the resulting error from Close(). - retErr error - - // fatalShutdownFn is called if the grace period is exceeded. - // Defined if the grace period is not nil. - fatalShutdownFn func() - - // closeFatalShutdown closes the fatal shutdown goroutine. Closing is a no-op - // if fatalShutdownFn is nil. - closeFatalShutdown chan struct{} - - clock clock.Clock - running atomic.Bool - closing atomic.Bool - closed atomic.Bool - closeCh chan struct{} - stopped chan struct{} -} - -// NewRunnerCloserManager creates a new RunnerCloserManager with the given -// grace period and runners. -// If gracePeriod is nil, the grace period is infinite. -func NewRunnerCloserManager(gracePeriod *time.Duration, runners ...Runner) *RunnerCloserManager { - c := &RunnerCloserManager{ - mngr: NewRunnerManager(runners...), - clock: clock.RealClock{}, - stopped: make(chan struct{}), - closeCh: make(chan struct{}), - closeFatalShutdown: make(chan struct{}), - } - - if gracePeriod == nil { - log.Warn("Graceful shutdown timeout is infinite, will wait indefinitely to shutdown") - return c - } - - c.fatalShutdownFn = func() { - log.Fatal("Graceful shutdown timeout exceeded, forcing shutdown") - } - - c.AddCloser(func() { - log.Debugf("Graceful shutdown timeout: %s", *gracePeriod) - - t := c.clock.NewTimer(*gracePeriod) - defer t.Stop() - - select { - case <-t.C(): - c.fatalShutdownFn() - case <-c.closeFatalShutdown: - } - }) - - return c -} - -// Add implements RunnerManager.Add. -func (c *RunnerCloserManager) Add(runner ...Runner) error { - if c.running.Load() { - return ErrManagerAlreadyStarted - } - - return c.mngr.Add(runner...) -} - -// AddCloser adds a closer to the list of closers to be closed once the main -// runners are done. -func (c *RunnerCloserManager) AddCloser(closers ...any) error { - if c.closing.Load() { - return ErrManagerAlreadyClosed - } - - c.mngr.lock.Lock() - defer c.mngr.lock.Unlock() - - var errs []error - for _, cl := range closers { - switch v := cl.(type) { - case io.Closer: - c.closers = append(c.closers, v.Close) - case func(context.Context) error: - c.closers = append(c.closers, func() error { - // We use a background context here since the fatalShutdownFn will kill - // the program if the grace period is exceeded. - return v(context.Background()) - }) - case func() error: - c.closers = append(c.closers, v) - case func(): - c.closers = append(c.closers, func() error { - v() - return nil - }) - default: - errs = append(errs, fmt.Errorf("unsupported closer type: %T", v)) - } - } - - return errors.Join(errs...) -} - -// Add implements RunnerManager.Run. -func (c *RunnerCloserManager) Run(ctx context.Context) error { - if !c.running.CompareAndSwap(false, true) { - return ErrManagerAlreadyStarted - } - - // Signal the manager is stopped. - defer close(c.stopped) - - // If the main runner has at least one runner, add a closer that will - // close the context once Close() is called. - if len(c.mngr.runners) > 0 { - c.mngr.Add(func(ctx context.Context) error { - select { - case <-ctx.Done(): - case <-c.closeCh: - } - return nil - }) - } - - errCh := make(chan error, len(c.closers)) - go func() { - errCh <- c.mngr.Run(ctx) - }() - - rErr := <-errCh - - c.mngr.lock.Lock() - defer c.mngr.lock.Unlock() - c.closing.Store(true) - - errs := make([]error, len(c.closers)+1) - errs[0] = rErr - - for _, closer := range c.closers { - go func(closer func() error) { - errCh <- closer() - }(closer) - } - - // Wait for all closers to be done. - for i := 1; i < len(c.closers)+1; i++ { - // Close the fatal shutdown goroutine if all closers are done. This is a - // no-op if the fatal go routine is not defined. - if i == len(c.closers) { - close(c.closeFatalShutdown) - } - errs[i] = <-errCh - } - - c.retErr = errors.Join(errs...) - - return c.retErr -} - -// Close will close the main runners and then the closers. -func (c *RunnerCloserManager) Close() error { - if c.closed.CompareAndSwap(false, true) { - close(c.closeCh) - } - // If the manager is not running yet, we stop immediately. - if c.running.CompareAndSwap(false, true) { - close(c.stopped) - } - c.WaitUntilShutdown() - return c.retErr -} - -// WaitUntilShutdown will block until the main runners and closers are done. -func (c *RunnerCloserManager) WaitUntilShutdown() { - <-c.stopped -} diff --git a/pkg/concurrency/closer_test.go b/pkg/concurrency/closer_test.go deleted file mode 100644 index ff26ce12650..00000000000 --- a/pkg/concurrency/closer_test.go +++ /dev/null @@ -1,1103 +0,0 @@ -/* -Copyright 2023 The Dapr Authors -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. -*/ - -package concurrency - -import ( - "context" - "errors" - "strings" - "sync/atomic" - "testing" - "time" - - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" - clocktesting "k8s.io/utils/clock/testing" -) - -type mockCloser func() error - -func (m mockCloser) Close() error { - return m() -} - -func Test_RunnerClosterManager(t *testing.T) { - t.Parallel() - - t.Run("runner with no tasks or closers should return nil", func(t *testing.T) { - t.Parallel() - - assert.NoError(t, NewRunnerCloserManager(nil).Run(context.Background())) - }) - - t.Run("runner with a task that completes should return nil", func(t *testing.T) { - t.Parallel() - - var i atomic.Int32 - assert.NoError(t, NewRunnerCloserManager(nil, func(context.Context) error { - i.Add(1) - return nil - }).Run(context.Background())) - assert.Equal(t, int32(1), i.Load()) - }) - - t.Run("runner with a task and closer that completes should return nil", func(t *testing.T) { - t.Parallel() - - var i atomic.Int32 - mngr := NewRunnerCloserManager(nil, func(context.Context) error { - i.Add(1) - return nil - }) - assert.NoError(t, mngr.AddCloser(func(context.Context) error { - i.Add(1) - return nil - })) - assert.NoError(t, mngr.Run(context.Background())) - assert.Equal(t, int32(2), i.Load()) - }) - - t.Run("runner with multiple tasks and closers that complete should return nil", func(t *testing.T) { - t.Parallel() - - var i atomic.Int32 - mngr := NewRunnerCloserManager(nil, - func(context.Context) error { - i.Add(1) - return nil - }, - func(context.Context) error { - i.Add(1) - return nil - }, - func(context.Context) error { - i.Add(1) - return nil - }, - ) - assert.NoError(t, mngr.AddCloser( - func(context.Context) error { - i.Add(1) - return nil - }, - func() error { - i.Add(1) - return nil - }, - func() { - i.Add(1) - }, - mockCloser(func() error { - i.Add(1) - return nil - }), - )) - - assert.NoError(t, mngr.Run(context.Background())) - assert.Equal(t, int32(7), i.Load()) - }) - - t.Run("a runner that errors should error but still call the closers", func(t *testing.T) { - t.Parallel() - - var i atomic.Int32 - mngr := NewRunnerCloserManager(nil, - func(ctx context.Context) error { - i.Add(1) - return errors.New("error") - }, - func(ctx context.Context) error { - i.Add(1) - return nil - }, - func(ctx context.Context) error { - i.Add(1) - return nil - }, - ) - assert.NoError(t, mngr.AddCloser( - func(ctx context.Context) error { - i.Add(1) - return nil - }, - )) - - assert.EqualError(t, mngr.Run(context.Background()), "error") - assert.Equal(t, int32(4), i.Load()) - }) - - t.Run("a runner that has closter errors should error", func(t *testing.T) { - t.Parallel() - - var i atomic.Int32 - mngr := NewRunnerCloserManager(nil, - func(ctx context.Context) error { - i.Add(1) - return nil - }, - func(ctx context.Context) error { - i.Add(1) - return nil - }, - func(ctx context.Context) error { - i.Add(1) - return nil - }, - ) - assert.NoError(t, mngr.AddCloser( - func(ctx context.Context) error { - i.Add(1) - return errors.New("error") - }, - )) - - assert.EqualError(t, mngr.Run(context.Background()), "error") - assert.Equal(t, int32(4), i.Load()) - }) - - t.Run("a runner with multiple errors should collect all errors (string match)", func(t *testing.T) { - t.Parallel() - - var i atomic.Int32 - mngr := NewRunnerCloserManager(nil, - func(ctx context.Context) error { - i.Add(1) - return errors.New("error") - }, - func(ctx context.Context) error { - i.Add(1) - return errors.New("error") - }, - func(ctx context.Context) error { - i.Add(1) - return errors.New("error") - }, - ) - assert.NoError(t, mngr.AddCloser( - func(ctx context.Context) error { - i.Add(1) - return errors.New("closererror") - }, - func() error { - i.Add(1) - return errors.New("closererror") - }, - mockCloser(func() error { - i.Add(1) - return errors.New("closererror") - }), - )) - - err := mngr.Run(context.Background()) - require.Error(t, err) - assert.ErrorContains(t, err, "error\nerror\nerror\nclosererror\nclosererror\nclosererror") //nolint:dupword - assert.Equal(t, int32(6), i.Load()) - }) - - t.Run("a runner with multiple errors should collect all errors (unique)", func(t *testing.T) { - t.Parallel() - - var i atomic.Int32 - mngr := NewRunnerCloserManager(nil, - func(ctx context.Context) error { - i.Add(1) - return errors.New("error1") - }, - func(ctx context.Context) error { - i.Add(1) - return errors.New("error2") - }, - func(ctx context.Context) error { - i.Add(1) - return errors.New("error3") - }, - ) - assert.NoError(t, mngr.AddCloser( - func(ctx context.Context) error { - i.Add(1) - return errors.New("closererror1") - }, - func() error { - i.Add(1) - return errors.New("closererror2") - }, - mockCloser(func() error { - i.Add(1) - return errors.New("closererror3") - }), - )) - - err := mngr.Run(context.Background()) - require.Error(t, err) - assert.ElementsMatch(t, - []string{"error1", "error2", "error3", "closererror1", "closererror2", "closererror3"}, - strings.Split(err.Error(), "\n"), - ) - assert.Equal(t, int32(6), i.Load()) - }) - - t.Run("should be able to add runner with New, Add and AddCloser", func(t *testing.T) { - t.Parallel() - - var i atomic.Int32 - mngr := NewRunnerCloserManager(nil, - func(ctx context.Context) error { - i.Add(1) - return nil - }, - ) - assert.NoError(t, mngr.Add( - func(ctx context.Context) error { - i.Add(1) - return nil - }, - )) - assert.NoError(t, mngr.Add( - func(ctx context.Context) error { - i.Add(1) - return nil - }, - )) - assert.NoError(t, mngr.AddCloser( - func(ctx context.Context) error { - i.Add(1) - return nil - }, - )) - assert.NoError(t, mngr.AddCloser( - func() { - i.Add(1) - }, - )) - - assert.NoError(t, mngr.Run(context.Background())) - assert.Equal(t, int32(5), i.Load()) - }) - - t.Run("when a runner returns, expect context to be cancelled for other runners, but not for closers returning", func(t *testing.T) { - t.Parallel() - - var i atomic.Int32 - mngr := NewRunnerCloserManager(nil, - func(ctx context.Context) error { - i.Add(1) - return nil - }, - func(ctx context.Context) error { - i.Add(1) - select { - case <-ctx.Done(): - case <-time.After(time.Second): - t.Error("context should have been cancelled in time") - } - return nil - }, - func(ctx context.Context) error { - i.Add(1) - select { - case <-ctx.Done(): - case <-time.After(time.Second): - t.Error("context should have been cancelled in time") - } - return nil - }, - ) - - closer1Ch := make(chan struct{}) - closer2Ch := make(chan struct{}) - assert.NoError(t, mngr.AddCloser( - func(ctx context.Context) error { - i.Add(1) - close(closer1Ch) - return nil - }, - func(ctx context.Context) error { - i.Add(1) - select { - case <-ctx.Done(): - t.Error("context should not have been cancelled") - case <-closer1Ch: - } - close(closer2Ch) - return nil - }, - func(ctx context.Context) error { - i.Add(1) - select { - case <-ctx.Done(): - t.Error("context should not have been cancelled") - case <-closer2Ch: - } - return nil - }, - )) - - assert.NoError(t, mngr.Run(context.Background())) - assert.Equal(t, int32(6), i.Load()) - }) - - t.Run("when a runner errors, expect context to be cancelled for other runners, but closers should still run", func(t *testing.T) { - t.Parallel() - - var i atomic.Int32 - mngr := NewRunnerCloserManager(nil, - func(ctx context.Context) error { - i.Add(1) - select { - case <-ctx.Done(): - case <-time.After(time.Second): - t.Error("context should have been cancelled in time") - } - return errors.New("error1") - }, - func(ctx context.Context) error { - i.Add(1) - select { - case <-ctx.Done(): - case <-time.After(time.Second): - t.Error("context should have been cancelled in time") - } - return errors.New("error2") - }, - func(ctx context.Context) error { - i.Add(1) - return errors.New("error3") - }, - ) - - assert.NoError(t, mngr.AddCloser( - func(ctx context.Context) error { - i.Add(1) - select { - case <-ctx.Done(): - t.Error("context should not have been cancelled") - default: - } - return errors.New("closererror1") - }, - func(ctx context.Context) error { - i.Add(1) - select { - case <-ctx.Done(): - t.Error("context should not have been cancelled") - default: - } - return errors.New("closererror2") - }, - )) - - err := mngr.Run(context.Background()) - require.Error(t, err) - assert.ElementsMatch(t, - []string{"error1", "error2", "error3", "closererror1", "closererror2"}, - strings.Split(err.Error(), "\n"), - ) - assert.Equal(t, int32(5), i.Load()) - }) - - t.Run("a manger started twice should error", func(t *testing.T) { - t.Parallel() - - var i atomic.Int32 - m := NewRunnerCloserManager(nil, func(ctx context.Context) error { - i.Add(1) - return nil - }) - assert.NoError(t, m.Run(context.Background())) - assert.Equal(t, int32(1), i.Load()) - assert.Error(t, m.Run(context.Background()), errors.New("manager already started")) - assert.Equal(t, int32(1), i.Load()) - }) - - t.Run("a manger started twice should error", func(t *testing.T) { - t.Parallel() - - var i atomic.Int32 - m := NewRunnerCloserManager(nil, func(ctx context.Context) error { - i.Add(1) - return nil - }) - - assert.NoError(t, m.AddCloser(func(ctx context.Context) error { - i.Add(1) - return nil - })) - - assert.NoError(t, m.Run(context.Background())) - assert.Equal(t, int32(2), i.Load()) - assert.NoError(t, m.Close()) - assert.NoError(t, m.Close()) - assert.Error(t, m.Run(context.Background()), errors.New("manager already started")) - assert.Equal(t, int32(2), i.Load()) - }) - - t.Run("adding a task to a started manager should error", func(t *testing.T) { - t.Parallel() - - var i atomic.Int32 - m := NewRunnerCloserManager(nil, func(ctx context.Context) error { - i.Add(1) - return nil - }) - assert.NoError(t, m.Run(context.Background())) - assert.Equal(t, int32(1), i.Load()) - err := m.Add(func(ctx context.Context) error { - i.Add(1) - return nil - }) - require.Error(t, err) - assert.Equal(t, err, errors.New("runner manager already started")) - assert.Equal(t, int32(1), i.Load()) - }) - - t.Run("adding a closer to a closing manager should error", func(t *testing.T) { - t.Parallel() - - var i atomic.Int32 - m := NewRunnerCloserManager(nil, func(ctx context.Context) error { - i.Add(1) - return nil - }) - assert.NoError(t, m.Run(context.Background())) - assert.Equal(t, int32(1), i.Load()) - assert.NoError(t, m.Close()) - err := m.AddCloser(func(ctx context.Context) error { - i.Add(1) - return nil - }) - require.Error(t, err) - assert.Equal(t, err, errors.New("runner manager already closed")) - assert.Equal(t, int32(1), i.Load()) - }) - - t.Run("if grace period is not given, should have no force shutdown", func(t *testing.T) { - t.Parallel() - - mngr := NewRunnerCloserManager(nil) - assert.Len(t, mngr.closers, 0) - }) - - t.Run("if grace period is given, should have force shutdown", func(t *testing.T) { - t.Parallel() - - dur := time.Second - mngr := NewRunnerCloserManager(&dur) - assert.Len(t, mngr.closers, 1) - }) - - t.Run("if closing but grace period not reached, should return", func(t *testing.T) { - t.Parallel() - - dur := time.Second - mngr := NewRunnerCloserManager(&dur) - - var i atomic.Int32 - assert.NoError(t, mngr.AddCloser(func() { - i.Add(1) - })) - - assert.Len(t, mngr.closers, 2) - - clock := clocktesting.NewFakeClock(time.Now()) - mngr.clock = clock - - fatalCalled := make(chan struct{}) - mngr.WithFatalShutdown(func() { - i.Add(1) - close(fatalCalled) - }) - - errCh := make(chan error) - go func() { - errCh <- mngr.Run(context.Background()) - }() - - select { - case err := <-errCh: - assert.NoError(t, err) - case <-fatalCalled: - t.Error("fatal shutdown called") - case <-time.After(time.Second * 3): - t.Error("Run() not returned") - } - - assert.Eventually(t, func() bool { - return !clock.HasWaiters() - }, time.Second*3, time.Millisecond*100, - "fatal shutdown should have not have been called and returned", - ) - - assert.Equal(t, int32(1), i.Load()) - }) - - t.Run("if closing and grace period is reached, should force shutdown", func(t *testing.T) { - t.Parallel() - - dur := time.Second - mngr := NewRunnerCloserManager(&dur) - assert.Len(t, mngr.closers, 1) - - clock := clocktesting.NewFakeClock(time.Now()) - mngr.clock = clock - - ctx, cancel := context.WithCancel(context.Background()) - t.Cleanup(cancel) - - fatalCalled := make(chan struct{}) - mngr.WithFatalShutdown(func() { - close(fatalCalled) - }) - mngr.AddCloser(func() error { - <-ctx.Done() - return nil - }) - - errCh := make(chan error) - t.Cleanup(func() { - select { - case err := <-errCh: - assert.NoError(t, err) - case <-time.After(time.Second * 3): - t.Error("manager not closed") - } - }) - go func() { - errCh <- mngr.Run(context.Background()) - }() - - assert.Eventually(t, func() bool { - return clock.HasWaiters() - }, time.Second*3, time.Millisecond*100) - - clock.Step(time.Second * 2) - - select { - case <-fatalCalled: - case <-time.After(time.Second * 3): - t.Error("fatal shutdown not called") - } - cancel() - }) -} - -func TestClose(t *testing.T) { - t.Parallel() - - t.Run("calling close should stop the main runner and call all closers", func(t *testing.T) { - t.Parallel() - - var i atomic.Int32 - runnerWaiting := make(chan struct{}) - mngr := NewRunnerCloserManager(nil, func(ctx context.Context) error { - close(runnerWaiting) - <-ctx.Done() - i.Add(1) - return nil - }) - assert.NoError(t, mngr.AddCloser(func() { - i.Add(1) - })) - - errCh := make(chan error) - go func() { - errCh <- mngr.Run(context.Background()) - }() - - select { - case <-runnerWaiting: - case <-time.After(time.Second * 3): - t.Error("runner not waiting") - } - - assert.NoError(t, mngr.Close()) - - select { - case err := <-errCh: - assert.NoError(t, err) - case <-time.After(time.Second * 3): - t.Error("Run() not returned") - } - - assert.Equal(t, int32(2), i.Load()) - }) - - t.Run("calling close should wait for all closers to return", func(t *testing.T) { - t.Parallel() - - var i atomic.Int32 - runnerWaiting := make(chan struct{}) - mngr := NewRunnerCloserManager(nil, func(ctx context.Context) error { - close(runnerWaiting) - <-ctx.Done() - i.Add(1) - return nil - }) - - returnClose := make(chan struct{}) - assert.NoError(t, mngr.AddCloser( - func() { - i.Add(1) - <-returnClose - }, - func() error { - i.Add(1) - <-returnClose - return nil - }, - func(context.Context) error { - i.Add(1) - <-returnClose - return nil - }, - mockCloser(func() error { - i.Add(1) - <-returnClose - return nil - }), - )) - - assert.Len(t, mngr.closers, 4) - - errCh := make(chan error) - go func() { - errCh <- mngr.Run(context.Background()) - }() - - select { - case <-runnerWaiting: - case <-time.After(time.Second * 3): - t.Error("runner not waiting") - } - - // Should be zero because main runner context is not cancelled yet. - assert.Equal(t, int32(0), i.Load()) - - closeReturned := make(chan struct{}) - go func() { - mngr.Close() - close(closeReturned) - }() - - assert.Eventually(t, func() bool { - return i.Load() == 5 - }, time.Second*3, time.Millisecond*100) - - close(returnClose) - - select { - case err := <-errCh: - assert.NoError(t, err) - case <-time.After(time.Second * 3): - t.Error("Run() not returned") - } - - select { - case <-closeReturned: - case <-time.After(time.Second * 3): - t.Error("Close() not returned") - } - }) - - t.Run("calling close should wait for all closers return but should not call fatal when enabled", func(t *testing.T) { - t.Parallel() - - dur := time.Second - var i atomic.Int32 - runnerWaiting := make(chan struct{}) - mngr := NewRunnerCloserManager(&dur, func(ctx context.Context) error { - close(runnerWaiting) - <-ctx.Done() - i.Add(1) - return nil - }) - - clock := clocktesting.NewFakeClock(time.Now()) - mngr.clock = clock - - mngr.WithFatalShutdown(func() { - i.Add(1) - }) - - assert.Len(t, mngr.closers, 1) - - returnClose := make(chan struct{}) - assert.NoError(t, mngr.AddCloser( - func() { - i.Add(1) - <-returnClose - }, - func() error { - i.Add(1) - <-returnClose - return nil - }, - func(context.Context) error { - i.Add(1) - <-returnClose - return nil - }, - mockCloser(func() error { - i.Add(1) - <-returnClose - return nil - }), - )) - - assert.Len(t, mngr.closers, 5) - - errCh := make(chan error) - go func() { - errCh <- mngr.Run(context.Background()) - }() - - select { - case <-runnerWaiting: - case <-time.After(time.Second * 3): - t.Error("runner not waiting") - } - - // Should be zero because main runner context is not cancelled yet. - assert.Equal(t, int32(0), i.Load()) - - closeReturned := make(chan struct{}) - go func() { - mngr.Close() - close(closeReturned) - }() - - assert.Eventually(t, func() bool { - return i.Load() == 5 - }, time.Second*3, time.Millisecond*100) - - close(returnClose) - - select { - case err := <-errCh: - assert.NoError(t, err) - case <-time.After(time.Second * 3): - t.Error("Run() not returned") - } - - select { - case <-closeReturned: - case <-time.After(time.Second * 3): - t.Error("Close() not returned") - } - - assert.Equal(t, int32(5), i.Load()) - }) - - t.Run("calling close should call fatal if the grace period is reached", func(t *testing.T) { - t.Parallel() - - dur := time.Second - var i atomic.Int32 - runnerWaiting := make(chan struct{}) - mngr := NewRunnerCloserManager(&dur, func(ctx context.Context) error { - close(runnerWaiting) - <-ctx.Done() - i.Add(1) - return nil - }) - - clock := clocktesting.NewFakeClock(time.Now()) - mngr.clock = clock - - fatalCalled := make(chan struct{}) - mngr.WithFatalShutdown(func() { - close(fatalCalled) - }) - - assert.Len(t, mngr.closers, 1) - - returnClose := make(chan struct{}) - for n := 0; n < 4; n++ { - assert.NoError(t, mngr.AddCloser(func() { - i.Add(1) - <-returnClose - })) - } - - assert.Len(t, mngr.closers, 5) - - errCh := make(chan error) - go func() { - errCh <- mngr.Run(context.Background()) - }() - - select { - case <-runnerWaiting: - case <-time.After(time.Second * 3): - t.Error("runner not waiting") - } - - // Should be zero because main runner context is not cancelled yet. - assert.Equal(t, int32(0), i.Load()) - - closeReturned := make(chan struct{}) - go func() { - mngr.Close() - close(closeReturned) - }() - - // Wait for all closers to be called, and fatal routine is waiting. - assert.Eventually(t, func() bool { - return clock.HasWaiters() && i.Load() == 5 - }, time.Second*3, time.Millisecond*100) - - clock.Step(time.Second) - - select { - case <-fatalCalled: - case <-closeReturned: - t.Error("Close() returned") - case <-time.After(time.Second * 3): - t.Error("fatal not called") - } - - close(returnClose) - }) - - t.Run("calling close should return the errors from the main runner and all closers", func(t *testing.T) { - t.Parallel() - - mngr := NewRunnerCloserManager(nil, - func(ctx context.Context) error { - return errors.New("error1") - }, - func(ctx context.Context) error { - return errors.New("error2") - }, - func(ctx context.Context) error { - return errors.New("error3") - }, - ) - - assert.NoError(t, mngr.AddCloser( - func() error { - return errors.New("error4") - }, - func(context.Context) error { - return errors.New("error5") - }, - mockCloser(func() error { - return errors.New("error6") - }), - )) - - errCh := make(chan error) - go func() { - errCh <- mngr.Run(context.Background()) - }() - - var err error - select { - case err = <-errCh: - case <-time.After(time.Second * 3): - t.Error("Run() not returned") - } - - exp := []string{"error1", "error2", "error3", "error4", "error5", "error6"} - assert.ElementsMatch(t, exp, strings.Split(err.Error(), "\n")) - assert.ElementsMatch(t, exp, strings.Split(mngr.Close().Error(), "\n")) - assert.ElementsMatch(t, exp, strings.Split(mngr.Close().Error(), "\n")) - }) - - t.Run("calling Close before Run should return immediately", func(t *testing.T) { - dur := time.Second - mngr := NewRunnerCloserManager(&dur, - func(ctx context.Context) error { - return errors.New("error1") - }, - ) - assert.NoError(t, mngr.AddCloser(func() error { - return errors.New("error2") - })) - - assert.NoError(t, mngr.Close()) - assert.NoError(t, mngr.Close()) - assert.Equal(t, mngr.Run(context.Background()), errors.New("runner manager already started")) - }) -} - -func TestAddCloser(t *testing.T) { - t.Parallel() - - tests := map[string]struct { - closers []any - expErr error - }{ - "Add supported closer type": { - closers: []any{func() {}}, - }, - "Add unsupported closer type": { - closers: []any{42}, - expErr: errors.Join(errors.New("unsupported closer type: int")), - }, - "Add various supported closer types": { - closers: []any{new(mockCloser), func(ctx context.Context) error { return nil }, func() error { return nil }, func() {}}, - expErr: nil, - }, - "Add combination of supported and unsupported closer types": { - closers: []any{new(mockCloser), 42}, - expErr: errors.Join(errors.New("unsupported closer type: int")), - }, - } - - for name, test := range tests { - t.Run(name, func(t *testing.T) { - err := NewRunnerCloserManager(nil).AddCloser(test.closers...) - assert.Equalf(t, test.expErr, err, "%v", err) - }) - } - - t.Run("no error if adding a closer during main routine", func(t *testing.T) { - t.Parallel() - - mngr := NewRunnerCloserManager(nil, func(ctx context.Context) error { - <-ctx.Done() - return nil - }) - - ctx, cancel := context.WithCancel(context.Background()) - errCh := make(chan error) - go func() { - errCh <- mngr.Run(ctx) - }() - assert.NoError(t, mngr.AddCloser(func() {})) - cancel() - assert.NoError(t, <-errCh) - }) - - t.Run("should error if closing", func(t *testing.T) { - t.Parallel() - - mngr := NewRunnerCloserManager(nil) - - ctx, cancel := context.WithCancel(context.Background()) - closerCh := make(chan struct{}) - assert.NoError(t, mngr.AddCloser(func() { - cancel() - <-closerCh - })) - - errCh := make(chan error) - go func() { - errCh <- mngr.Run(context.Background()) - }() - - select { - case <-ctx.Done(): - case <-time.After(time.Second * 3): - t.Error("Run() not returned") - } - - closeErrCh := make(chan error) - go func() { - closeErrCh <- mngr.AddCloser(nil) - }() - - close(closerCh) - - select { - case err := <-errCh: - assert.NoError(t, err) - case <-time.After(time.Second * 3): - t.Error("Run() not returned") - } - - select { - case err := <-closeErrCh: - assert.Equal(t, err, errors.New("runner manager already closed")) - case <-time.After(time.Second * 3): - t.Error("AddCloser() not returned") - } - }) - - t.Run("should error if manager already returned", func(t *testing.T) { - t.Parallel() - - mngr := NewRunnerCloserManager(nil) - assert.NoError(t, mngr.Run(context.Background())) - assert.Equal(t, mngr.AddCloser(nil), errors.New("runner manager already closed")) - }) -} - -func TestWaitUntilShutdown(t *testing.T) { - t.Parallel() - - dur := time.Second * 3 - mngr := NewRunnerCloserManager(&dur, func(ctx context.Context) error { - <-ctx.Done() - return nil - }) - - clock := clocktesting.NewFakeClock(time.Now()) - mngr.clock = clock - - shutDownReturned := make(chan struct{}) - go func() { - mngr.WaitUntilShutdown() - close(shutDownReturned) - }() - - returnClose := make(chan struct{}) - assert.NoError(t, mngr.AddCloser(func() { - <-returnClose - })) - - ctx, cancel := context.WithCancel(context.Background()) - - errCh := make(chan error) - go func() { - errCh <- mngr.Run(ctx) - }() - - cancel() - - select { - case <-shutDownReturned: - t.Error("WaitUntilShutdown() returned") - case <-errCh: - t.Error("Run() returned") - default: - } - - assert.Eventually(t, func() bool { - return clock.HasWaiters() - }, time.Second*3, time.Millisecond*100, "fatal shutdown should be waiting") - - select { - case <-shutDownReturned: - t.Error("WaitUntilShutdown() returned") - case <-errCh: - t.Error("Run() returned") - default: - } - - close(returnClose) - - select { - case <-shutDownReturned: - case <-time.After(time.Second * 3): - t.Error("WaitUntilShutdown() not returned") - } - - select { - case err := <-errCh: - assert.NoError(t, err) - case <-time.After(time.Second * 3): - t.Error("Run() not returned") - } -} diff --git a/pkg/concurrency/closer_unit.go b/pkg/concurrency/closer_unit.go deleted file mode 100644 index 38ff1c67e0b..00000000000 --- a/pkg/concurrency/closer_unit.go +++ /dev/null @@ -1,23 +0,0 @@ -//go:build unit -// +build unit - -/* -Copyright 2021 The Dapr Authors -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. -*/ - -package concurrency - -// WithFatalShutdown sets the fatal shutdown function for the closer manager. -// Used for testing. -func (c *RunnerCloserManager) WithFatalShutdown(fn func()) { - c.fatalShutdownFn = fn -} diff --git a/pkg/concurrency/runner.go b/pkg/concurrency/runner.go deleted file mode 100644 index edf4b4553c0..00000000000 --- a/pkg/concurrency/runner.go +++ /dev/null @@ -1,94 +0,0 @@ -/* -Copyright 2023 The Dapr Authors -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. -*/ - -package concurrency - -import ( - "context" - "errors" - "sync" - "sync/atomic" -) - -var ErrManagerAlreadyStarted = errors.New("runner manager already started") - -// Runner is a function that runs a task. -type Runner func(context.Context) error - -// RunnerManager is a manager for runners. It runs all runners in parallel and -// waits for all runners to finish. If any runner returns, the RunnerManager -// will stop all other runners and return any error. -type RunnerManager struct { - lock sync.Mutex - runners []Runner - running atomic.Bool -} - -// NewRunnerManager creates a new RunnerManager. -func NewRunnerManager(runners ...Runner) *RunnerManager { - return &RunnerManager{ - runners: runners, - } -} - -// Add adds a new runner to the RunnerManager. -func (r *RunnerManager) Add(runner ...Runner) error { - if r.running.Load() { - return ErrManagerAlreadyStarted - } - r.lock.Lock() - defer r.lock.Unlock() - r.runners = append(r.runners, runner...) - return nil -} - -// Run runs all runners in parallel and waits for all runners to finish. If any -// runner returns, the RunnerManager will stop all other runners and return any -// error. -func (r *RunnerManager) Run(ctx context.Context) error { - if !r.running.CompareAndSwap(false, true) { - return ErrManagerAlreadyStarted - } - - ctx, cancel := context.WithCancel(ctx) - defer cancel() - - errCh := make(chan error) - for _, runner := range r.runners { - go func(runner Runner) { - // Since the task returned, we need to cancel all other tasks. - // This is a noop if the parent context is already cancelled, or another - // task returned before this one. - defer cancel() - - // Ignore context cancelled errors since errors from a runner manager - // will likely determine the exit code of the program. - // Context cancelled errors are also not really useful to the user in - // this situation. - rErr := runner(ctx) - if rErr != nil && !errors.Is(rErr, context.Canceled) { - errCh <- rErr - return - } - errCh <- nil - }(runner) - } - - // Collect all errors - errObjs := make([]error, len(r.runners)) - for i := 0; i < len(r.runners); i++ { - errObjs[i] = <-errCh - } - - return errors.Join(errObjs...) -} diff --git a/pkg/concurrency/runner_test.go b/pkg/concurrency/runner_test.go deleted file mode 100644 index 89b0f6874c6..00000000000 --- a/pkg/concurrency/runner_test.go +++ /dev/null @@ -1,258 +0,0 @@ -/* -Copyright 2023 The Dapr Authors -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. -*/ - -package concurrency - -import ( - "context" - "errors" - "strings" - "sync/atomic" - "testing" - "time" - - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" -) - -func Test_RunnerManager(t *testing.T) { - t.Parallel() - - t.Run("runner with no tasks should return nil", func(t *testing.T) { - t.Parallel() - - assert.NoError(t, NewRunnerManager().Run(context.Background())) - }) - - t.Run("runner with a task that completes should return nil", func(t *testing.T) { - t.Parallel() - - var i int32 - assert.NoError(t, NewRunnerManager(func(ctx context.Context) error { - atomic.AddInt32(&i, 1) - return nil - }).Run(context.Background())) - assert.Equal(t, int32(1), i) - }) - - t.Run("runner with multiple tasks that complete should return nil", func(t *testing.T) { - t.Parallel() - - var i int32 - assert.NoError(t, NewRunnerManager( - func(ctx context.Context) error { - atomic.AddInt32(&i, 1) - return nil - }, - func(ctx context.Context) error { - atomic.AddInt32(&i, 1) - return nil - }, - func(ctx context.Context) error { - atomic.AddInt32(&i, 1) - return nil - }, - ).Run(context.Background())) - assert.Equal(t, int32(3), i) - }) - - t.Run("a runner that errors should error", func(t *testing.T) { - t.Parallel() - - var i int32 - assert.Error(t, NewRunnerManager( - func(ctx context.Context) error { - atomic.AddInt32(&i, 1) - return errors.New("error") - }, - func(ctx context.Context) error { - atomic.AddInt32(&i, 1) - return nil - }, - func(ctx context.Context) error { - atomic.AddInt32(&i, 1) - return nil - }, - ).Run(context.Background()), errors.New("error")) - assert.Equal(t, int32(3), i) - }) - - t.Run("a runner with multiple errors should collect all errors (string match)", func(t *testing.T) { - t.Parallel() - - var i int32 - err := NewRunnerManager( - func(ctx context.Context) error { - atomic.AddInt32(&i, 1) - return errors.New("error") - }, - func(ctx context.Context) error { - atomic.AddInt32(&i, 1) - return errors.New("error") - }, - func(ctx context.Context) error { - atomic.AddInt32(&i, 1) - return errors.New("error") - }, - ).Run(context.Background()) - require.Error(t, err) - assert.ErrorContains(t, err, "error\nerror\nerror") //nolint:dupword - assert.Equal(t, int32(3), i) - }) - - t.Run("a runner with multiple errors should collect all errors (unique)", func(t *testing.T) { - t.Parallel() - - var i int32 - err := NewRunnerManager( - func(ctx context.Context) error { - atomic.AddInt32(&i, 1) - return errors.New("error1") - }, - func(ctx context.Context) error { - atomic.AddInt32(&i, 1) - return errors.New("error2") - }, - func(ctx context.Context) error { - atomic.AddInt32(&i, 1) - return errors.New("error3") - }, - ).Run(context.Background()) - require.Error(t, err) - assert.ElementsMatch(t, []string{"error1", "error2", "error3"}, strings.Split(err.Error(), "\n")) - assert.Equal(t, int32(3), i) - }) - - t.Run("should be able to add runner with both New and Add", func(t *testing.T) { - t.Parallel() - - var i int32 - mngr := NewRunnerManager( - func(ctx context.Context) error { - atomic.AddInt32(&i, 1) - return nil - }, - ) - assert.NoError(t, mngr.Add( - func(ctx context.Context) error { - atomic.AddInt32(&i, 1) - return nil - }, - )) - assert.NoError(t, mngr.Add( - func(ctx context.Context) error { - atomic.AddInt32(&i, 1) - return nil - }, - )) - assert.NoError(t, mngr.Run(context.Background())) - assert.Equal(t, int32(3), i) - }) - - t.Run("when a runner returns, expect context to be cancelled for other runners", func(t *testing.T) { - t.Parallel() - - var i int32 - assert.NoError(t, NewRunnerManager( - func(ctx context.Context) error { - atomic.AddInt32(&i, 1) - return nil - }, - func(ctx context.Context) error { - atomic.AddInt32(&i, 1) - select { - case <-ctx.Done(): - case <-time.After(time.Second): - t.Error("context should have been cancelled in time") - } - return nil - }, - func(ctx context.Context) error { - atomic.AddInt32(&i, 1) - select { - case <-ctx.Done(): - case <-time.After(time.Second): - t.Error("context should have been cancelled in time") - } - return nil - }, - ).Run(context.Background())) - assert.Equal(t, int32(3), i) - }) - - t.Run("when a runner errors, expect context to be cancelled for other runners", func(t *testing.T) { - t.Parallel() - - var i int32 - err := NewRunnerManager( - func(ctx context.Context) error { - atomic.AddInt32(&i, 1) - select { - case <-ctx.Done(): - case <-time.After(time.Second): - t.Error("context should have been cancelled in time") - } - return errors.New("error1") - }, - func(ctx context.Context) error { - atomic.AddInt32(&i, 1) - select { - case <-ctx.Done(): - case <-time.After(time.Second): - t.Error("context should have been cancelled in time") - } - return errors.New("error2") - }, - func(ctx context.Context) error { - atomic.AddInt32(&i, 1) - return errors.New("error3") - }, - ).Run(context.Background()) - require.Error(t, err) - assert.ElementsMatch(t, []string{"error1", "error2", "error3"}, strings.Split(err.Error(), "\n")) - assert.Equal(t, int32(3), i) - }) - - t.Run("a manger started twice should error", func(t *testing.T) { - t.Parallel() - - var i int32 - m := NewRunnerManager(func(ctx context.Context) error { - atomic.AddInt32(&i, 1) - return nil - }) - assert.NoError(t, m.Run(context.Background())) - assert.Equal(t, int32(1), i) - assert.Error(t, m.Run(context.Background()), errors.New("manager already started")) - assert.Equal(t, int32(1), i) - }) - - t.Run("adding a task to a started manager should error", func(t *testing.T) { - t.Parallel() - - var i int32 - m := NewRunnerManager(func(ctx context.Context) error { - atomic.AddInt32(&i, 1) - return nil - }) - assert.NoError(t, m.Run(context.Background())) - assert.Equal(t, int32(1), i) - err := m.Add(func(ctx context.Context) error { - atomic.AddInt32(&i, 1) - return nil - }) - require.Error(t, err) - assert.ErrorIs(t, err, ErrManagerAlreadyStarted) - assert.Equal(t, int32(1), i) - }) -} diff --git a/pkg/diagnostics/http_monitoring.go b/pkg/diagnostics/http_monitoring.go index 2a3c6744185..f39d37e7ee1 100644 --- a/pkg/diagnostics/http_monitoring.go +++ b/pkg/diagnostics/http_monitoring.go @@ -25,7 +25,7 @@ import ( diagUtils "github.com/dapr/dapr/pkg/diagnostics/utils" "github.com/dapr/dapr/pkg/http/endpoints" - "github.com/dapr/dapr/utils/responsewriter" + "github.com/dapr/dapr/pkg/responsewriter" ) // To track the metrics for fasthttp using opencensus, this implementation is inspired by diff --git a/pkg/diagnostics/http_tracing.go b/pkg/diagnostics/http_tracing.go index 9ab9bb6d4fc..8c03757e017 100644 --- a/pkg/diagnostics/http_tracing.go +++ b/pkg/diagnostics/http_tracing.go @@ -26,7 +26,7 @@ import ( diagConsts "github.com/dapr/dapr/pkg/diagnostics/consts" diagUtils "github.com/dapr/dapr/pkg/diagnostics/utils" "github.com/dapr/dapr/pkg/http/endpoints" - "github.com/dapr/dapr/utils/responsewriter" + "github.com/dapr/dapr/pkg/responsewriter" ) // We have leveraged the code from opencensus-go plugin to adhere the w3c trace context. diff --git a/pkg/diagnostics/http_tracing_test.go b/pkg/diagnostics/http_tracing_test.go index e01398e3c05..049f10fac72 100644 --- a/pkg/diagnostics/http_tracing_test.go +++ b/pkg/diagnostics/http_tracing_test.go @@ -36,7 +36,7 @@ import ( diagConsts "github.com/dapr/dapr/pkg/diagnostics/consts" diagUtils "github.com/dapr/dapr/pkg/diagnostics/utils" "github.com/dapr/dapr/pkg/http/endpoints" - "github.com/dapr/dapr/utils/responsewriter" + "github.com/dapr/dapr/pkg/responsewriter" ) func TestSpanContextFromRequest(t *testing.T) { diff --git a/pkg/grpc/proxy/handler.go b/pkg/grpc/proxy/handler.go index 7ea36c1ce61..dc5e36b5d93 100644 --- a/pkg/grpc/proxy/handler.go +++ b/pkg/grpc/proxy/handler.go @@ -19,7 +19,7 @@ import ( "github.com/dapr/dapr/pkg/diagnostics" "github.com/dapr/dapr/pkg/grpc/proxy/codec" "github.com/dapr/dapr/pkg/resiliency" - "github.com/dapr/dapr/utils" + "github.com/dapr/kit/utils" ) // Metadata header used to indicate if the call should be handled as a gRPC stream. diff --git a/pkg/http/api_crypto.go b/pkg/http/api_crypto.go index 122cb801884..454cf7d5e00 100644 --- a/pkg/http/api_crypto.go +++ b/pkg/http/api_crypto.go @@ -24,9 +24,9 @@ import ( contribCrypto "github.com/dapr/components-contrib/crypto" "github.com/dapr/dapr/pkg/http/endpoints" "github.com/dapr/dapr/pkg/messages" - "github.com/dapr/dapr/utils" "github.com/dapr/kit/ptr" encv1 "github.com/dapr/kit/schemes/enc/v1" + "github.com/dapr/kit/utils" ) const ( diff --git a/pkg/http/endpoints/endpoint.go b/pkg/http/endpoints/endpoint.go index 2a0894e89b6..1cdc8407d21 100644 --- a/pkg/http/endpoints/endpoint.go +++ b/pkg/http/endpoints/endpoint.go @@ -19,7 +19,7 @@ import ( "github.com/valyala/fasthttp" - "github.com/dapr/dapr/utils/nethttpadaptor" + "github.com/dapr/dapr/pkg/nethttpadaptor" ) // Endpoint is a collection of route information for an Dapr API. diff --git a/pkg/http/middlewares.go b/pkg/http/middlewares.go index cfc68eaec21..29307bf52a0 100644 --- a/pkg/http/middlewares.go +++ b/pkg/http/middlewares.go @@ -22,7 +22,7 @@ import ( chi "github.com/go-chi/chi/v5" securityConsts "github.com/dapr/dapr/pkg/security/consts" - "github.com/dapr/dapr/utils/streams" + "github.com/dapr/kit/streams" ) // MaxBodySizeMiddleware limits the body size to the given size (in bytes). diff --git a/pkg/http/server.go b/pkg/http/server.go index 17446c979c7..f0512b3dcdd 100644 --- a/pkg/http/server.go +++ b/pkg/http/server.go @@ -44,10 +44,10 @@ import ( diagUtils "github.com/dapr/dapr/pkg/diagnostics/utils" "github.com/dapr/dapr/pkg/http/endpoints" httpMiddleware "github.com/dapr/dapr/pkg/middleware/http" + "github.com/dapr/dapr/pkg/responsewriter" "github.com/dapr/dapr/pkg/security" - "github.com/dapr/dapr/utils" - "github.com/dapr/dapr/utils/responsewriter" "github.com/dapr/kit/logger" + "github.com/dapr/kit/utils" ) var ( diff --git a/pkg/httpendpoint/http_endpoint.go b/pkg/httpendpoint/http_endpoint.go index 2734335d2f7..87317530266 100644 --- a/pkg/httpendpoint/http_endpoint.go +++ b/pkg/httpendpoint/http_endpoint.go @@ -17,13 +17,12 @@ import ( "os" "path/filepath" + "gopkg.in/yaml.v3" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" httpEndpointV1alpha1 "github.com/dapr/dapr/pkg/apis/httpEndpoint/v1alpha1" - "github.com/dapr/dapr/utils" "github.com/dapr/kit/logger" - - "gopkg.in/yaml.v3" + "github.com/dapr/kit/utils" ) const ( diff --git a/pkg/injector/patcher/sidecar.go b/pkg/injector/patcher/sidecar.go index 84b9ccd8372..6bc9d2df439 100644 --- a/pkg/injector/patcher/sidecar.go +++ b/pkg/injector/patcher/sidecar.go @@ -22,7 +22,7 @@ import ( corev1 "k8s.io/api/core/v1" injectorConsts "github.com/dapr/dapr/pkg/injector/consts" - "github.com/dapr/dapr/utils" + "github.com/dapr/kit/utils" ) // GetInjectedComponentContainersFn is a function that returns the list of component containers for a given appID and namespace. diff --git a/pkg/injector/service/config.go b/pkg/injector/service/config.go index 3051e3c4bbc..31aaa602d63 100644 --- a/pkg/injector/service/config.go +++ b/pkg/injector/service/config.go @@ -20,6 +20,7 @@ import ( corev1 "k8s.io/api/core/v1" "github.com/dapr/dapr/utils" + kitutils "github.com/dapr/kit/utils" ) // Config represents configuration options for the Dapr Sidecar Injector webhook server. @@ -101,7 +102,7 @@ func (c *Config) GetRunAsNonRoot() bool { if c.RunAsNonRoot == "" { return true } - return utils.IsTruthy(c.RunAsNonRoot) + return kitutils.IsTruthy(c.RunAsNonRoot) } func (c *Config) GetReadOnlyRootFilesystem() bool { @@ -109,17 +110,17 @@ func (c *Config) GetReadOnlyRootFilesystem() bool { if c.ReadOnlyRootFilesystem == "" { return true } - return utils.IsTruthy(c.ReadOnlyRootFilesystem) + return kitutils.IsTruthy(c.ReadOnlyRootFilesystem) } func (c *Config) GetDropCapabilities() bool { // Default is false if empty - return utils.IsTruthy(c.SidecarDropALLCapabilities) + return kitutils.IsTruthy(c.SidecarDropALLCapabilities) } func (c *Config) GetSkipPlacement() bool { // Default is false if empty - return utils.IsTruthy(c.SkipPlacement) + return kitutils.IsTruthy(c.SkipPlacement) } func (c *Config) parseTolerationsJSON() { diff --git a/pkg/messaging/v1/replayable_request.go b/pkg/messaging/v1/replayable_request.go index 073547b3a4e..bc2014cc785 100644 --- a/pkg/messaging/v1/replayable_request.go +++ b/pkg/messaging/v1/replayable_request.go @@ -18,8 +18,8 @@ import ( "io" "sync" - "github.com/dapr/dapr/utils" - streamutils "github.com/dapr/dapr/utils/streams" + "github.com/dapr/kit/byteslicepool" + streamutils "github.com/dapr/kit/streams" ) // Minimum capacity for the slices is 2KB @@ -29,7 +29,7 @@ const minByteSliceCapacity = 2 << 10 // Used to reduce the number of allocations in replayableRequest for buffers and relieve pressure on the GC. var ( bufPool = sync.Pool{New: newBuffer} - bsPool = utils.NewByteSlicePool(minByteSliceCapacity) + bsPool = byteslicepool.NewByteSlicePool(minByteSliceCapacity) ) func newBuffer() any { diff --git a/utils/nethttpadaptor/nethttpadaptor.go b/pkg/nethttpadaptor/nethttpadaptor.go similarity index 100% rename from utils/nethttpadaptor/nethttpadaptor.go rename to pkg/nethttpadaptor/nethttpadaptor.go diff --git a/utils/nethttpadaptor/nethttpadaptor_test.go b/pkg/nethttpadaptor/nethttpadaptor_test.go similarity index 100% rename from utils/nethttpadaptor/nethttpadaptor_test.go rename to pkg/nethttpadaptor/nethttpadaptor_test.go diff --git a/pkg/operator/handlers/dapr_handler.go b/pkg/operator/handlers/dapr_handler.go index f1448d3153e..7c7ec98e504 100644 --- a/pkg/operator/handlers/dapr_handler.go +++ b/pkg/operator/handlers/dapr_handler.go @@ -21,8 +21,8 @@ import ( "github.com/dapr/dapr/pkg/operator/meta" "github.com/dapr/dapr/pkg/operator/monitoring" "github.com/dapr/dapr/pkg/validation" - "github.com/dapr/dapr/utils" "github.com/dapr/kit/logger" + "github.com/dapr/kit/utils" ) const ( diff --git a/pkg/operator/meta/meta.go b/pkg/operator/meta/meta.go index 0913828f87c..6af1563a330 100644 --- a/pkg/operator/meta/meta.go +++ b/pkg/operator/meta/meta.go @@ -3,7 +3,7 @@ package meta import ( "github.com/dapr/dapr/pkg/injector/annotations" injectorConsts "github.com/dapr/dapr/pkg/injector/consts" - "github.com/dapr/dapr/utils" + "github.com/dapr/kit/utils" ) // IsAnnotatedForDapr whether the dapr enabled annotation is present and true. diff --git a/pkg/operator/operator.go b/pkg/operator/operator.go index 9eee4433964..45ee8cb210f 100644 --- a/pkg/operator/operator.go +++ b/pkg/operator/operator.go @@ -41,13 +41,13 @@ import ( resiliencyapi "github.com/dapr/dapr/pkg/apis/resiliency/v1alpha1" subscriptionsapiV1alpha1 "github.com/dapr/dapr/pkg/apis/subscriptions/v1alpha1" subscriptionsapiV2alpha1 "github.com/dapr/dapr/pkg/apis/subscriptions/v2alpha1" - "github.com/dapr/dapr/pkg/concurrency" "github.com/dapr/dapr/pkg/health" "github.com/dapr/dapr/pkg/modes" "github.com/dapr/dapr/pkg/operator/api" operatorcache "github.com/dapr/dapr/pkg/operator/cache" "github.com/dapr/dapr/pkg/operator/handlers" "github.com/dapr/dapr/pkg/security" + "github.com/dapr/kit/concurrency" "github.com/dapr/kit/logger" ) diff --git a/pkg/operator/watchdog.go b/pkg/operator/watchdog.go index fae4e275b21..725bcc7ddbd 100644 --- a/pkg/operator/watchdog.go +++ b/pkg/operator/watchdog.go @@ -15,7 +15,7 @@ import ( injectorConsts "github.com/dapr/dapr/pkg/injector/consts" operatorConsts "github.com/dapr/dapr/pkg/operator/meta" - "github.com/dapr/dapr/utils" + "github.com/dapr/kit/utils" ) const ( diff --git a/utils/ports.go b/pkg/ports/ports.go similarity index 99% rename from utils/ports.go rename to pkg/ports/ports.go index e9fd7acfefa..d4c9182a854 100644 --- a/utils/ports.go +++ b/pkg/ports/ports.go @@ -11,7 +11,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package utils +package ports import ( "crypto/sha256" diff --git a/utils/ports_test.go b/pkg/ports/ports_test.go similarity index 99% rename from utils/ports_test.go rename to pkg/ports/ports_test.go index cd1f3fa9c4d..c18edeb0356 100644 --- a/utils/ports_test.go +++ b/pkg/ports/ports_test.go @@ -11,7 +11,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package utils +package ports import ( "net" diff --git a/pkg/resiliency/resiliency.go b/pkg/resiliency/resiliency.go index 878ea0008f7..76691a07af1 100644 --- a/pkg/resiliency/resiliency.go +++ b/pkg/resiliency/resiliency.go @@ -33,10 +33,10 @@ import ( diag "github.com/dapr/dapr/pkg/diagnostics" operatorv1pb "github.com/dapr/dapr/pkg/proto/operator/v1" "github.com/dapr/dapr/pkg/resiliency/breaker" - "github.com/dapr/dapr/utils" "github.com/dapr/kit/config" "github.com/dapr/kit/logger" "github.com/dapr/kit/retry" + "github.com/dapr/kit/utils" ) const ( diff --git a/utils/responsewriter/README.md b/pkg/responsewriter/README.md similarity index 100% rename from utils/responsewriter/README.md rename to pkg/responsewriter/README.md diff --git a/utils/responsewriter/response_writer.go b/pkg/responsewriter/response_writer.go similarity index 100% rename from utils/responsewriter/response_writer.go rename to pkg/responsewriter/response_writer.go diff --git a/utils/responsewriter/response_writer_test.go b/pkg/responsewriter/response_writer_test.go similarity index 100% rename from utils/responsewriter/response_writer_test.go rename to pkg/responsewriter/response_writer_test.go diff --git a/pkg/runtime/config.go b/pkg/runtime/config.go index bd03b95efdf..98fcd811eef 100644 --- a/pkg/runtime/config.go +++ b/pkg/runtime/config.go @@ -31,6 +31,7 @@ import ( "github.com/dapr/dapr/pkg/metrics" "github.com/dapr/dapr/pkg/modes" "github.com/dapr/dapr/pkg/operator/client" + "github.com/dapr/dapr/pkg/ports" operatorV1 "github.com/dapr/dapr/pkg/proto/operator/v1" resiliencyConfig "github.com/dapr/dapr/pkg/resiliency" rterrors "github.com/dapr/dapr/pkg/runtime/errors" @@ -323,7 +324,7 @@ func (c *Config) toInternal() (*internalConfig, error) { // acquired using a deterministic algorithm that returns the same value if // the same app is restarted // Otherwise, the port will be random. - intc.internalGRPCPort, err = utils.GetStablePort(47300, intc.id) + intc.internalGRPCPort, err = ports.GetStablePort(47300, intc.id) if err != nil { return nil, fmt.Errorf("failed to get free port for internal grpc server: %w", err) } diff --git a/pkg/runtime/processor/state/state.go b/pkg/runtime/processor/state/state.go index 3c49332506f..0d4f2a99fde 100644 --- a/pkg/runtime/processor/state/state.go +++ b/pkg/runtime/processor/state/state.go @@ -29,8 +29,8 @@ import ( "github.com/dapr/dapr/pkg/runtime/compstore" rterrors "github.com/dapr/dapr/pkg/runtime/errors" "github.com/dapr/dapr/pkg/runtime/meta" - "github.com/dapr/dapr/utils" "github.com/dapr/kit/logger" + "github.com/dapr/kit/utils" ) const ( diff --git a/pkg/runtime/pubsub/outbox.go b/pkg/runtime/pubsub/outbox.go index a0a9dafee1f..a8ef55f0169 100644 --- a/pkg/runtime/pubsub/outbox.go +++ b/pkg/runtime/pubsub/outbox.go @@ -27,8 +27,8 @@ import ( "github.com/dapr/components-contrib/state" "github.com/dapr/dapr/pkg/apis/components/v1alpha1" "github.com/dapr/dapr/pkg/outbox" - "github.com/dapr/dapr/utils" "github.com/dapr/kit/logger" + "github.com/dapr/kit/utils" ) const ( diff --git a/pkg/runtime/pubsub/subscriptions.go b/pkg/runtime/pubsub/subscriptions.go index 718c7ee7aa0..d8e1f083142 100644 --- a/pkg/runtime/pubsub/subscriptions.go +++ b/pkg/runtime/pubsub/subscriptions.go @@ -24,8 +24,8 @@ import ( operatorv1pb "github.com/dapr/dapr/pkg/proto/operator/v1" runtimev1pb "github.com/dapr/dapr/pkg/proto/runtime/v1" "github.com/dapr/dapr/pkg/resiliency" - "github.com/dapr/dapr/utils" "github.com/dapr/kit/logger" + "github.com/dapr/kit/utils" ) const ( diff --git a/pkg/runtime/runtime.go b/pkg/runtime/runtime.go index 6f5be98201f..52142158e00 100644 --- a/pkg/runtime/runtime.go +++ b/pkg/runtime/runtime.go @@ -41,13 +41,17 @@ import ( apiextensionsV1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + nr "github.com/dapr/components-contrib/nameresolution" + "github.com/dapr/components-contrib/secretstores" + "github.com/dapr/components-contrib/state" "github.com/dapr/dapr/pkg/actors" commonapi "github.com/dapr/dapr/pkg/apis/common" componentsV1alpha1 "github.com/dapr/dapr/pkg/apis/components/v1alpha1" httpEndpointV1alpha1 "github.com/dapr/dapr/pkg/apis/httpEndpoint/v1alpha1" "github.com/dapr/dapr/pkg/apphealth" "github.com/dapr/dapr/pkg/components" - "github.com/dapr/dapr/pkg/concurrency" + "github.com/dapr/dapr/pkg/components/pluggable" + secretstoresLoader "github.com/dapr/dapr/pkg/components/secretstores" "github.com/dapr/dapr/pkg/config" "github.com/dapr/dapr/pkg/config/protocol" diag "github.com/dapr/dapr/pkg/diagnostics" @@ -66,6 +70,8 @@ import ( operatorv1pb "github.com/dapr/dapr/pkg/proto/operator/v1" "github.com/dapr/dapr/pkg/resiliency" "github.com/dapr/dapr/pkg/runtime/channels" + "github.com/dapr/dapr/pkg/runtime/compstore" + rterrors "github.com/dapr/dapr/pkg/runtime/errors" "github.com/dapr/dapr/pkg/runtime/meta" "github.com/dapr/dapr/pkg/runtime/processor" "github.com/dapr/dapr/pkg/runtime/registry" @@ -73,16 +79,8 @@ import ( "github.com/dapr/dapr/pkg/security" securityConsts "github.com/dapr/dapr/pkg/security/consts" "github.com/dapr/dapr/utils" + "github.com/dapr/kit/concurrency" "github.com/dapr/kit/logger" - - "github.com/dapr/dapr/pkg/components/pluggable" - secretstoresLoader "github.com/dapr/dapr/pkg/components/secretstores" - "github.com/dapr/dapr/pkg/runtime/compstore" - rterrors "github.com/dapr/dapr/pkg/runtime/errors" - - nr "github.com/dapr/components-contrib/nameresolution" - "github.com/dapr/components-contrib/secretstores" - "github.com/dapr/components-contrib/state" ) const ( diff --git a/pkg/security/security.go b/pkg/security/security.go index d7091ed632d..dc0797151e0 100644 --- a/pkg/security/security.go +++ b/pkg/security/security.go @@ -33,10 +33,10 @@ import ( "google.golang.org/grpc/credentials/insecure" "k8s.io/utils/clock" - "github.com/dapr/dapr/pkg/concurrency" "github.com/dapr/dapr/pkg/diagnostics" "github.com/dapr/dapr/pkg/modes" "github.com/dapr/dapr/pkg/security/legacy" + "github.com/dapr/kit/concurrency" "github.com/dapr/kit/fswatcher" "github.com/dapr/kit/logger" ) diff --git a/pkg/sentry/sentry.go b/pkg/sentry/sentry.go index 08547e98c16..99ef0d292a6 100644 --- a/pkg/sentry/sentry.go +++ b/pkg/sentry/sentry.go @@ -24,7 +24,6 @@ import ( "github.com/spiffe/go-spiffe/v2/spiffeid" - "github.com/dapr/dapr/pkg/concurrency" sentryv1pb "github.com/dapr/dapr/pkg/proto/sentry/v1" "github.com/dapr/dapr/pkg/security" "github.com/dapr/dapr/pkg/sentry/config" @@ -36,6 +35,7 @@ import ( validatorJWKS "github.com/dapr/dapr/pkg/sentry/server/validator/jwks" validatorKube "github.com/dapr/dapr/pkg/sentry/server/validator/kubernetes" "github.com/dapr/dapr/utils" + "github.com/dapr/kit/concurrency" "github.com/dapr/kit/logger" ) diff --git a/pkg/signals/signals.go b/pkg/signals/signals.go deleted file mode 100644 index 64dc9754816..00000000000 --- a/pkg/signals/signals.go +++ /dev/null @@ -1,56 +0,0 @@ -/* -Copyright 2023 The Dapr Authors -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. -*/ - -package signals - -import ( - "context" - "os" - "os/signal" - - "github.com/dapr/kit/logger" -) - -var ( - log = logger.NewLogger("dapr.signals") - - // Inspired by - // https://github.com/kubernetes-sigs/controller-runtime/blob/8499b67e316a03b260c73f92d0380de8cd2e97a1/pkg/manager/signals/signal.go#L25 - onlyOneSignalHandler = make(chan struct{}) -) - -// Context returns a context which will be canceled when either the SIGINT -// (windows and non windows) or SIGTERM (non windows) signal is caught. If -// either signal is caught a second time, the program is terminated immediately -// with exit code 1. -func Context() context.Context { - // panics when called twice - close(onlyOneSignalHandler) - - ctx, cancel := context.WithCancel(context.Background()) - sigCh := make(chan os.Signal, 1) - signal.Notify(sigCh, shutdownSignals...) - - go func() { - sig := <-sigCh - log.Infof(`Received signal '%s'; beginning shutdown`, sig) - cancel() - sig = <-sigCh - log.Fatalf( - `Received signal '%s' during shutdown; exiting immediately`, - sig, - ) - }() - - return ctx -} diff --git a/pkg/signals/signals_posix.go b/pkg/signals/signals_posix.go deleted file mode 100644 index 7b7a90f72ca..00000000000 --- a/pkg/signals/signals_posix.go +++ /dev/null @@ -1,24 +0,0 @@ -//go:build !windows -// +build !windows - -/* -Copyright 2023 The Dapr Authors -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. -*/ - -package signals - -import ( - "os" - "syscall" -) - -var shutdownSignals = []os.Signal{os.Interrupt, syscall.SIGTERM} diff --git a/pkg/signals/signals_posix_test.go b/pkg/signals/signals_posix_test.go deleted file mode 100644 index 230a9fadd88..00000000000 --- a/pkg/signals/signals_posix_test.go +++ /dev/null @@ -1,45 +0,0 @@ -//go:build !windows -// +build !windows - -/* -Copyright 2023 The Dapr Authors -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. -*/ - -package signals - -// Note this file is not built on Windows, as we depend on syscall methods not available on Windows. - -import ( - "os/signal" - "syscall" - "testing" - "time" - - "github.com/stretchr/testify/assert" -) - -func TestContext(t *testing.T) { - signal.Reset() - - t.Run("if receive signal, should cancel context", func(t *testing.T) { - defer signal.Reset() - onlyOneSignalHandler = make(chan struct{}) - - ctx := Context() - assert.NoError(t, syscall.Kill(syscall.Getpid(), syscall.SIGINT)) - select { - case <-ctx.Done(): - case <-time.After(1 * time.Second): - t.Error("context should be cancelled in time") - } - }) -} diff --git a/pkg/signals/signals_windows.go b/pkg/signals/signals_windows.go deleted file mode 100644 index 8248b204ab7..00000000000 --- a/pkg/signals/signals_windows.go +++ /dev/null @@ -1,20 +0,0 @@ -/* -Copyright 2023 The Dapr Authors -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. -*/ - -package signals - -import ( - "os" -) - -var shutdownSignals = []os.Signal{os.Interrupt} diff --git a/utils/streams/testutils.go b/pkg/testing/error_reader.go similarity index 98% rename from utils/streams/testutils.go rename to pkg/testing/error_reader.go index ae48c882b70..c97c45f85d7 100644 --- a/utils/streams/testutils.go +++ b/pkg/testing/error_reader.go @@ -15,7 +15,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package streams +package testing import ( "io" diff --git a/tests/apps/resiliencyapp/go.mod b/tests/apps/resiliencyapp/go.mod index 8e5fd469697..a225d171146 100644 --- a/tests/apps/resiliencyapp/go.mod +++ b/tests/apps/resiliencyapp/go.mod @@ -15,10 +15,10 @@ require ( github.com/google/uuid v1.3.1 // indirect go.opentelemetry.io/otel v1.16.0 // indirect go.opentelemetry.io/otel/trace v1.16.0 // indirect - golang.org/x/net v0.15.0 // indirect - golang.org/x/sys v0.12.0 // indirect + golang.org/x/net v0.16.0 // indirect + golang.org/x/sys v0.13.0 // indirect golang.org/x/text v0.13.0 // indirect - google.golang.org/genproto/googleapis/rpc v0.0.0-20230807174057-1744710a1577 // indirect + google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d // indirect ) replace github.com/dapr/dapr => ../../../ diff --git a/tests/apps/resiliencyapp/go.sum b/tests/apps/resiliencyapp/go.sum index 5b8a377578d..5c92c921116 100644 --- a/tests/apps/resiliencyapp/go.sum +++ b/tests/apps/resiliencyapp/go.sum @@ -49,7 +49,7 @@ go.opentelemetry.io/otel/trace v1.16.0 h1:8JRpaObFoW0pxuVPapkgH8UhHQj+bJW8jJsCZE go.opentelemetry.io/otel/trace v1.16.0/go.mod h1:Yt9vYq1SdNz3xdjZZK7wcXv1qv2pwLkqr2QVwea0ef0= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= -golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g= +golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI= golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= @@ -58,8 +58,8 @@ golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73r golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= -golang.org/x/net v0.15.0 h1:ugBLEUaxABaB5AJqW9enI0ACdci2RUd4eP51NTBvuJ8= -golang.org/x/net v0.15.0/go.mod h1:idbUs1IY1+zTqbi8yxTbhexhEEk5ur9LInksu6HrEpk= +golang.org/x/net v0.16.0 h1:7eBu7KsSvFDtSXUIDbh3aqlK4DPsZ1rByC8PFfBThos= +golang.org/x/net v0.16.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= @@ -68,8 +68,8 @@ golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o= -golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE= +golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k= golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= @@ -84,8 +84,8 @@ google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoA google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc= google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEYHJ7i3ixzK3sjbqSGDJWnxyFXZblF3eUsNvo= google.golang.org/genproto v0.0.0-20200806141610-86f49bd18e98/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no= -google.golang.org/genproto/googleapis/rpc v0.0.0-20230807174057-1744710a1577 h1:wukfNtZmZUurLN/atp2hiIeTKn7QJWIQdHzqmsOnAOk= -google.golang.org/genproto/googleapis/rpc v0.0.0-20230807174057-1744710a1577/go.mod h1:+Bk1OCOj40wS2hwAMA+aCW9ypzm63QTBBHp6lQ3p+9M= +google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d h1:uvYuEyMHKNt+lT4K3bN6fGswmK8qSvcreM3BwjDh+y4= +google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d/go.mod h1:+Bk1OCOj40wS2hwAMA+aCW9ypzm63QTBBHp6lQ3p+9M= google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg= google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQciAY= diff --git a/tests/apps/resiliencyapp_grpc/go.mod b/tests/apps/resiliencyapp_grpc/go.mod index bd4f8e6ad2d..fb1c4a98701 100644 --- a/tests/apps/resiliencyapp_grpc/go.mod +++ b/tests/apps/resiliencyapp_grpc/go.mod @@ -13,10 +13,10 @@ require ( github.com/golang/protobuf v1.5.3 // indirect go.opentelemetry.io/otel v1.16.0 // indirect go.opentelemetry.io/otel/trace v1.16.0 // indirect - golang.org/x/net v0.15.0 // indirect - golang.org/x/sys v0.12.0 // indirect + golang.org/x/net v0.16.0 // indirect + golang.org/x/sys v0.13.0 // indirect golang.org/x/text v0.13.0 // indirect - google.golang.org/genproto/googleapis/rpc v0.0.0-20230807174057-1744710a1577 // indirect + google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d // indirect ) replace github.com/dapr/dapr => ../../../ diff --git a/tests/apps/resiliencyapp_grpc/go.sum b/tests/apps/resiliencyapp_grpc/go.sum index 6abce21348d..2e4cea5f883 100644 --- a/tests/apps/resiliencyapp_grpc/go.sum +++ b/tests/apps/resiliencyapp_grpc/go.sum @@ -45,7 +45,7 @@ go.opentelemetry.io/otel/trace v1.16.0 h1:8JRpaObFoW0pxuVPapkgH8UhHQj+bJW8jJsCZE go.opentelemetry.io/otel/trace v1.16.0/go.mod h1:Yt9vYq1SdNz3xdjZZK7wcXv1qv2pwLkqr2QVwea0ef0= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= -golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g= +golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI= golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= @@ -54,8 +54,8 @@ golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73r golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= -golang.org/x/net v0.15.0 h1:ugBLEUaxABaB5AJqW9enI0ACdci2RUd4eP51NTBvuJ8= -golang.org/x/net v0.15.0/go.mod h1:idbUs1IY1+zTqbi8yxTbhexhEEk5ur9LInksu6HrEpk= +golang.org/x/net v0.16.0 h1:7eBu7KsSvFDtSXUIDbh3aqlK4DPsZ1rByC8PFfBThos= +golang.org/x/net v0.16.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= @@ -64,8 +64,8 @@ golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o= -golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE= +golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k= golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= @@ -80,8 +80,8 @@ google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoA google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc= google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEYHJ7i3ixzK3sjbqSGDJWnxyFXZblF3eUsNvo= google.golang.org/genproto v0.0.0-20200806141610-86f49bd18e98/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no= -google.golang.org/genproto/googleapis/rpc v0.0.0-20230807174057-1744710a1577 h1:wukfNtZmZUurLN/atp2hiIeTKn7QJWIQdHzqmsOnAOk= -google.golang.org/genproto/googleapis/rpc v0.0.0-20230807174057-1744710a1577/go.mod h1:+Bk1OCOj40wS2hwAMA+aCW9ypzm63QTBBHp6lQ3p+9M= +google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d h1:uvYuEyMHKNt+lT4K3bN6fGswmK8qSvcreM3BwjDh+y4= +google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d/go.mod h1:+Bk1OCOj40wS2hwAMA+aCW9ypzm63QTBBHp6lQ3p+9M= google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg= google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQciAY= diff --git a/tests/apps/service_invocation_grpc_proxy_client/go.mod b/tests/apps/service_invocation_grpc_proxy_client/go.mod index 71ee558b744..c95d3b4ac7e 100644 --- a/tests/apps/service_invocation_grpc_proxy_client/go.mod +++ b/tests/apps/service_invocation_grpc_proxy_client/go.mod @@ -14,10 +14,10 @@ require ( github.com/google/uuid v1.3.1 // indirect go.opentelemetry.io/otel v1.16.0 // indirect go.opentelemetry.io/otel/trace v1.16.0 // indirect - golang.org/x/net v0.15.0 // indirect - golang.org/x/sys v0.12.0 // indirect + golang.org/x/net v0.16.0 // indirect + golang.org/x/sys v0.13.0 // indirect golang.org/x/text v0.13.0 // indirect - google.golang.org/genproto/googleapis/rpc v0.0.0-20230807174057-1744710a1577 // indirect + google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d // indirect google.golang.org/protobuf v1.31.0 // indirect ) diff --git a/tests/apps/service_invocation_grpc_proxy_client/go.sum b/tests/apps/service_invocation_grpc_proxy_client/go.sum index 76dff4e1642..18452578df5 100644 --- a/tests/apps/service_invocation_grpc_proxy_client/go.sum +++ b/tests/apps/service_invocation_grpc_proxy_client/go.sum @@ -49,7 +49,7 @@ go.opentelemetry.io/otel/trace v1.16.0 h1:8JRpaObFoW0pxuVPapkgH8UhHQj+bJW8jJsCZE go.opentelemetry.io/otel/trace v1.16.0/go.mod h1:Yt9vYq1SdNz3xdjZZK7wcXv1qv2pwLkqr2QVwea0ef0= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= -golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g= +golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI= golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= @@ -58,8 +58,8 @@ golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73r golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= -golang.org/x/net v0.15.0 h1:ugBLEUaxABaB5AJqW9enI0ACdci2RUd4eP51NTBvuJ8= -golang.org/x/net v0.15.0/go.mod h1:idbUs1IY1+zTqbi8yxTbhexhEEk5ur9LInksu6HrEpk= +golang.org/x/net v0.16.0 h1:7eBu7KsSvFDtSXUIDbh3aqlK4DPsZ1rByC8PFfBThos= +golang.org/x/net v0.16.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= @@ -68,8 +68,8 @@ golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o= -golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE= +golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k= golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= @@ -84,8 +84,8 @@ google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoA google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc= google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEYHJ7i3ixzK3sjbqSGDJWnxyFXZblF3eUsNvo= google.golang.org/genproto v0.0.0-20200806141610-86f49bd18e98/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no= -google.golang.org/genproto/googleapis/rpc v0.0.0-20230807174057-1744710a1577 h1:wukfNtZmZUurLN/atp2hiIeTKn7QJWIQdHzqmsOnAOk= -google.golang.org/genproto/googleapis/rpc v0.0.0-20230807174057-1744710a1577/go.mod h1:+Bk1OCOj40wS2hwAMA+aCW9ypzm63QTBBHp6lQ3p+9M= +google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d h1:uvYuEyMHKNt+lT4K3bN6fGswmK8qSvcreM3BwjDh+y4= +google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d/go.mod h1:+Bk1OCOj40wS2hwAMA+aCW9ypzm63QTBBHp6lQ3p+9M= google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg= google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQciAY= diff --git a/tests/e2e/utils/helpers.go b/tests/e2e/utils/helpers.go index f2c96bbdd2d..c35301cc080 100644 --- a/tests/e2e/utils/helpers.go +++ b/tests/e2e/utils/helpers.go @@ -18,7 +18,6 @@ import ( "fmt" "os" "runtime" - "strings" "time" guuid "github.com/google/uuid" @@ -93,17 +92,6 @@ func FormatDuration(d time.Duration) string { return fmt.Sprintf("%dms", d.Truncate(100*time.Microsecond).Milliseconds()) } -// IsTruthy returns true if a string is a truthy value. -// Truthy values are "y", "yes", "true", "t", "on", "1" (case-insensitive); everything else is false. -func IsTruthy(val string) bool { - switch strings.ToLower(strings.TrimSpace(val)) { - case "y", "yes", "true", "t", "on", "1": - return true - default: - return false - } -} - // HealthCheckApps performs healthchecks for multiple apps, waiting for them to be ready. func HealthCheckApps(urls ...string) error { count := len(urls) diff --git a/tests/e2e/utils/http.go b/tests/e2e/utils/http.go index 87038e2d8f4..b7bcc80a4da 100644 --- a/tests/e2e/utils/http.go +++ b/tests/e2e/utils/http.go @@ -25,6 +25,8 @@ import ( "time" "golang.org/x/net/http2" + + "github.com/dapr/dapr/tests/apps/utils" ) const ( @@ -45,7 +47,7 @@ func InitHTTPClient(allowHTTP2 bool) { // This should not be used except in rare circumstances. Developers should use the shared httpClient instead to re-use sockets as much as possible. func NewHTTPClient(allowHTTP2 bool) *http.Client { // HTTP/2 is allowed only if the DAPR_TESTS_HTTP2 env var is set - allowHTTP2 = allowHTTP2 && IsTruthy(os.Getenv("DAPR_TESTS_HTTP2")) + allowHTTP2 = allowHTTP2 && utils.IsTruthy(os.Getenv("DAPR_TESTS_HTTP2")) if allowHTTP2 { return &http.Client{ diff --git a/tests/platforms/kubernetes/app_description.go b/tests/platforms/kubernetes/app_description.go index c139fd6dcfa..c5cd3bf75ba 100644 --- a/tests/platforms/kubernetes/app_description.go +++ b/tests/platforms/kubernetes/app_description.go @@ -19,7 +19,7 @@ import ( apiv1 "k8s.io/api/core/v1" - "github.com/dapr/dapr/utils" + "github.com/dapr/kit/utils" ) const ( diff --git a/tests/platforms/kubernetes/kubeobj.go b/tests/platforms/kubernetes/kubeobj.go index c8107a3553b..ae145a19ba4 100644 --- a/tests/platforms/kubernetes/kubeobj.go +++ b/tests/platforms/kubernetes/kubeobj.go @@ -27,7 +27,7 @@ import ( commonapi "github.com/dapr/dapr/pkg/apis/common" v1alpha1 "github.com/dapr/dapr/pkg/apis/components/v1alpha1" - "github.com/dapr/dapr/utils" + "github.com/dapr/kit/utils" ) const ( diff --git a/utils/byteslicepool.go b/utils/byteslicepool.go deleted file mode 100644 index b836e053eef..00000000000 --- a/utils/byteslicepool.go +++ /dev/null @@ -1,85 +0,0 @@ -/* -Copyright 2021 The Dapr Authors -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. -*/ - -package utils - -import ( - "sync" -) - -/* -Originally based on https://github.com/xdg-go/zzz-slice-recycling -Copyright (C) 2019 by David A. Golden -License (Apache2): https://github.com/xdg-go/zzz-slice-recycling/blob/master/LICENSE -*/ - -// ByteSlicePool is a wrapper around sync.Pool to get []byte objects with a given capacity. -type ByteSlicePool struct { - MinCap int - pool *sync.Pool -} - -// NewByteSlicePool returns a new ByteSlicePool object. -func NewByteSlicePool(minCap int) *ByteSlicePool { - return &ByteSlicePool{ - MinCap: minCap, - pool: &sync.Pool{}, - } -} - -// Get a slice from the pool. -// The cap parameter is used only if we need to allocate a new byte slice; there's no guarantee a slice retrieved from the pool will have enough capacity for that. -func (sp ByteSlicePool) Get(cap int) []byte { - bp := sp.pool.Get() - if bp == nil { - if cap < sp.MinCap { - cap = sp.MinCap - } - return make([]byte, 0, cap) - } - buf := bp.([]byte) - // This will be optimized by the compiler - for i := range buf { - buf[i] = 0 - } - return buf[0:0] -} - -// Put a slice back in the pool. -func (sp ByteSlicePool) Put(bs []byte) { - // The linter here complains because we're putting a slice rather than a pointer in the pool. - // The complain is valid, because doing so does cause an allocation for the local copy of the slice header. - // However, this is ok for us because given how we use ByteSlicePool, we can't keep around the pointer we took out. - // See this thread for some discussion: https://github.com/dominikh/go-tools/issues/1336 - //nolint:staticcheck - sp.pool.Put(bs) -} - -// Resize a byte slice, making sure that it has enough capacity for a given size. -func (sp ByteSlicePool) Resize(orig []byte, size int) []byte { - if size < cap(orig) { - return orig[0:size] - } - - // Allocate a new byte slice and then discard the old one, too small, so it can be garbage collected - temp := make([]byte, size, max(size, cap(orig)*2)) - copy(temp, orig) - return temp -} - -func max(x, y int) int { - if x < y { - return y - } - return x -} diff --git a/utils/byteslicepool_test.go b/utils/byteslicepool_test.go deleted file mode 100644 index 0c1441d1990..00000000000 --- a/utils/byteslicepool_test.go +++ /dev/null @@ -1,59 +0,0 @@ -/* -Copyright 2021 The Dapr Authors -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. -*/ - -package utils - -import ( - "testing" - - "github.com/stretchr/testify/assert" -) - -func TestByteSlicePool(t *testing.T) { - minCap := 32 - pool := NewByteSlicePool(minCap) - - bs := pool.Get(minCap) - assert.Equal(t, 0, len(bs)) - assert.Equal(t, minCap, cap(bs)) - - pool.Put(bs) - bs2 := pool.Get(minCap) - assert.Equal(t, &bs, &bs2) - assert.Equal(t, minCap, cap(bs2)) - - for i := 0; i < minCap; i++ { - bs2 = append(bs2, 0) - } - - // Less than minCap - // Capacity will not change after resize - size2 := 16 - bs2 = pool.Resize(bs2, size2) - assert.Equal(t, size2, len(bs2)) - assert.Equal(t, minCap, cap(bs2)) - - // Less than twice the minCap - // Will automatically expand to twice the original capacity - size3 := 48 - bs2 = pool.Resize(bs2, size3) - assert.Equal(t, size3, len(bs2)) - assert.Equal(t, minCap*2, cap(bs2)) - - // More than twice the minCap - // Will automatically expand to the specified size - size4 := 128 - bs2 = pool.Resize(bs2, size4) - assert.Equal(t, size4, len(bs2)) - assert.Equal(t, size4, cap(bs2)) -} diff --git a/utils/http-middlewares.go b/utils/http-middlewares.go index daf5e5a3de8..e3e0ede7ba3 100644 --- a/utils/http-middlewares.go +++ b/utils/http-middlewares.go @@ -17,7 +17,7 @@ import ( "io" "net/http" - "github.com/dapr/dapr/utils/streams" + "github.com/dapr/kit/streams" ) // UppercaseRequestMiddleware is a HTTP middleware that transforms the request body to uppercase diff --git a/utils/streams/limitreadcloser.go b/utils/streams/limitreadcloser.go deleted file mode 100644 index a03573ee8d3..00000000000 --- a/utils/streams/limitreadcloser.go +++ /dev/null @@ -1,81 +0,0 @@ -/* -Copyright 2023 The Dapr Authors -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. -*/ - -package streams - -import ( - "errors" - "io" -) - -/*! -Adapted from the Go 1.18.3 source code -Copyright 2009 The Go Authors. All rights reserved. -License: BSD (https://github.com/golang/go/blob/go1.18.3/LICENSE) -*/ - -// ErrStreamTooLarge is returned by LimitReadCloser when the stream is too large. -var ErrStreamTooLarge = errors.New("stream too large") - -// LimitReadCloser returns a ReadCloser that reads from r but stops with ErrStreamTooLarge after n bytes. -func LimitReadCloser(r io.ReadCloser, n int64) io.ReadCloser { - return &limitReadCloser{ - R: r, - N: n, - } -} - -type limitReadCloser struct { - R io.ReadCloser - N int64 - closed bool -} - -func (l *limitReadCloser) Read(p []byte) (n int, err error) { - if l.N < 0 || l.R == nil { - return 0, ErrStreamTooLarge - } - if len(p) == 0 { - return 0, nil - } - if l.closed { - return 0, io.EOF - } - if int64(len(p)) > (l.N + 1) { - p = p[0:(l.N + 1)] - } - n, err = l.R.Read(p) - l.N -= int64(n) - if l.N < 0 { - // Special case if we just read the "l.N+1" byte - if l.N == -1 { - n-- - } - if err == nil { - err = ErrStreamTooLarge - } - if !l.closed { - l.closed = true - l.R.Close() - } - } - return -} - -func (l *limitReadCloser) Close() error { - if l.closed { - return nil - } - l.closed = true - return l.R.Close() -} diff --git a/utils/streams/limitreadcloser_test.go b/utils/streams/limitreadcloser_test.go deleted file mode 100644 index 92aec110c6a..00000000000 --- a/utils/streams/limitreadcloser_test.go +++ /dev/null @@ -1,124 +0,0 @@ -/* -Copyright 2023 The Dapr Authors -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. -*/ - -package streams - -import ( - "io" - "strings" - "testing" - - "github.com/stretchr/testify/require" -) - -func TestLimitReadCloser(t *testing.T) { - t.Run("stream shorter than limit", func(t *testing.T) { - s := LimitReadCloser(io.NopCloser(strings.NewReader("e ho guardato dentro un'emozione")), 1000) - read, err := io.ReadAll(s) - require.NoError(t, err) - require.Equal(t, "e ho guardato dentro un'emozione", string(read)) - - // Reading again should return io.EOF - n, err := s.Read(read) - require.ErrorIs(t, err, io.EOF) - require.Equal(t, 0, n) - }) - - t.Run("stream has same length as limit", func(t *testing.T) { - s := LimitReadCloser(io.NopCloser(strings.NewReader("e ci ho visto dentro tanto amore")), 32) - read, err := io.ReadAll(s) - require.NoError(t, err) - require.Equal(t, "e ci ho visto dentro tanto amore", string(read)) - - // Reading again should return io.EOF - n, err := s.Read(read) - require.ErrorIs(t, err, io.EOF) - require.Equal(t, 0, n) - }) - - t.Run("stream longer than limit", func(t *testing.T) { - s := LimitReadCloser(io.NopCloser(strings.NewReader("che ho capito perche' non si comanda al cuore")), 21) - read, err := io.ReadAll(s) - require.Error(t, err) - require.ErrorIs(t, err, ErrStreamTooLarge) - require.Equal(t, "che ho capito perche'", string(read)) - - // Reading again should return ErrStreamTooLarge again - n, err := s.Read(read) - require.ErrorIs(t, err, ErrStreamTooLarge) - require.Equal(t, 0, n) - }) - - t.Run("stream longer than limit, read with byte slice", func(t *testing.T) { - s := LimitReadCloser(io.NopCloser(strings.NewReader("e va bene cosi'")), 4) - - read := make([]byte, 100) - n, err := s.Read(read) - require.Error(t, err) - require.ErrorIs(t, err, ErrStreamTooLarge) - require.Equal(t, "e va", string(read[0:n])) - - // Reading again should return ErrStreamTooLarge again - n, err = s.Read(read) - require.ErrorIs(t, err, ErrStreamTooLarge) - require.Equal(t, 0, n) - }) - - t.Run("read in two segments", func(t *testing.T) { - s := LimitReadCloser(io.NopCloser(strings.NewReader("senza parole")), 9) - - read := make([]byte, 5) - - n, err := s.Read(read) - require.NoError(t, err) - require.Equal(t, "senza", string(read[0:n])) - - n, err = s.Read(read) - require.Error(t, err) - require.ErrorIs(t, err, ErrStreamTooLarge) - require.Equal(t, " par", string(read[0:n])) - - // Reading again should return ErrStreamTooLarge again - n, err = s.Read(read) - require.ErrorIs(t, err, ErrStreamTooLarge) - require.Equal(t, 0, n) - }) - - t.Run("close early", func(t *testing.T) { - s := LimitReadCloser(io.NopCloser(strings.NewReader("senza parole")), 10) - - // Read 5 bytes then close - read := make([]byte, 5) - n, err := s.Read(read) - require.NoError(t, err) - require.Equal(t, "senza", string(read[0:n])) - - // Reading should now return io.EOF - err = s.Close() - require.NoError(t, err) - - n, err = s.Read(read) - require.Error(t, err) - require.ErrorIs(t, err, io.EOF) - require.Equal(t, 0, n) - }) - - t.Run("stream is nil", func(t *testing.T) { - s := LimitReadCloser(nil, 10) - - // Reading should return ErrStreamTooLarge again - n, err := s.Read(make([]byte, 10)) - require.ErrorIs(t, err, ErrStreamTooLarge) - require.Equal(t, 0, n) - }) -} diff --git a/utils/streams/multireadercloser.go b/utils/streams/multireadercloser.go deleted file mode 100644 index 46a8bbacdc4..00000000000 --- a/utils/streams/multireadercloser.go +++ /dev/null @@ -1,99 +0,0 @@ -/* -Copyright 2022 The Dapr Authors -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. -*/ - -package streams - -import ( - "errors" - "io" - "net/http" -) - -// NewMultiReaderCloser returns a stream that is like io.MultiReader but that can be closed. -// When the returned stream is closed, it closes the readable streams too, if they implement io.Closer. -func NewMultiReaderCloser(readers ...io.Reader) *MultiReaderCloser { - r := make([]io.Reader, len(readers)) - copy(r, readers) - return &MultiReaderCloser{ - readers: r, - } -} - -/*! -Adapted from the Go 1.19.3 source code -Copyright 2009 The Go Authors. All rights reserved. -License: BSD (https://github.com/golang/go/blob/go1.19.3/LICENSE) -*/ - -// MultiReaderCloser is an io.MultiReader that also implements the io.Closer interface to close the readable streams. -// Readable streams are also closed when we're done reading from them. -type MultiReaderCloser struct { - readers []io.Reader -} - -func (mr *MultiReaderCloser) Read(p []byte) (n int, err error) { - for len(mr.readers) > 0 { - r := mr.readers[0] - n, err = r.Read(p) - - // When reading from a http.Response Body, we may get ErrBodyReadAfterClose if we already read it all - // We consider that the same as io.EOF - if errors.Is(err, http.ErrBodyReadAfterClose) { - err = io.EOF - mr.readers = mr.readers[1:] - } else if err == io.EOF { - if rc, ok := r.(io.Closer); ok { - _ = rc.Close() - } - mr.readers = mr.readers[1:] - } - if n > 0 || err != io.EOF { - if err == io.EOF && len(mr.readers) > 0 { - // Don't return EOF yet. More readers remain. - err = nil - } - return - } - } - return 0, io.EOF -} - -func (mr *MultiReaderCloser) WriteTo(w io.Writer) (sum int64, err error) { - return mr.writeToWithBuffer(w, make([]byte, 1024*32)) -} - -func (mr *MultiReaderCloser) writeToWithBuffer(w io.Writer, buf []byte) (sum int64, err error) { - var n int64 - for i, r := range mr.readers { - n, err = io.CopyBuffer(w, r, buf) - sum += n - if err != nil { - mr.readers = mr.readers[i:] // permit resume / retry after error - return sum, err - } - mr.readers[i] = nil // permit early GC - } - mr.readers = nil - return sum, nil -} - -// Close implements io.Closer. -func (mr *MultiReaderCloser) Close() error { - for _, r := range mr.readers { - if rc, ok := r.(io.Closer); ok { - _ = rc.Close() - } - } - mr.readers = mr.readers[:0] - return nil -} diff --git a/utils/streams/teereadcloser.go b/utils/streams/teereadcloser.go deleted file mode 100644 index ed73b89fbad..00000000000 --- a/utils/streams/teereadcloser.go +++ /dev/null @@ -1,110 +0,0 @@ -/* -Copyright 2022 The Dapr Authors -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. -*/ - -package streams - -import ( - "errors" - "fmt" - "io" - "sync" -) - -/*! -Adapted from the Go 1.18.3 source code -Copyright 2009 The Go Authors. All rights reserved. -License: BSD (https://github.com/golang/go/blob/go1.18.3/LICENSE) -*/ - -// NewTeeReadCloser returns a stream that is like io.TeeReader but that can be closed. -// When the returned stream is closed, it closes the readable stream too, if it implements io.Closer. -func NewTeeReadCloser(r io.Reader, w io.Writer) *TeeReadCloser { - return &TeeReadCloser{ - r: r, - w: w, - } -} - -// TeeReadCloser is an io.TeeReader that also implements the io.Closer interface to close the readable stream. -type TeeReadCloser struct { - r io.Reader - w io.Writer - eof bool - lock sync.Mutex -} - -// Close implements io.Closer. -func (t *TeeReadCloser) Close() error { - t.lock.Lock() - defer t.lock.Unlock() - - var errR, errW error - // r and w do not need to implement the io.Closer interface - if r, ok := t.r.(io.Closer); ok { - errR = r.Close() - } - if w, ok := t.w.(io.Closer); ok { - errW = w.Close() - } - t.r = nil - t.w = nil - - if errR != nil && errW != nil { - return fmt.Errorf("failed to close r stream with error='%v' and failed to close w stream with error='%v'", errR, errW) - } else if errR != nil { - return fmt.Errorf("failed to close r stream with error='%v'", errR) - } else if errW != nil { - return fmt.Errorf("failed to close w stream with error='%v'", errW) - } - return nil -} - -// Stop closes the underlying writer, which will blocks all future read operations with ErrClosedPipe, but doesn't close the reader stream. -// It's meant to be used when the reader needs to be swapped. -func (t *TeeReadCloser) Stop() (err error) { - t.lock.Lock() - defer t.lock.Unlock() - - // w does not need to implement the io.Closer interface - if w, ok := t.w.(io.Closer); ok { - err = w.Close() - } - t.w = nil - - return err -} - -// Read from the R stream and tee it into the w stream. -func (t *TeeReadCloser) Read(p []byte) (n int, err error) { - t.lock.Lock() - defer t.lock.Unlock() - - if t.r == nil || t.w == nil { - return 0, io.ErrClosedPipe - } - if t.eof { - return 0, io.EOF - } - - n, err = t.r.Read(p) - if errors.Is(err, io.EOF) { - t.eof = true - } - if n > 0 { - //nolint:govet - if n, err := t.w.Write(p[:n]); err != nil { - return n, err - } - } - return n, err -} diff --git a/utils/streams/uppercase_transformer.go b/utils/streams/uppercase_transformer.go deleted file mode 100644 index a42a868de8f..00000000000 --- a/utils/streams/uppercase_transformer.go +++ /dev/null @@ -1,52 +0,0 @@ -/* -Copyright 2022 The Dapr Authors -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. -*/ - -package streams - -import ( - "bufio" - "io" - "strings" - "unicode" - - "github.com/tidwall/transform" -) - -// UppercaseTransformer uppercases all characters in the stream, by calling strings.ToUpper() on them. -func UppercaseTransformer(r io.Reader) io.Reader { - br := bufio.NewReader(r) - return transform.NewTransformer(func() ([]byte, error) { - c, _, err := br.ReadRune() - if err != nil { - return nil, err - } - - return RuneToUppercase(c), nil - }) -} - -// RuneToUppercase converts a rune into a byte slice where all lowercase letters (Unicode-aware) are converted to uppercase ones. -func RuneToUppercase(c rune) []byte { - // Optimize for ASCII characters - if c < 128 { - b := byte(c) - if 'a' <= b && b <= 'z' { - return []byte{b - 0x20} - } else { - return []byte{b} - } - } - - // Unicode - return []byte(strings.Map(unicode.ToUpper, string([]rune{c}))) -} diff --git a/utils/streams/uppercase_transformer_test.go b/utils/streams/uppercase_transformer_test.go deleted file mode 100644 index 05cd1f7bd6a..00000000000 --- a/utils/streams/uppercase_transformer_test.go +++ /dev/null @@ -1,67 +0,0 @@ -/* -Copyright 2022 The Dapr Authors -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. -*/ - -package streams - -import ( - "io" - "strings" - "testing" -) - -func TestUppercaseTransformer(t *testing.T) { - tests := []struct { - name string - in string - out string - }{ - { - name: "empty string", - in: "", - out: "", - }, - { - name: "hello world", - in: "hello world", - out: "HELLO WORLD", - }, - { - name: "UPPERCASE", - in: "UPPERCASE", - out: "UPPERCASE", - }, - { - name: "Dante's verses (mixed case + unicode)", - in: "Nel mezzo del cammin di nostra vita mi ritrovai per una selva oscura, chΓ© la diritta via era smarrita.", - out: "NEL MEZZO DEL CAMMIN DI NOSTRA VITA MI RITROVAI PER UNA SELVA OSCURA, CHΓ‰ LA DIRITTA VIA ERA SMARRITA.", - }, - { - name: "emojis", - in: "πŸ˜€ciaoπŸ‘©β€πŸ‘©β€πŸ‘¦β€πŸ‘¦", // πŸ˜€ = single codepoint; πŸ‘©β€πŸ‘©β€πŸ‘¦β€πŸ‘¦ = 7 (!!) codepoints - out: "πŸ˜€CIAOπŸ‘©β€πŸ‘©β€πŸ‘¦β€πŸ‘¦", - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - tr := UppercaseTransformer(strings.NewReader(tt.in)) - gotOut, err := io.ReadAll(tr) - if err != nil { - t.Errorf("read stream error = %v", err) - return - } - if string(gotOut) != tt.out { - t.Errorf("gotOut = %v, want %v", string(gotOut), tt.out) - } - }) - } -} diff --git a/utils/utils.go b/utils/utils.go index 34c7ff5b79f..bd22f5ae939 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -17,7 +17,6 @@ import ( "fmt" "io/fs" "os" - "path/filepath" "strings" ) @@ -65,31 +64,6 @@ func GetEnvOrElse(name, orElse string) string { return orElse } -// IsTruthy returns true if a string is a truthy value. -// Truthy values are "y", "yes", "true", "t", "on", "1" (case-insensitive); everything else is false. -func IsTruthy(val string) bool { - val = strings.TrimSpace(val) - if len(val) > 4 { - // Short-circuit, this can never be a truthy value - return false - } - switch strings.ToLower(val) { - case "y", "yes", "true", "t", "on", "1": - return true - default: - return false - } -} - -// IsYaml checks whether the file is yaml or not. -func IsYaml(fileName string) bool { - extension := strings.ToLower(filepath.Ext(fileName)) - if extension == ".yaml" || extension == ".yml" { - return true - } - return false -} - // GetIntValOrDefault returns an int value if greater than 0 OR default value. func GetIntValOrDefault(val int, defaultValue int) int { if val > 0 { diff --git a/utils/utils_test.go b/utils/utils_test.go index e3fe75eec80..0e8530cfd36 100644 --- a/utils/utils_test.go +++ b/utils/utils_test.go @@ -65,27 +65,6 @@ func TestSetEnvVariables(t *testing.T) { }) } -func TestIsYaml(t *testing.T) { - testCases := []struct { - input string - expected bool - }{ - { - input: "a.yaml", - expected: true, - }, { - input: "a.yml", - expected: true, - }, { - input: "a.txt", - expected: false, - }, - } - for _, tc := range testCases { - assert.Equal(t, IsYaml(tc.input), tc.expected) - } -} - func TestGetIntValFromStringVal(t *testing.T) { tcs := []struct { name string @@ -284,14 +263,6 @@ func TestGetNamespaceOrDefault(t *testing.T) { }) } -func BenchmarkIsTruthy(b *testing.B) { - for n := 0; n < b.N; n++ { - IsTruthy("true") - IsTruthy("false") - IsTruthy(" on ") - } -} - func BenchmarkFilter(b *testing.B) { vals := make([]int, 100) for i := 0; i < len(vals); i++ {