Skip to content

Commit

Permalink
as: Add default pagination limit
Browse files Browse the repository at this point in the history
  • Loading branch information
halimi committed Dec 3, 2024
1 parent eda0a45 commit dec23f1
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pkg/applicationserver/applicationserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import (
"go.thethings.network/lorawan-stack/v3/pkg/messageprocessors/cayennelpp"
"go.thethings.network/lorawan-stack/v3/pkg/messageprocessors/devicerepository"
"go.thethings.network/lorawan-stack/v3/pkg/messageprocessors/javascript"
"go.thethings.network/lorawan-stack/v3/pkg/redis"
"go.thethings.network/lorawan-stack/v3/pkg/rpcmiddleware/hooks"
"go.thethings.network/lorawan-stack/v3/pkg/rpcmiddleware/rpclog"
"go.thethings.network/lorawan-stack/v3/pkg/rpcmiddleware/rpctracer"
Expand Down Expand Up @@ -155,6 +156,10 @@ func New(c *component.Component, conf *Config) (as *ApplicationServer, err error
return nil, err
}

redis.SetPaginationDefaults(redis.PaginationDefaults{
DefaultLimit: conf.Pagination.DefaultLimit,
})

as = &ApplicationServer{
Component: c,
ctx: ctx,
Expand Down
6 changes: 6 additions & 0 deletions pkg/applicationserver/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ type DownlinksConfig struct {
ConfirmationConfig ConfirmationConfig `name:"confirmation" description:"Configuration for confirmed downlink"`
}

// PaginationConfig represents the configuration for pagination.
type PaginationConfig struct {
DefaultLimit int64 `name:"default-limit" description:"Default limit for pagination"`
}

// Config represents the ApplicationServer configuration.
type Config struct {
LinkMode string `name:"link-mode" description:"Deprecated - mode to link applications to their Network Server (all, explicit)"`
Expand All @@ -123,6 +128,7 @@ type Config struct {
DeviceKEKLabel string `name:"device-kek-label" description:"Label of KEK used to encrypt device keys at rest"`
DeviceLastSeen LastSeenConfig `name:"device-last-seen" description:"End Device last seen batch update configuration"`
Downlinks DownlinksConfig `name:"downlinks" description:"Downlink configuration"`
Pagination PaginationConfig `name:"pagination" description:"Pagination configuration"`
}

func (c Config) toProto() *ttnpb.AsConfiguration {
Expand Down
13 changes: 13 additions & 0 deletions pkg/redis/pagination.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ import (
"context"
)

// PaginationDefaults sets default values for paginations options within the Redis store.
type PaginationDefaults struct {
DefaultLimit int64
}

var paginationDefaults = PaginationDefaults{}

// SetPaginationDefaults should only be called at the initialization of the server.
func SetPaginationDefaults(d PaginationDefaults) { paginationDefaults = d }

type paginationOptionsKeyType struct{}

var paginationOptionsKey paginationOptionsKeyType
Expand All @@ -33,6 +43,9 @@ func NewContextWithPagination(ctx context.Context, limit, page int64, total *int
if page == 0 {
page = 1
}
if limit == 0 {
limit = paginationDefaults.DefaultLimit
}
return context.WithValue(ctx, paginationOptionsKey, paginationOptions{
limit: limit,
offset: (page - 1) * limit,
Expand Down
12 changes: 12 additions & 0 deletions pkg/redis/pagination_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,16 @@ func TestPagination(t *testing.T) {
redis.SetPaginationTotal(ctx, total)
a.So(totalCount, should.Equal, total)
})

t.Run("SetPaginationDefaults", func(t *testing.T) {
t.Parallel()
redis.SetPaginationDefaults(redis.PaginationDefaults{DefaultLimit: 20})

ctx := test.Context()
ctx = redis.NewContextWithPagination(ctx, 0, 0, nil)

limit, _ := redis.PaginationLimitAndOffsetFromContext(ctx)

a.So(limit, should.Equal, uint64(20))
})
}

0 comments on commit dec23f1

Please sign in to comment.