Use mutex when reloading file notifier

This commit is contained in:
Julien Riou 2018-06-11 21:05:34 +02:00
parent 3a89201a94
commit 9d72ce2201
No known key found for this signature in database
GPG key ID: BA3E15176E45E85D

View file

@ -4,6 +4,7 @@ import (
"github.com/jouir/pgterminate/base" "github.com/jouir/pgterminate/base"
"log" "log"
"os" "os"
"sync"
"time" "time"
) )
@ -12,6 +13,7 @@ type File struct {
handle *os.File handle *os.File
name string name string
sessions chan base.Session sessions chan base.Session
mutex sync.Mutex
} }
// NewFile creates a file notifier // NewFile creates a file notifier
@ -43,6 +45,8 @@ func (f *File) open() {
// Reload closes and re-open the file to be compatible with logrotate // Reload closes and re-open the file to be compatible with logrotate
func (f *File) Reload() { func (f *File) Reload() {
f.mutex.Lock()
defer f.mutex.Unlock()
log.Println("Re-opening log file", f.name) log.Println("Re-opening log file", f.name)
f.handle.Close() f.handle.Close()
f.open() f.open()