Skip to content

Commit 82bad16

Browse files
Support SYNC and PSYNC (#1741)
Co-authored-by: Chayim <chayim@users.noreply.github.com>
1 parent 6c1e215 commit 82bad16

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

redis/commands/core.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,31 @@ def flushdb(self, asynchronous=False, **kwargs):
637637
args.append(b"ASYNC")
638638
return self.execute_command("FLUSHDB", *args, **kwargs)
639639

640+
def sync(self):
641+
"""
642+
Initiates a replication stream from the master.
643+
644+
For more information check https://redis.io/commands/sync
645+
"""
646+
from redis.client import NEVER_DECODE
647+
648+
options = {}
649+
options[NEVER_DECODE] = []
650+
return self.execute_command("SYNC", **options)
651+
652+
def psync(self, replicationid, offset):
653+
"""
654+
Initiates a replication stream from the master.
655+
Newer version for `sync`.
656+
657+
For more information check https://redis.io/commands/sync
658+
"""
659+
from redis.client import NEVER_DECODE
660+
661+
options = {}
662+
options[NEVER_DECODE] = []
663+
return self.execute_command("PSYNC", replicationid, offset, **options)
664+
640665
def swapdb(self, first, second, **kwargs):
641666
"""
642667
Swap two databases

redis/connection.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ def __del__(self):
382382
except Exception:
383383
pass
384384

385-
def on_connect(self, connection):
385+
def on_connect(self, connection, **kwargs):
386386
self._sock = connection._sock
387387
self._socket_timeout = connection.socket_timeout
388388
kwargs = {

tests/test_commands.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4151,6 +4151,18 @@ def test_replicaof(self, r):
41514151
assert r.replicaof("NO ONE")
41524152
assert r.replicaof("NO", "ONE")
41534153

4154+
@skip_if_server_version_lt("2.8.0")
4155+
def test_sync(self, r):
4156+
r2 = redis.Redis(port=6380, decode_responses=False)
4157+
res = r2.sync()
4158+
assert b"REDIS" in res
4159+
4160+
@skip_if_server_version_lt("2.8.0")
4161+
def test_psync(self, r):
4162+
r2 = redis.Redis(port=6380, decode_responses=False)
4163+
res = r2.psync(r2.client_id(), 1)
4164+
assert b"FULLRESYNC" in res
4165+
41544166

41554167
@pytest.mark.onlynoncluster
41564168
class TestBinarySave:

0 commit comments

Comments
 (0)