Skip to content

Commit

Permalink
Support SYNC and PSYNC (#1741)
Browse files Browse the repository at this point in the history
Co-authored-by: Chayim <chayim@users.noreply.github.com>
  • Loading branch information
AvitalFineRedis and chayim authored Dec 15, 2021
1 parent 6c1e215 commit 82bad16
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
25 changes: 25 additions & 0 deletions redis/commands/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,31 @@ def flushdb(self, asynchronous=False, **kwargs):
args.append(b"ASYNC")
return self.execute_command("FLUSHDB", *args, **kwargs)

def sync(self):
"""
Initiates a replication stream from the master.
For more information check https://redis.io/commands/sync
"""
from redis.client import NEVER_DECODE

options = {}
options[NEVER_DECODE] = []
return self.execute_command("SYNC", **options)

def psync(self, replicationid, offset):
"""
Initiates a replication stream from the master.
Newer version for `sync`.
For more information check https://redis.io/commands/sync
"""
from redis.client import NEVER_DECODE

options = {}
options[NEVER_DECODE] = []
return self.execute_command("PSYNC", replicationid, offset, **options)

def swapdb(self, first, second, **kwargs):
"""
Swap two databases
Expand Down
2 changes: 1 addition & 1 deletion redis/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ def __del__(self):
except Exception:
pass

def on_connect(self, connection):
def on_connect(self, connection, **kwargs):
self._sock = connection._sock
self._socket_timeout = connection.socket_timeout
kwargs = {
Expand Down
12 changes: 12 additions & 0 deletions tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -4151,6 +4151,18 @@ def test_replicaof(self, r):
assert r.replicaof("NO ONE")
assert r.replicaof("NO", "ONE")

@skip_if_server_version_lt("2.8.0")
def test_sync(self, r):
r2 = redis.Redis(port=6380, decode_responses=False)
res = r2.sync()
assert b"REDIS" in res

@skip_if_server_version_lt("2.8.0")
def test_psync(self, r):
r2 = redis.Redis(port=6380, decode_responses=False)
res = r2.psync(r2.client_id(), 1)
assert b"FULLRESYNC" in res


@pytest.mark.onlynoncluster
class TestBinarySave:
Expand Down

0 comments on commit 82bad16

Please sign in to comment.