Fix channel concurency issue

This commit is contained in:
Zoe Roux 2024-01-12 21:01:25 +01:00
parent b0c0ca0e0f
commit 45091da5ac
3 changed files with 8 additions and 8 deletions

View File

@ -1 +1,2 @@
/target /target
transcoder

View File

@ -9,27 +9,26 @@ type Transcoder struct {
// All file streams currently running, index is file path // All file streams currently running, index is file path
streams map[string]FileStream streams map[string]FileStream
// Streams that are staring up // Streams that are staring up
preparing map[string]bool preparing map[string]chan *FileStream
mutex sync.RWMutex mutex sync.RWMutex
channel chan *FileStream
} }
func (t *Transcoder) GetMaster(path string, client string) (string, error) { func (t *Transcoder) GetMaster(path string, client string) (string, error) {
t.mutex.RLock() t.mutex.RLock()
stream, ok := t.streams[path] stream, ok := t.streams[path]
preparing := t.preparing[path] channel, preparing := t.preparing[path]
t.mutex.RUnlock() t.mutex.RUnlock()
if preparing { if preparing {
pstream := <-t.channel pstream := <-channel
if pstream == nil { if pstream == nil {
return "", errors.New("could not transcode file. Try again later") return "", errors.New("could not transcode file. Try again later")
} }
stream = *pstream stream = *pstream
} else if !ok { } else if !ok {
t.mutex.Lock() t.mutex.Lock()
t.preparing[path] = true channel = make(chan *FileStream, 1)
t.preparing[path] = channel
t.cleanUnused() t.cleanUnused()
t.mutex.Unlock() t.mutex.Unlock()
@ -38,7 +37,7 @@ func (t *Transcoder) GetMaster(path string, client string) (string, error) {
t.mutex.Lock() t.mutex.Lock()
delete(t.preparing, path) delete(t.preparing, path)
t.mutex.Unlock() t.mutex.Unlock()
t.channel <- nil channel <- nil
return "", err return "", err
} }
@ -48,7 +47,7 @@ func (t *Transcoder) GetMaster(path string, client string) (string, error) {
delete(t.preparing, path) delete(t.preparing, path)
t.mutex.Unlock() t.mutex.Unlock()
t.channel <- stream channel <- stream
} }
return stream.GetMaster(), nil return stream.GetMaster(), nil

Binary file not shown.