@@ -192,12 +192,8 @@ func (wl *walForge) rotateLogIfNeeded(entrySize uint32) error {
192192
193193// rotateLog rotates the log by closing the current segment file,
194194// incrementing the current segment index, and opening a new segment file.
195- // This method is thread safe.
196195func (wl * walForge ) rotateLog () error {
197196 fmt .Println ("rotating log" )
198- wl .mu .Lock ()
199- defer wl .mu .Unlock ()
200-
201197 // TODO: Ideally this function should not return any error
202198 // Check for the conditions where it can return an error
203199 // and handle them gracefully.
@@ -237,11 +233,7 @@ func (wl *walForge) rotateLog() error {
237233
238234// Writes out any data in the WAL's in-memory buffer to the segment file.
239235// and syncs the segment file to disk.
240- // This method is thread safe.
241236func (wl * walForge ) sync () error {
242- wl .mu .Lock ()
243- defer wl .mu .Unlock ()
244-
245237 // Flush the buffer to the segment file
246238 if err := wl .csWriter .Flush (); err != nil {
247239 return err
@@ -265,10 +257,12 @@ func (wl *walForge) periodicSyncBuffer() {
265257 for {
266258 select {
267259 case <- wl .bufferSyncTicker .C :
260+ wl .mu .Lock ()
268261 err := wl .sync ()
269262 if err != nil {
270263 slog .Error ("failed to sync buffer" , slog .String ("error" , err .Error ()))
271264 }
265+ wl .mu .Unlock ()
272266 case <- wl .ctx .Done ():
273267 return
274268 }
@@ -281,9 +275,11 @@ func (wl *walForge) periodicRotateSegment() {
281275 select {
282276 case <- wl .segmentRotationTicker .C :
283277 // TODO: Remove this error handling once we clean up the error handling in the rotateLog function.
278+ wl .mu .Lock ()
284279 if err := wl .rotateLog (); err != nil {
285280 slog .Error ("failed to rotate segment" , slog .String ("error" , err .Error ()))
286281 }
282+ wl .mu .Unlock ()
287283 case <- wl .ctx .Done ():
288284 return
289285 }
0 commit comments