Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Metadata TryGetTTL: adds time.ParseDuration support #3122

Merged
merged 9 commits into from
Jan 16, 2024
12 changes: 6 additions & 6 deletions bindings/azure/storagequeues/metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ metadata:
Set the interval to poll Azure Storage Queues for new messages
example: '"30s"'
default: '"10s"'
- name: "ttlInSeconds"
type: number
- name: "ttl"
type: duration
description: |
Set the default message Time To Live (TTL), in seconds.
Set the default message Time To Live (TTL).
If empty, messages expire after 10 minutes.
It's also possible to specify a per-message TTL by setting the `ttlInSeconds` property in the invocation request's metadata.
example: '30'
default: '600'
It's also possible to specify a per-message TTL by setting the `ttl` property in the invocation request's metadata.
example: '30s'
default: '10m'
binding:
output: true
input: false
Expand Down
4 changes: 3 additions & 1 deletion bindings/azure/storagequeues/storagequeues.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ type storageQueuesMetadata struct {
DecodeBase64 bool
EncodeBase64 bool
PollingInterval time.Duration `mapstructure:"pollingInterval"`
TTL *time.Duration `mapstructure:"ttlInSeconds"`
TTL *time.Duration `mapstructure:"ttl" mapstructurealiases:"ttlInSeconds"`
VisibilityTimeout *time.Duration
}

Expand Down Expand Up @@ -328,6 +328,8 @@ func parseMetadata(meta bindings.Metadata) (*storageQueuesMetadata, error) {
}
if ok {
m.TTL = &ttl
} else {
m.TTL = nil
}

return &m, nil
Expand Down
8 changes: 6 additions & 2 deletions bindings/azure/storagequeues/storagequeues_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,6 @@ func TestParseMetadata(t *testing.T) {
// expectedAccountKey: "myKey",
expectedQueueName: "queue1",
expectedQueueEndpointURL: "",
expectedTTL: ptr.Of(time.Duration(0)),
expectedPollingInterval: defaultPollingInterval,
expectedVisibilityTimeout: ptr.Of(defaultVisibilityTimeout),
},
Expand Down Expand Up @@ -394,7 +393,12 @@ func TestParseMetadata(t *testing.T) {
require.NoError(t, err)
// assert.Equal(t, tt.expectedAccountKey, meta.AccountKey)
assert.Equal(t, tt.expectedQueueName, meta.QueueName)
assert.Equal(t, tt.expectedTTL, meta.TTL)
if tt.expectedTTL != nil {
_ = assert.NotNil(t, meta.TTL, "Expected TTL to be %v", *tt.expectedTTL) &&
assert.Equal(t, *tt.expectedTTL, *meta.TTL)
} else if meta.TTL != nil {
assert.Failf(t, "Expected TTL to be nil", "Value was %v", *meta.TTL)
}
assert.Equal(t, tt.expectedQueueEndpointURL, meta.QueueEndpoint)
assert.Equal(t, tt.expectedVisibilityTimeout, meta.VisibilityTimeout)
})
Expand Down
6 changes: 3 additions & 3 deletions bindings/rabbitmq/metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ metadata:
description: "Enables or disables auto-delete."
default: 'false'
example: '"true", "false"'
- name: ttlInSeconds
type: number
- name: ttl
type: duration
description: |
Set the default message time to live at RabbitMQ queue level.
If this parameter is omitted, messages won't expire, continuing
to exist on the queue until processed.
example: '60'
example: '60s'
url:
title: "RabbitMQ Time-To-Live and Expiration"
url: "https://www.rabbitmq.com/ttl.html"
Expand Down
2 changes: 1 addition & 1 deletion bindings/rabbitmq/rabbitmq.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ type rabbitMQMetadata struct {
PrefetchCount int `mapstructure:"prefetchCount"`
MaxPriority *uint8 `mapstructure:"maxPriority"` // Priority Queue deactivated if nil
ReconnectWait time.Duration `mapstructure:"reconnectWaitInSeconds"`
DefaultQueueTTL *time.Duration `mapstructure:"ttlInSeconds"`
DefaultQueueTTL *time.Duration `mapstructure:"ttl" mapstructurealiases:"ttlInSeconds"`
CaCert string `mapstructure:"caCert"`
ClientCert string `mapstructure:"clientCert"`
ClientKey string `mapstructure:"clientKey"`
Expand Down
28 changes: 19 additions & 9 deletions metadata/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ import (
"strings"
"time"

kitmd "github.com/dapr/kit/metadata"
"github.com/dapr/kit/utils"
)

const (
// TTLMetadataKey defines the metadata key for setting a time to live (in seconds).
TTLMetadataKey = "ttlInSeconds"
// TTLMetadataKey defines the metadata key for setting a time to live (as a Go duration or number of seconds).
TTLMetadataKey = "ttl"
TTLInSecondsMetadataKey = "ttlInSeconds"

// RawPayloadKey defines the metadata key for forcing raw payload in pubsub.
RawPayloadKey = "rawPayload"
Expand All @@ -46,26 +48,34 @@ const (

// TryGetTTL tries to get the ttl as a time.Duration value for pubsub, binding and any other building block.
func TryGetTTL(props map[string]string) (time.Duration, bool, error) {
if val, ok := props[TTLMetadataKey]; ok && val != "" {
val, _ := kitmd.GetMetadataProperty(props, TTLMetadataKey, TTLInSecondsMetadataKey)
if val == "" {
return 0, false, nil
}

// Try to parse as duration string first
duration, err := time.ParseDuration(val)
if err != nil {
// Failed to parse Duration string.
// Let's try Integer and assume the value is in seconds
valInt64, err := strconv.ParseInt(val, 10, 64)
if err != nil {
return 0, false, fmt.Errorf("%s value must be a valid integer: actual is '%s'", TTLMetadataKey, val)
}

if valInt64 <= 0 {
return 0, false, fmt.Errorf("%s value must be higher than zero: actual is %d", TTLMetadataKey, valInt64)
return 0, false, fmt.Errorf("%s value must be higher than zero: actual is '%d'", TTLMetadataKey, valInt64)
}

duration := time.Duration(valInt64) * time.Second
duration = time.Duration(valInt64) * time.Second
if duration < 0 {
// Overflow
duration = math.MaxInt64
}

return duration, true, nil
} else if duration < 0 {
duration = 0
}

return 0, false, nil
return duration, true, nil
}

// TryGetPriority tries to get the priority for binding and any other building block.
Expand Down
106 changes: 106 additions & 0 deletions metadata/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ limitations under the License.
package metadata

import (
"fmt"
"math"
"reflect"
"testing"
"time"
Expand All @@ -24,6 +26,110 @@ import (
"github.com/dapr/kit/metadata"
)

func TestTryGetTTL(t *testing.T) {
tests := []struct {
name string
md map[string]string
result any
wantOK bool
wantErr bool
errStr string
}{
{
name: "ttl valid duration 20s",
md: map[string]string{
TTLMetadataKey: "20s",
},
result: time.Duration(20) * time.Second,
wantOK: true,
},
{
name: "ttl valid integer 20",
md: map[string]string{
TTLMetadataKey: "20",
},
result: time.Duration(20) * time.Second,
wantOK: true,
},
{
name: "ttlInSeconds valid duration 20s",
md: map[string]string{
TTLInSecondsMetadataKey: "20s",
},
result: time.Duration(20) * time.Second,
wantOK: true,
},
{
name: "ttlInSeconds valid integer 20",
md: map[string]string{
TTLInSecondsMetadataKey: "20",
},
result: time.Duration(20) * time.Second,
wantOK: true,
},
{
name: "invalid integer 20b",
md: map[string]string{
TTLMetadataKey: "20b",
},
wantOK: false,
wantErr: true,
errStr: "value must be a valid integer: actual is '20b'",
result: time.Duration(0) * time.Second,
},
{
name: "negative ttl -1",
md: map[string]string{
TTLMetadataKey: "-1",
},
wantOK: false,
wantErr: true,
errStr: "value must be higher than zero: actual is '-1'",
result: time.Duration(0) * time.Second,
},
{
name: "negative ttl -1s",
md: map[string]string{
TTLMetadataKey: "-1s",
},
wantOK: true,
wantErr: false,
result: time.Duration(0) * time.Second,
},
{
name: "no ttl",
md: map[string]string{},
wantOK: false,
wantErr: false,
result: time.Duration(0) * time.Second,
},
{
name: "out of range",
md: map[string]string{
TTLMetadataKey: fmt.Sprintf("%d1", math.MaxInt64),
},
wantOK: false,
wantErr: true,
result: time.Duration(0) * time.Second,
errStr: "value must be a valid integer: actual is '92233720368547758071'",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
d, ok, err := TryGetTTL(tt.md)

if tt.wantErr {
require.Error(t, err)
require.ErrorContains(t, err, tt.errStr)
} else {
require.NoError(t, err)
}
assert.Equal(t, tt.wantOK, ok, "wanted ok, but instead got not ok")
assert.Equal(t, tt.result, d, "expected result %v, but instead got = %v", tt.result, d)
})
}
}

func TestIsRawPayload(t *testing.T) {
t.Run("Metadata not found", func(t *testing.T) {
val, err := IsRawPayload(map[string]string{
Expand Down