From 3c370d63560120d7d86e2a4638732ffd3e3718fd Mon Sep 17 00:00:00 2001 From: Bazen <49089563+bazen-teklehaymanot@users.noreply.github.com> Date: Tue, 9 Jan 2024 14:41:25 +0200 Subject: [PATCH 1/2] Added GetTimeSent to message --- README.md | 13 +++++++++++++ consumer.go | 13 +++++++++++++ 2 files changed, 26 insertions(+) diff --git a/README.md b/README.md index 27b40f8..6e56f38 100644 --- a/README.md +++ b/README.md @@ -807,16 +807,29 @@ message.Delay(); ``` ### Get headers + Get headers per message + ```go headers := msg.GetHeaders() ``` ### Get message sequence number + Get message sequence number + ```go sequenceNumber, err := msg.GetSequenceNumber() ``` + +### Get message time sent + +Get message time sent + +```go +timeSent, err := msg.GetTimeSent() +``` + ### Destroying a Consumer ```go diff --git a/consumer.go b/consumer.go index 2eeafa7..a1959a6 100644 --- a/consumer.go +++ b/consumer.go @@ -184,6 +184,19 @@ func (m *Msg) GetSequenceNumber() (uint64, error) { return seq, nil } +// Msg.GetTimeSent - get message's time sent +func (m *Msg) GetTimeSent() (time.Time, error) { + if jsMsg, ok := m.msg.(jetstream.Msg); ok { + md, err := jsMsg.Metadata() + if err != nil { + return time.Time{}, errors.New("message format is not supported") + } + return md.Timestamp, nil + } else { + return time.Time{}, errors.New("message format is not supported") + } +} + // Msg.Ack - ack the message. func (m *Msg) Ack() error { var err error From 4315a67e9b4e28e8aba9b634f39b82651eb21ed6 Mon Sep 17 00:00:00 2001 From: Bazen <49089563+bazen-teklehaymanot@users.noreply.github.com> Date: Thu, 11 Jan 2024 15:32:43 +0200 Subject: [PATCH 2/2] Added support for getting current time when message format is nats.Msg --- consumer.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/consumer.go b/consumer.go index a1959a6..2d236b6 100644 --- a/consumer.go +++ b/consumer.go @@ -192,6 +192,8 @@ func (m *Msg) GetTimeSent() (time.Time, error) { return time.Time{}, errors.New("message format is not supported") } return md.Timestamp, nil + } else if _, ok := m.msg.(*nats.Msg); ok { + return time.Now(), nil } else { return time.Time{}, errors.New("message format is not supported") }