Created IsMaxMemoryExceeded utility function that is used to check whether the server has exceeded the max memory.

Check whether max memory is exceeded before setting value or creating new key.
Return error from SetValue function when max memory is exceeded. This change has been propagated to the aof and snapshot engines as well.
Snapshot engine now only accepts SetValue option. No longer does it neet to use CreateKeyAndLock & KeyUnlock functions itself.
This commit is contained in:
Kelvin Mwinuka
2024-03-06 15:20:45 +08:00
parent f23bbd481e
commit 118e155144
7 changed files with 65 additions and 25 deletions

View File

@@ -95,9 +95,16 @@ func NewServer(opts Opts) *Server {
GetState: server.GetState,
SetLatestSnapshotMilliseconds: server.SetLatestSnapshot,
GetLatestSnapshotMilliseconds: server.GetLatestSnapshot,
CreateKeyAndLock: server.CreateKeyAndLock,
KeyUnlock: server.KeyUnlock,
SetValue: server.SetValue,
SetValue: func(key string, value interface{}) error {
if _, err := server.CreateKeyAndLock(context.Background(), key); err != nil {
return err
}
if err := server.SetValue(context.Background(), key, value); err != nil {
return err
}
server.KeyUnlock(key)
return nil
},
})
// Set up standalone AOF engine
server.AOFEngine = aof.NewAOFEngine(
@@ -106,13 +113,15 @@ func NewServer(opts Opts) *Server {
aof.WithStartRewriteFunc(server.StartRewriteAOF),
aof.WithFinishRewriteFunc(server.FinishRewriteAOF),
aof.WithGetStateFunc(server.GetState),
aof.WithSetValueFunc(func(key string, value interface{}) {
aof.WithSetValueFunc(func(key string, value interface{}) error {
if _, err := server.CreateKeyAndLock(context.Background(), key); err != nil {
log.Println(err)
return
return err
}
if err := server.SetValue(context.Background(), key, value); err != nil {
return err
}
server.SetValue(context.Background(), key, value)
server.KeyUnlock(key)
return nil
}),
aof.WithHandleCommandFunc(func(command []byte) {
_, err := server.handleCommand(context.Background(), command, nil, true)