Skip to content
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: 6 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,10 @@ var (
Code: 112,
Message: "unrecoverable position",
}
// ErrorIncorrectHistoryTTLConfiguration means that history meta TTL is less than history TTL.
// History meta TTL must be greater than or equal to history TTL to avoid stream inconsistencies.
ErrorIncorrectHistoryTTLConfiguration = &Error{
Code: 113,
Message: "history meta TTL must be greater than or equal to history TTL",
}
)
12 changes: 12 additions & 0 deletions node.go
Original file line number Diff line number Diff line change
Expand Up @@ -771,6 +771,18 @@ func (n *Node) publish(ch string, data []byte, opts ...PublishOption) (PublishRe
for _, opt := range opts {
opt(pubOpts)
}

// Validate history TTL configuration if history is enabled.
if pubOpts.HistorySize > 0 && pubOpts.HistoryTTL > 0 {
historyMetaTTL := pubOpts.HistoryMetaTTL
if historyMetaTTL == 0 {
historyMetaTTL = n.config.HistoryMetaTTL
}
if historyMetaTTL > 0 && historyMetaTTL < pubOpts.HistoryTTL {
return PublishResult{}, ErrorIncorrectHistoryTTLConfiguration
}
}

n.metrics.incMessagesSent("publication", ch)
streamPos, fromCache, err := n.getBroker(ch).Publish(ch, data, *pubOpts)
if err != nil {
Expand Down
29 changes: 29 additions & 0 deletions node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1471,3 +1471,32 @@ func TestGetPresenceManager(t *testing.T) {
_, err = node.Presence("test2")
require.NoError(t, err)
}

func TestNode_PublishHistoryTTLValidation(t *testing.T) {
node := defaultTestNode()
defer func() { _ = node.Shutdown(context.Background()) }()

_, err := node.Publish("test", []byte("{}"), WithHistory(10, time.Hour, 2*time.Hour))
require.NoError(t, err)

_, err = node.Publish("test", []byte("{}"), WithHistory(10, time.Hour, time.Hour))
require.NoError(t, err)

_, err = node.Publish("test", []byte("{}"), WithHistory(10, 2*time.Hour, time.Hour))
require.Error(t, err)
require.Equal(t, ErrorIncorrectHistoryTTLConfiguration, err)

_, err = node.Publish("test", []byte("{}"), WithHistory(10, time.Hour))
require.NoError(t, err)

longTTL := 31 * 24 * time.Hour // 31 days
_, err = node.Publish("test", []byte("{}"), WithHistory(10, longTTL))
require.Error(t, err)
require.Equal(t, ErrorIncorrectHistoryTTLConfiguration, err)

_, err = node.Publish("test", []byte("{}"), WithHistory(0, 2*time.Hour, time.Hour))
require.NoError(t, err)

_, err = node.Publish("test", []byte("{}"), WithHistory(10, 0, time.Hour))
require.NoError(t, err)
}