map hotfix

This commit is contained in:
simon 2019-07-24 16:14:15 -04:00
parent bb3282c467
commit 8067eb0bfe
3 changed files with 16 additions and 14 deletions

View File

@ -12,7 +12,7 @@ import (
) )
type Beemer struct { type Beemer struct {
fileMap map[string]*File fileMap *sync.Map
tempDir string tempDir string
beemCommand func(string, string) (string, []string) beemCommand func(string, string) (string, []string)
beemChan chan string beemChan chan string
@ -61,19 +61,19 @@ func (b *Beemer) initWatchDir(watchDir string) {
func (b *Beemer) getAndResetTimer(name string) *time.Timer { func (b *Beemer) getAndResetTimer(name string) *time.Timer {
file, ok := b.fileMap[name] file, ok := b.fileMap.Load(name)
if ok { if ok {
file.WaitTimer.Stop() file.(*File).WaitTimer.Stop()
if file.BeemLock == true { if file.(*File).BeemLock == true {
return nil return nil
} }
} }
newTimer := time.NewTimer(b.inactiveDelay) newTimer := time.NewTimer(b.inactiveDelay)
b.fileMap[name] = &File{ b.fileMap.Store(name, &File{
newTimer, newTimer,
false, false,
} })
return newTimer return newTimer
} }
@ -97,9 +97,9 @@ func (b *Beemer) handleFileChange(event fsnotify.Event) {
go b.handleFileInactive(t, event.Name) go b.handleFileInactive(t, event.Name)
} }
} else if event.Op&fsnotify.Remove == fsnotify.Remove || event.Op&fsnotify.Rename == fsnotify.Rename { } else if event.Op&fsnotify.Remove == fsnotify.Remove || event.Op&fsnotify.Rename == fsnotify.Rename {
if file, ok := b.fileMap[event.Name]; ok { if file, ok := b.fileMap.Load(event.Name); ok {
file.WaitTimer.Stop() file.(*File).WaitTimer.Stop()
delete(b.fileMap, event.Name) b.fileMap.Delete(event.Name)
} }
} }
} }
@ -191,7 +191,8 @@ func (b *Beemer) beemTar() {
func (b *Beemer) handleFileInactive(t *time.Timer, name string) { func (b *Beemer) handleFileInactive(t *time.Timer, name string) {
<-t.C <-t.C
b.fileMap[name].BeemLock = true file, _ := b.fileMap.Load(name)
file.(*File).BeemLock = true
logrus.WithFields(logrus.Fields{ logrus.WithFields(logrus.Fields{
"name": name, "name": name,

2
cli.go
View File

@ -74,7 +74,7 @@ func main() {
} }
beemer := Beemer{ beemer := Beemer{
fileMap: make(map[string]*File, 0), fileMap: &sync.Map{},
beemChan: make(chan string, transfers), beemChan: make(chan string, transfers),
tarChan: make(chan string, 100), tarChan: make(chan string, 100),
beemCommand: parseCommand(cmdString), beemCommand: parseCommand(cmdString),

View File

@ -109,9 +109,10 @@ func isDir(name string) bool {
func (b *Beemer) dispose() { func (b *Beemer) dispose() {
b.watcher.Close() b.watcher.Close()
for _, v := range b.fileMap { b.fileMap.Range(func(key, value interface{}) bool {
v.WaitTimer.Stop() value.(*File).WaitTimer.Stop()
} return true
})
close(b.beemChan) close(b.beemChan)