Skip to content

Commit

Permalink
added stringsutil package
Browse files Browse the repository at this point in the history
Signed-off-by: Miguel Ángel Ortuño <[email protected]>
  • Loading branch information
ortuman committed Dec 13, 2022
1 parent b81688e commit b080bbe
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 64 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
* [ENHANCEMENT] Replace go-kit/kit/log with go-kit/log. #52
* [ENHANCEMENT] Add spanlogger package. #42
* [ENHANCEMENT] Add runutil.CloseWithLogOnErr function. #58
* [ENHANCEMENT] Add cache and gate packages. #239
* [ENHANCEMENT] Add cache, gate and stringsutil packages. #239
* [ENHANCEMENT] Optimise memberlist receive path when used as a backing store for rings with a large number of members. #76 #77 #84 #91 #93
* [ENHANCEMENT] Memberlist: prepare the data to send on the write before starting counting the elapsed time for `-memberlist.packet-write-timeout`, in order to reduce chances we hit the timeout when sending a packet to other node. #89
* [ENHANCEMENT] Memberlist: parallelize processing of messages received by memberlist. #110
Expand Down
13 changes: 3 additions & 10 deletions cache/compression.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/golang/snappy"

"github.com/grafana/dskit/util/stringsutil"
)

const (
Expand All @@ -33,7 +35,7 @@ func (cfg *CompressionConfig) RegisterFlagsWithPrefix(f *flag.FlagSet, prefix st
}

func (cfg *CompressionConfig) Validate() error {
if cfg.Compression != "" && !strSliceContains(supportedCompressions, cfg.Compression) {
if cfg.Compression != "" && !stringsutil.SliceContains(supportedCompressions, cfg.Compression) {
return errUnsupportedCompression
}
return nil
Expand Down Expand Up @@ -93,12 +95,3 @@ func (s *snappyCache) Fetch(ctx context.Context, keys []string) map[string][]byt
func (s *snappyCache) Name() string {
return s.next.Name()
}

func strSliceContains(slice []string, s string) bool {
for _, v := range slice {
if v == s {
return true
}
}
return false
}
32 changes: 0 additions & 32 deletions cache/memcached_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"gopkg.in/yaml.v2"

"github.com/grafana/dskit/dns"
"github.com/grafana/dskit/flagext"
Expand All @@ -40,17 +39,6 @@ var (
errMemcachedConfigNoAddrs = errors.New("no memcached addresses provided")
errMemcachedDNSUpdateIntervalNotPositive = errors.New("DNS provider update interval must be positive")
errMemcachedMaxAsyncConcurrencyNotPositive = errors.New("max async concurrency must be positive")

defaultMemcachedClientConfig = MemcachedClientConfig{
Timeout: 500 * time.Millisecond,
MaxIdleConnections: 100,
MaxAsyncConcurrency: 20,
MaxAsyncBufferSize: 10000,
MaxItemSize: flagext.Bytes(1024 * 1024),
MaxGetMultiConcurrency: 100,
MaxGetMultiBatchSize: 0,
DNSProviderUpdateInterval: 10 * time.Second,
}
)

var (
Expand Down Expand Up @@ -162,16 +150,6 @@ func (c *MemcachedClientConfig) validate() error {
return nil
}

// parseMemcachedClientConfig unmarshals a buffer into a MemcachedClientConfig with default values.
func parseMemcachedClientConfig(conf []byte) (MemcachedClientConfig, error) {
config := defaultMemcachedClientConfig
if err := yaml.Unmarshal(conf, &config); err != nil {
return MemcachedClientConfig{}, err
}

return config, nil
}

type memcachedClient struct {
logger log.Logger
config MemcachedClientConfig
Expand Down Expand Up @@ -219,16 +197,6 @@ type memcachedGetMultiResult struct {
err error
}

// NewMemcachedClient makes a new RemoteCacheClient.
func NewMemcachedClient(logger log.Logger, name string, conf []byte, reg prometheus.Registerer) (RemoteCacheClient, error) {
config, err := parseMemcachedClientConfig(conf)
if err != nil {
return nil, err
}

return NewMemcachedClientWithConfig(logger, name, config, reg)
}

// NewMemcachedClientWithConfig makes a new RemoteCacheClient.
func NewMemcachedClientWithConfig(logger log.Logger, name string, config MemcachedClientConfig, reg prometheus.Registerer) (RemoteCacheClient, error) {
if err := config.validate(); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions ring/client/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (
"google.golang.org/grpc/health/grpc_health_v1"

"github.com/grafana/dskit/concurrency"
"github.com/grafana/dskit/ring/util"
"github.com/grafana/dskit/services"
"github.com/grafana/dskit/util/stringsutil"
)

// PoolClient is the interface that should be implemented by a
Expand Down Expand Up @@ -171,7 +171,7 @@ func (p *Pool) removeStaleClients() {
}

for _, addr := range p.RegisteredAddresses() {
if util.StringsContain(serviceAddrs, addr) {
if stringsutil.SliceContains(serviceAddrs, addr) {
continue
}
level.Info(p.logger).Log("msg", "removing stale client", "addr", addr)
Expand Down
8 changes: 4 additions & 4 deletions ring/ring.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ import (

"github.com/grafana/dskit/kv"
shardUtil "github.com/grafana/dskit/ring/shard"
"github.com/grafana/dskit/ring/util"
"github.com/grafana/dskit/services"

"github.com/grafana/dskit/flagext"
dsmath "github.com/grafana/dskit/internal/math"
"github.com/grafana/dskit/util/stringsutil"
)

const (
Expand Down Expand Up @@ -291,7 +291,7 @@ func (r *Ring) updateRingState(ringDesc *Desc) {
// Filter out all instances belonging to excluded zones.
if len(r.cfg.ExcludedZones) > 0 {
for instanceID, instance := range ringDesc.Ingesters {
if util.StringsContain(r.cfg.ExcludedZones, instance.Zone) {
if stringsutil.SliceContains(r.cfg.ExcludedZones, instance.Zone) {
delete(ringDesc.Ingesters, instanceID)
}
}
Expand Down Expand Up @@ -364,13 +364,13 @@ func (r *Ring) Get(key uint32, op Operation, bufDescs []InstanceDesc, bufHosts,
}

// We want n *distinct* instances && distinct zones.
if util.StringsContain(distinctHosts, info.InstanceID) {
if stringsutil.SliceContains(distinctHosts, info.InstanceID) {
continue
}

// Ignore if the instances don't have a zone set.
if r.cfg.ZoneAwarenessEnabled && info.Zone != "" {
if util.StringsContain(distinctZones, info.Zone) {
if stringsutil.SliceContains(distinctZones, info.Zone) {
continue
}
}
Expand Down
6 changes: 3 additions & 3 deletions ring/ring_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ import (
"github.com/grafana/dskit/kv"
"github.com/grafana/dskit/kv/consul"
"github.com/grafana/dskit/ring/shard"
"github.com/grafana/dskit/ring/util"
"github.com/grafana/dskit/services"
"github.com/grafana/dskit/test"
"github.com/grafana/dskit/util/stringsutil"
)

const (
Expand Down Expand Up @@ -1280,7 +1280,7 @@ func TestRing_ShuffleShard_Shuffling(t *testing.T) {

numMatching := 0
for _, c := range currShard {
if util.StringsContain(otherShard, c) {
if stringsutil.SliceContains(otherShard, c) {
numMatching++
}
}
Expand Down Expand Up @@ -2218,7 +2218,7 @@ func TestRingUpdates(t *testing.T) {

// Ensure there's no instance in an excluded zone.
if len(testData.excludedZones) > 0 {
assert.False(t, util.StringsContain(testData.excludedZones, ing.Zone))
assert.False(t, stringsutil.SliceContains(testData.excludedZones, ing.Zone))
}
}

Expand Down
12 changes: 0 additions & 12 deletions ring/util/string_utils.go

This file was deleted.

11 changes: 11 additions & 0 deletions util/stringsutil/stringsutil.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package stringsutil

// SliceContains returns true if the search value is within the list of input values.
func SliceContains(values []string, search string) bool {
for _, v := range values {
if search == v {
return true
}
}
return false
}

0 comments on commit b080bbe

Please sign in to comment.