mirror of
https://github.com/zoriya/Kyoo.git
synced 2025-05-24 02:02:36 -04:00
Fix channel concurency issue
This commit is contained in:
parent
b0c0ca0e0f
commit
45091da5ac
1
transcoder/.gitignore
vendored
1
transcoder/.gitignore
vendored
@ -1 +1,2 @@
|
|||||||
/target
|
/target
|
||||||
|
transcoder
|
||||||
|
@ -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.
Loading…
x
Reference in New Issue
Block a user