def _readiness(self) -> Optional[str]:
        """Check if readiness conditions are met.

        :returns: None if node can be considered ready or diagnostic message if not."""

        patroni = self.server.patroni
        if patroni.ha.is_leader():
            # We only become leader after bootstrap or once up as a standby, so we are definitely ready.
            return

        # When postgres is not running we are not ready.
        if patroni.postgresql.state != PostgresqlState.RUNNING:
            return 'PostgreSQL is not running'

        postgres = self.get_postgresql_status(True)
        latest_end_lsn = postgres.get('latest_end_lsn', 0)

        if postgres.get('replication_state') != 'streaming':
            return 'PostgreSQL replication state is not streaming'

        cluster = patroni.dcs.cluster

        if not cluster and not latest_end_lsn:
            if patroni.ha.failsafe_is_active():
                return
            return 'DCS is not accessible'

        leader_optime = max(cluster and cluster.status.last_lsn or 0, latest_end_lsn)

        mode = 'write' if self.path_query.get('mode', [None])[0] == 'write' else 'apply'
        location = 'received_location' if mode == 'write' else 'replayed_location'
        lag = leader_optime - postgres.get('xlog', {}).get(location, 0)

        max_replica_lag = parse_int(self.path_query.get('lag', [None])[0], 'B')
        if max_replica_lag is None:
            max_replica_lag = global_config.maximum_lag_on_failover

        if lag > max_replica_lag:
            return f'Replication {mode} lag {lag} exceeds maximum allowable {max_replica_lag}'
Check if readiness conditions are met.

        :returns: None if node can be considered ready or diagnostic message if not.
patroni/api.py