-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: replication: implement WAIT command (#16)
- Loading branch information
Showing
4 changed files
with
140 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package command | ||
|
||
import ( | ||
"errors" | ||
"strconv" | ||
"time" | ||
|
||
"github.com/codecrafters-io/redis-starter-go/internal/client" | ||
"github.com/codecrafters-io/redis-starter-go/pkg/resp" | ||
) | ||
|
||
type Wait struct { | ||
serverInfo ServerInfoProvider | ||
} | ||
|
||
func (w *Wait) Execute(c *client.Client, wr *resp.Writer, args []*resp.Resp) error { | ||
if len(args) != 2 { | ||
return wr.WriteError(errors.New("ERR wrong number of arguments for 'wait' command")) | ||
} | ||
|
||
numReplicas, err := strconv.Atoi(args[0].String()) | ||
if err != nil || numReplicas < 0 { | ||
return wr.WriteError(errors.New("ERR invalid number of replicas")) | ||
} | ||
|
||
timeoutMillis, err := strconv.Atoi(args[1].String()) | ||
if err != nil || timeoutMillis < 0 { | ||
return wr.WriteError(errors.New("ERR invalid timeout")) | ||
} | ||
|
||
clientOffset := c.GetLastWriteOffset() | ||
// If there's no pending writes, return immediately | ||
if clientOffset == 0 { | ||
return wr.WriteSimpleValue(resp.Integer, []byte("0")) | ||
} | ||
|
||
// Start waiting for replicas to acknowledge up to clientOffset | ||
startTime := time.Now() | ||
timeout := time.Duration(timeoutMillis) * time.Millisecond | ||
for { | ||
ackedReplicas := w.serverInfo.GetReplicaAcknowledgedCount(clientOffset) | ||
if ackedReplicas >= numReplicas { | ||
return wr.WriteSimpleValue(resp.Integer, []byte(strconv.Itoa(ackedReplicas))) | ||
} | ||
|
||
if timeoutMillis == 0 { | ||
// Block forever, so just sleep briefly | ||
time.Sleep(10 * time.Millisecond) | ||
continue | ||
} | ||
|
||
elapsed := time.Since(startTime) | ||
if elapsed >= timeout { | ||
return wr.WriteSimpleValue(resp.Integer, []byte(strconv.Itoa(ackedReplicas))) | ||
} | ||
|
||
// Sleep briefly before checking again | ||
time.Sleep(10 * time.Millisecond) | ||
} | ||
} | ||
|
||
func (w *Wait) IsBlocking(_ []*resp.Resp) bool { | ||
return true | ||
} |