Skip to content
This repository has been archived by the owner on Oct 17, 2024. It is now read-only.

Add ability to configure etcd store default limit #126

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions cmd/regula/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ import (
// Config holds the server configuration.
type Config struct {
Etcd struct {
Endpoints []string `config:"etcd-endpoints"`
Namespace string `config:"etcd-namespace"`
Endpoints []string `config:"etcd-endpoints"`
DefaultLimit int `config:"etcd-default-limit"`
Namespace string `config:"etcd-namespace"`
}
Server struct {
Address string `config:"addr"`
Expand All @@ -40,6 +41,7 @@ func LoadConfig(args []string) (*Config, error) {
var cfg Config
flag := stdflag.NewFlagSet("", stdflag.ContinueOnError)
flag.StringVar(&cfg.Etcd.Namespace, "etcd-namespace", "", "etcd namespace to use")
flag.IntVar(&cfg.Etcd.DefaultLimit, "etcd-default-limit", 50, "etcd default limit for Get requests")
flag.StringVar(&cfg.LogLevel, "log-level", zerolog.DebugLevel.String(), "debug level")
cfg.Etcd.Endpoints = []string{"127.0.0.1:2379"}
flag.Var(commaSeparatedFlag{&cfg.Etcd.Endpoints}, "etcd-endpoints", "comma separated etcd endpoints")
Expand Down
7 changes: 4 additions & 3 deletions cmd/regula/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ func main() {
defer etcdCli.Close()

service := etcd.RulesetService{
Client: etcdCli,
Namespace: cfg.Etcd.Namespace,
Logger: logger.With().Str("service", "etcd").Logger(),
Client: etcdCli,
DefaultLimit: cfg.Etcd.DefaultLimit,
Namespace: cfg.Etcd.Namespace,
Logger: logger.With().Str("service", "etcd").Logger(),
}

srv := server.New(&service, server.Config{
Expand Down
13 changes: 8 additions & 5 deletions store/etcd/rulesets.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@ import (
"github.com/segmentio/ksuid"
)

const maxLimit = 100

// RulesetService manages the rulesets using etcd.
type RulesetService struct {
Client *clientv3.Client
Logger zerolog.Logger
Namespace string
Client *clientv3.Client
DefaultLimit int
Logger zerolog.Logger
Namespace string
}

// List returns all the rulesets entries under the given prefix.
Expand All @@ -34,8 +37,8 @@ func (s *RulesetService) List(ctx context.Context, prefix string, limit int, con

var key string

if limit < 0 || limit > 100 {
limit = 50 // TODO(asdine): make this configurable in future releases.
if limit < 0 || limit > maxLimit {
limit = s.DefaultLimit
}

if continueToken != "" {
Expand Down
18 changes: 13 additions & 5 deletions store/etcd/rulesets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ var (
)

var (
dialTimeout = 5 * time.Second
endpoints = []string{"localhost:2379", "etcd:2379"}
dialTimeout = 5 * time.Second
endpoints = []string{"localhost:2379", "etcd:2379"}
defaultLimit = 50
)

func Init() {
func init() {
rand.Seed(time.Now().Unix())
}

Expand All @@ -42,8 +43,9 @@ func newEtcdRulesetService(t *testing.T) (*etcd.RulesetService, func()) {
require.NoError(t, err)

s := etcd.RulesetService{
Client: cli,
Namespace: fmt.Sprintf("regula-store-tests-%d/", rand.Int()),
Client: cli,
DefaultLimit: defaultLimit,
Namespace: fmt.Sprintf("regula-store-tests-%d/", rand.Int()),
}

return &s, func() {
Expand Down Expand Up @@ -150,6 +152,12 @@ func TestList(t *testing.T) {
entries, err = s.List(context.Background(), "y", -10, "")
require.NoError(t, err)
require.Len(t, entries.Entries, 5)

// change default limit and check whether it's applied
s.DefaultLimit = 3
entries, err = s.List(context.Background(), "y", -10, "")
require.NoError(t, err)
require.Len(t, entries.Entries, 3)
})
}

Expand Down