Skip to content

js.get_msg(..., direct=True) does not populate RawStreamMsg.time #809

@oliverlambson

Description

@oliverlambson

Proposed change

Parse "Nats-Time-Stamp" header into RawStreamMsg.time

In js.get_msg(..., direct=False), time is populated by RawStreamMsg.from_response(), but from_response() isn't used for direct get because the time is in response.headers["Nats-Time-Stamp"] instead of the response["time"]

A way to do this would be:

  • pull the RawStreamMsg._python38_iso_parsing classmethod out into a helper function
  • use that helper function in JetstreamManager._lift_msg_to_raw_msg:
  @classmethod
  def _lift_msg_to_raw_msg(self, msg) -> api.RawStreamMsg:
      ...
      seq = msg.headers.get("Nats-Sequence")
      if seq:
          raw_msg.seq = int(seq)
      raw_msg.data = msg.data
      raw_msg.headers = msg.headers
+     if time_string := msg.headers.get("Nats-Time-Stamp"):
+         raw_msg.time = parse_iso_dt(time_string)

      return raw_msg

Use case

Anyone who wants to use the message timestamp from a direct get currently has to parse it out manually, which is unexpected behaviour and not a great experience:

from datetime import UTC, datetime

nc = nats.connect()
js = nc.jetstream()
msg = await js.get_msg(..., direct=True)
if msg.time is None and msg.headers and (dt := msg.headers.get("Nats-Time-Stamp")):
    msg.time = datetime.fromisoformat(dt).astimezone(UTC)

# parsing above requires python 3.11+
# for <3.11 need to drop ns from dt string first and use datetime.timezone.utc

Contribution

Sure

Metadata

Metadata

Assignees

No one assigned

    Labels

    proposalEnhancement idea or proposal

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions