Package Encoding/RandReader:

- optimize code
- remplace some pointer by atomic to prevent RACE

Other:
- bump dependencies
This commit is contained in:
Nicolas JUHEL
2024-05-29 16:34:40 +02:00
parent 1ed0b00db2
commit dbeb385267
6 changed files with 195 additions and 133 deletions

View File

@@ -27,6 +27,7 @@ package user
import (
"fmt"
sdkaws "github.com/aws/aws-sdk-go-v2/aws"
sdkiam "github.com/aws/aws-sdk-go-v2/service/iam"
"github.com/aws/aws-sdk-go-v2/service/iam/types"

View File

@@ -27,7 +27,6 @@
package randRead
import (
"bufio"
"io"
"sync/atomic"
)
@@ -49,11 +48,12 @@ func New(fct FuncRemote) io.ReadCloser {
f.Store(fct)
r := &remote{
r: new(atomic.Value),
f: f,
}
return &prnd{
b: bufio.NewReader(r),
b: new(atomic.Value),
r: r,
}
}

View File

@@ -28,94 +28,54 @@ package randRead
import (
"bufio"
"errors"
"bytes"
"fmt"
"io"
"sync/atomic"
)
type remote struct {
f *atomic.Value
r io.ReadCloser
}
func (o *remote) readReader(p []byte) (n int, err error) {
if o.r == nil {
return 0, io.EOF
}
if n, err = o.r.Read(p); err != nil {
return n, err
} else {
return n, nil
}
}
func (o *remote) readRemote() error {
if o.f == nil {
return fmt.Errorf("invalid reader")
} else if i := o.f.Load(); i == nil {
return fmt.Errorf("invalid reader")
} else if f, ok := i.(FuncRemote); !ok {
return fmt.Errorf("invalid reader")
} else if r, err := f(); err != nil {
return err
} else {
if o.r != nil {
_ = o.r.Close()
}
o.r = r
return nil
}
}
func (o *remote) Read(p []byte) (n int, err error) {
if n, err = o.readReader(p); err != nil && !errors.Is(err, io.EOF) {
return n, err
}
if err = o.readRemote(); err != nil {
return 0, err
}
return o.readReader(p)
}
func (o *remote) Close() error {
if o.r != nil {
_ = o.r.Close()
}
return nil
}
type prnd struct {
b *bufio.Reader
b *atomic.Value // *bufio.Reader
r *remote
}
func (o *prnd) Read(p []byte) (n int, err error) {
if o.b != nil {
return o.b.Read(p)
func (o *prnd) buf() (*bufio.Reader, error) {
if i := o.b.Load(); i != nil {
if b, k := i.(*bufio.Reader); k {
return b, nil
}
}
if o.r != nil {
o.b = bufio.NewReader(o.r)
return o.b.Read(p)
if o.r == nil {
return nil, fmt.Errorf("invalid reader")
}
b := bufio.NewReader(o.r)
o.b.Store(b)
return b, nil
}
func (o *prnd) reset() {
l := o.b.Swap(bufio.NewReader(bytes.NewReader(make([]byte, 0))))
if b, k := l.(*bufio.Reader); k {
b.Reset(bytes.NewReader(make([]byte, 0)))
}
}
func (o *prnd) Read(p []byte) (n int, err error) {
if b, e := o.buf(); e != nil {
return 0, e
} else {
return 0, fmt.Errorf("invalid reader")
return b.Read(p)
}
}
func (o *prnd) Close() error {
if o.b != nil {
o.b.Reset(nil)
o.reset()
if o.r == nil {
return fmt.Errorf("invalid reader")
}
if o.r != nil {
_ = o.r.Close()
}
return nil
return o.r.Close()
}

View File

@@ -71,5 +71,9 @@ var _ = Describe("encoding/randRead", func() {
Expect(nbr).ToNot(BeNumerically("==", 0))
})
}
It("must succeed when closing the random reader", func() {
Expect(rnd.Close()).ToNot(HaveOccurred())
})
})
})

View File

@@ -0,0 +1,96 @@
/*
* MIT License
*
* Copyright (c) 2024 Nicolas JUHEL
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*
*/
package randRead
import (
"fmt"
"io"
"sync/atomic"
)
type remote struct {
f *atomic.Value // FuncRemote
r *atomic.Value // io.ReadCloser
}
func (o *remote) reader(p []byte) (n int, err error) {
if i := o.r.Load(); i == nil {
return 0, io.EOF
} else if r, k := i.(io.ReadCloser); !k {
return 0, io.EOF
} else {
return r.Read(p)
}
}
func (o *remote) readRemote() error {
if o.f == nil {
return fmt.Errorf("invalid reader")
} else if i := o.f.Load(); i == nil {
return fmt.Errorf("invalid reader")
} else if f, ok := i.(FuncRemote); !ok {
return fmt.Errorf("invalid reader")
} else if r, err := f(); err != nil {
return err
} else {
l := o.r.Swap(r)
if v, k := l.(io.Closer); k && v != nil {
_ = v.Close()
}
return nil
}
}
func (o *remote) Read(p []byte) (n int, err error) {
n, err = o.reader(p)
if n > 0 {
return n, nil
}
if err = o.readRemote(); err != nil {
return 0, err
}
n, err = o.reader(p)
if n > 0 {
return n, nil
}
return 0, err
}
func (o *remote) Close() error {
if i := o.r.Load(); i == nil {
return nil
} else if r, k := i.(io.ReadCloser); !k {
return nil
} else {
return r.Close()
}
}

117
go.mod
View File

@@ -5,42 +5,42 @@ go 1.22
toolchain go1.22.2
require (
github.com/aws/aws-sdk-go v1.51.21
github.com/aws/aws-sdk-go-v2 v1.26.1
github.com/aws/aws-sdk-go-v2/config v1.27.11
github.com/aws/aws-sdk-go-v2/credentials v1.17.11
github.com/aws/aws-sdk-go-v2/service/iam v1.32.0
github.com/aws/aws-sdk-go-v2/service/s3 v1.53.1
github.com/aws/aws-sdk-go v1.53.11
github.com/aws/aws-sdk-go-v2 v1.27.0
github.com/aws/aws-sdk-go-v2/config v1.27.16
github.com/aws/aws-sdk-go-v2/credentials v1.17.16
github.com/aws/aws-sdk-go-v2/service/iam v1.32.4
github.com/aws/aws-sdk-go-v2/service/s3 v1.54.3
github.com/aws/smithy-go v1.20.2
github.com/bits-and-blooms/bitset v1.13.0
github.com/c-bata/go-prompt v0.2.6
github.com/dsnet/compress v0.0.1
github.com/fatih/color v1.16.0
github.com/fatih/color v1.17.0
github.com/fsnotify/fsnotify v1.7.0
github.com/fxamacker/cbor/v2 v2.6.0
github.com/gin-gonic/gin v1.9.1
github.com/go-ldap/ldap/v3 v3.4.7
github.com/go-playground/validator/v10 v10.19.0
github.com/gin-gonic/gin v1.10.0
github.com/go-ldap/ldap/v3 v3.4.8
github.com/go-playground/validator/v10 v10.20.0
github.com/google/go-github/v33 v33.0.0
github.com/hashicorp/go-hclog v1.6.3
github.com/hashicorp/go-retryablehttp v0.7.5
github.com/hashicorp/go-retryablehttp v0.7.6
github.com/hashicorp/go-uuid v1.0.3
github.com/hashicorp/go-version v1.6.0
github.com/hashicorp/go-version v1.7.0
github.com/jlaffaye/ftp v0.2.0
github.com/lni/dragonboat/v3 v3.3.8
github.com/matcornic/hermes/v2 v2.1.0
github.com/mattn/go-colorable v0.1.13
github.com/mitchellh/go-homedir v1.1.0
github.com/mitchellh/mapstructure v1.5.0
github.com/nats-io/jwt/v2 v2.5.5
github.com/nats-io/nats-server/v2 v2.10.14
github.com/nats-io/nats.go v1.34.1
github.com/nats-io/jwt/v2 v2.5.7
github.com/nats-io/nats-server/v2 v2.10.16
github.com/nats-io/nats.go v1.35.0
github.com/nutsdb/nutsdb v0.14.3
github.com/onsi/ginkgo/v2 v2.17.1
github.com/onsi/gomega v1.32.0
github.com/onsi/ginkgo/v2 v2.19.0
github.com/onsi/gomega v1.33.1
github.com/pelletier/go-toml v1.9.5
github.com/pierrec/lz4/v4 v4.1.21
github.com/prometheus/client_golang v1.19.0
github.com/prometheus/client_golang v1.19.1
github.com/shirou/gopsutil v3.21.11+incompatible
github.com/sirupsen/logrus v1.9.3
github.com/spf13/cobra v1.8.0
@@ -49,77 +49,78 @@ require (
github.com/ugorji/go/codec v1.2.12
github.com/ulikunitz/xz v0.5.12
github.com/vbauerster/mpb/v8 v8.7.3
github.com/xanzy/go-gitlab v0.102.0
github.com/xanzy/go-gitlab v0.105.0
github.com/xhit/go-simple-mail v2.2.2+incompatible
github.com/xujiajun/utils v0.0.0-20220904132955-5f7c5b914235
golang.org/x/exp v0.0.0-20240409090435-93d18d7e34b8
golang.org/x/net v0.24.0
golang.org/x/oauth2 v0.19.0
golang.org/x/exp v0.0.0-20240529005216-23cca8864a10
golang.org/x/net v0.25.0
golang.org/x/oauth2 v0.20.0
golang.org/x/sync v0.7.0
golang.org/x/sys v0.19.0
golang.org/x/term v0.19.0
golang.org/x/sys v0.20.0
golang.org/x/term v0.20.0
gopkg.in/yaml.v3 v3.0.1
gorm.io/driver/clickhouse v0.6.0
gorm.io/driver/mysql v1.5.6
gorm.io/driver/postgres v1.5.7
gorm.io/driver/sqlite v1.5.5
gorm.io/driver/sqlserver v1.5.3
gorm.io/gorm v1.25.9
gorm.io/gorm v1.25.10
)
require (
filippo.io/edwards25519 v1.1.0 // indirect
github.com/Azure/go-ntlmssp v0.0.0-20221128193559-754e69321358 // indirect
github.com/ClickHouse/ch-go v0.61.5 // indirect
github.com/ClickHouse/clickhouse-go/v2 v2.23.0 // indirect
github.com/ClickHouse/clickhouse-go/v2 v2.25.0 // indirect
github.com/Masterminds/goutils v1.1.1 // indirect
github.com/Masterminds/semver v1.5.0 // indirect
github.com/Masterminds/sprig v2.22.0+incompatible // indirect
github.com/PuerkitoBio/goquery v1.9.1 // indirect
github.com/PuerkitoBio/goquery v1.9.2 // indirect
github.com/VictoriaMetrics/metrics v1.6.2 // indirect
github.com/VividCortex/ewma v1.2.0 // indirect
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d // indirect
github.com/andybalholm/brotli v1.1.0 // indirect
github.com/andybalholm/cascadia v1.3.2 // indirect
github.com/antlabs/stl v0.0.2 // indirect
github.com/antlabs/timer v0.1.3 // indirect
github.com/antlabs/timer v0.1.4 // indirect
github.com/armon/go-metrics v0.4.1 // indirect
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.2 // indirect
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.1 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.5 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.5 // indirect
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.3 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.7 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.7 // indirect
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.0 // indirect
github.com/aws/aws-sdk-go-v2/internal/v4a v1.3.5 // indirect
github.com/aws/aws-sdk-go-v2/internal/v4a v1.3.7 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.11.2 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.3.7 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.11.7 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.17.5 // indirect
github.com/aws/aws-sdk-go-v2/service/sso v1.20.5 // indirect
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.23.4 // indirect
github.com/aws/aws-sdk-go-v2/service/sts v1.28.6 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.3.9 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.11.9 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.17.7 // indirect
github.com/aws/aws-sdk-go-v2/service/sso v1.20.9 // indirect
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.24.3 // indirect
github.com/aws/aws-sdk-go-v2/service/sts v1.28.10 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bwmarrin/snowflake v0.3.0 // indirect
github.com/bytedance/sonic v1.11.4 // indirect
github.com/bytedance/sonic v1.11.7 // indirect
github.com/bytedance/sonic/loader v0.1.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/cloudwego/base64x v0.1.0 // indirect
github.com/cloudwego/iasm v0.0.9 // indirect
github.com/cloudwego/base64x v0.1.4 // indirect
github.com/cloudwego/iasm v0.2.0 // indirect
github.com/cockroachdb/errors v1.7.5 // indirect
github.com/cockroachdb/logtags v0.0.0-20190617123548-eb05cc24525f // indirect
github.com/cockroachdb/pebble v0.0.0-20210331181633-27fc006b8bfb // indirect
github.com/cockroachdb/redact v1.0.6 // indirect
github.com/cockroachdb/sentry-go v0.6.1-cockroachdb.2 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/gabriel-vasile/mimetype v1.4.4 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-asn1-ber/asn1-ber v1.5.5 // indirect
github.com/go-asn1-ber/asn1-ber v1.5.7 // indirect
github.com/go-faster/city v1.0.1 // indirect
github.com/go-faster/errors v0.7.1 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-sql-driver/mysql v1.8.1 // indirect
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/go-task/slim-sprig/v3 v3.0.0 // indirect
github.com/goccy/go-json v0.10.3 // indirect
github.com/gofrs/flock v0.8.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9 // indirect
@@ -129,7 +130,7 @@ require (
github.com/google/btree v1.0.1 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/google/pprof v0.0.0-20240409012703-83162a5b38cd // indirect
github.com/google/pprof v0.0.0-20240528025155-186aa0362fba // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/gorilla/css v1.0.1 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
@@ -146,7 +147,7 @@ require (
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20231201235250-de7065d80cb9 // indirect
github.com/jackc/pgx/v5 v5.5.5 // indirect
github.com/jackc/pgx/v5 v5.6.0 // indirect
github.com/jackc/puddle/v2 v2.2.1 // indirect
github.com/jaytaylor/html2text v0.0.0-20230321000545-74c2419ad056 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
@@ -165,7 +166,7 @@ require (
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/mattn/go-sqlite3 v1.14.22 // indirect
github.com/mattn/go-tty v0.0.5 // indirect
github.com/microsoft/go-mssqldb v1.7.0 // indirect
github.com/microsoft/go-mssqldb v1.7.1 // indirect
github.com/miekg/dns v1.1.43 // indirect
github.com/minio/highwayhash v1.0.2 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
@@ -176,7 +177,7 @@ require (
github.com/nats-io/nuid v1.0.1 // indirect
github.com/olekukonko/tablewriter v0.0.5 // indirect
github.com/paulmach/orb v0.11.1 // indirect
github.com/pelletier/go-toml/v2 v2.2.1 // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pkg/term v1.2.0-beta.2 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
@@ -201,18 +202,18 @@ require (
github.com/valyala/fastrand v1.0.0 // indirect
github.com/valyala/histogram v1.0.1 // indirect
github.com/vanng822/css v1.0.1 // indirect
github.com/vanng822/go-premailer v1.20.2 // indirect
github.com/vanng822/go-premailer v1.21.0 // indirect
github.com/x448/float16 v0.8.4 // indirect
github.com/xujiajun/mmap-go v1.0.1 // indirect
github.com/yusufpapurcu/wmi v1.2.3 // indirect
go.opentelemetry.io/otel v1.25.0 // indirect
go.opentelemetry.io/otel/trace v1.25.0 // indirect
go.opentelemetry.io/otel v1.27.0 // indirect
go.opentelemetry.io/otel/trace v1.27.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/arch v0.7.0 // indirect
golang.org/x/crypto v0.22.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/arch v0.8.0 // indirect
golang.org/x/crypto v0.23.0 // indirect
golang.org/x/text v0.15.0 // indirect
golang.org/x/time v0.5.0 // indirect
golang.org/x/tools v0.20.0 // indirect
google.golang.org/protobuf v1.33.0 // indirect
golang.org/x/tools v0.21.0 // indirect
google.golang.org/protobuf v1.34.1 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
)