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

Added GetTimeSent to message #158

Merged
merged 2 commits into from
Jan 11, 2024
Merged
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
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -807,16 +807,29 @@ message.Delay(<time.Duration>);
```

### 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
Expand Down
15 changes: 15 additions & 0 deletions consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@

"github.com/nats-io/nats.go"
"github.com/nats-io/nats.go/jetstream"
"google.golang.org/protobuf/encoding/protojson"

Check failure on line 30 in consumer.go

View workflow job for this annotation

GitHub Actions / run-broker-and-test

cannot find package "google.golang.org/protobuf/encoding/protojson" in any of:
"google.golang.org/protobuf/proto"

Check failure on line 31 in consumer.go

View workflow job for this annotation

GitHub Actions / run-broker-and-test

cannot find package "google.golang.org/protobuf/proto" in any of:
"google.golang.org/protobuf/types/dynamicpb"
)

Expand Down Expand Up @@ -184,6 +184,21 @@
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 if _, ok := m.msg.(*nats.Msg); ok {
return time.Now(), 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
Expand Down
Loading