Check for nil rw object in LogStore before attemting a sync

This commit is contained in:
Kelvin Mwinuka
2024-03-25 11:48:58 +08:00
parent aea3e0675c
commit ea0092e7cc
3 changed files with 706 additions and 703 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -113,6 +113,12 @@ func NewAOFEngine(options ...func(engine *Engine)) *Engine {
handleCommand: func(command []byte) {}, handleCommand: func(command []byte) {},
} }
// Setup AOFEngine options first as these options are used
// when setting up the PreambleStore and AppendStore
for _, option := range options {
option(engine)
}
// Setup Preamble engine // Setup Preamble engine
engine.preambleStore = preamble.NewPreambleStore( engine.preambleStore = preamble.NewPreambleStore(
preamble.WithDirectory(engine.directory), preamble.WithDirectory(engine.directory),
@@ -129,10 +135,6 @@ func NewAOFEngine(options ...func(engine *Engine)) *Engine {
logstore.WithHandleCommandFunc(engine.handleCommand), logstore.WithHandleCommandFunc(engine.handleCommand),
) )
for _, option := range options {
option(engine)
}
// 3. Start the goroutine to pick up queued commands in order to write them to the file. // 3. Start the goroutine to pick up queued commands in order to write them to the file.
// LogCommand will get the open file handler from the struct top perform the AOF operation. // LogCommand will get the open file handler from the struct top perform the AOF operation.
go func() { go func() {

View File

@@ -69,13 +69,11 @@ func WithHandleCommandFunc(f func(command []byte)) func(store *AppendStore) {
func NewAppendStore(options ...func(store *AppendStore)) *AppendStore { func NewAppendStore(options ...func(store *AppendStore)) *AppendStore {
store := &AppendStore{ store := &AppendStore{
directory: "", directory: "",
strategy: "everysec", strategy: "everysec",
rw: nil, rw: nil,
mut: sync.Mutex{}, mut: sync.Mutex{},
handleCommand: func(command []byte) { handleCommand: func(command []byte) {},
// No-Op
},
} }
for _, option := range options { for _, option := range options {
@@ -130,8 +128,11 @@ func (store *AppendStore) Write(command []byte) error {
func (store *AppendStore) Sync() error { func (store *AppendStore) Sync() error {
store.mut.Lock() store.mut.Lock()
store.mut.Unlock() defer store.mut.Unlock()
return store.rw.Sync() if store.rw != nil {
return store.rw.Sync()
}
return nil
} }
func (store *AppendStore) Restore() error { func (store *AppendStore) Restore() error {