Class: Pgoutput::Client::Keepalive

Inherits:
KeepaliveData show all
Defined in:
lib/pgoutput/client/keepalive.rb

Overview

Immutable primary keepalive replication message.

PostgreSQL sends keepalive CopyData payloads while a replication stream is active. The payload layout is:

“‘text Byte 0 : message tag `k` Bytes 1-8 : current server WAL end, unsigned 64-bit big-endian Bytes 9-16 : server clock, PostgreSQL timestamp format Byte 17 : reply-requested flag, 1 for immediate feedback “`

The stream layer uses this message to advance its known WAL position and to decide whether to send standby status feedback immediately.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#reply_requestedBoolean (readonly)

whether PostgreSQL requested immediate feedback

Returns:

  • (Boolean)

    the current value of reply_requested



27
28
29
# File 'lib/pgoutput/client/keepalive.rb', line 27

def reply_requested
  @reply_requested
end

#server_clockInteger (readonly)

PostgreSQL server timestamp in microseconds since 2000-01-01 UTC

Returns:

  • (Integer)

    the current value of server_clock



27
28
29
# File 'lib/pgoutput/client/keepalive.rb', line 27

def server_clock
  @server_clock
end

#wal_endInteger (readonly)

latest server WAL position

Returns:

  • (Integer)

    the current value of wal_end



27
28
29
# File 'lib/pgoutput/client/keepalive.rb', line 27

def wal_end
  @wal_end
end

Class Method Details

.parse(bytes) ⇒ Keepalive

Parse a keepalive CopyData payload.

Parameters:

  • bytes (String)

    raw CopyData payload beginning with ‘k`

Returns:

  • (Keepalive)

    immutable parsed keepalive message

Raises:

  • (ProtocolError)

    if the payload is empty, has the wrong message tag, or is too short to contain the required fields



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/pgoutput/client/keepalive.rb', line 34

def self.parse(bytes)
  binary = bytes.b
  raise ProtocolError, "empty CopyData payload" if binary.empty?
  raise ProtocolError, "expected keepalive message" unless binary.getbyte(0) == "k".ord
  raise ProtocolError, "truncated keepalive message" if binary.bytesize < 18

  wal_end = unpack_u64(binary, 1)
  server_clock = unpack_u64(binary, 9)
  reply_requested = binary.getbyte(17) == 1

  Ractor.make_shareable(new(wal_end, server_clock, reply_requested))
end

Instance Method Details

#wal_end_lsnString

Latest server WAL position formatted as a PostgreSQL LSN string.

Returns:

  • (String)


50
# File 'lib/pgoutput/client/keepalive.rb', line 50

def wal_end_lsn = LSN.format(wal_end)