This repository has been archived by the owner on Feb 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pm.go
93 lines (74 loc) · 2.02 KB
/
pm.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package fshandler
import (
"context"
"io"
"net/http"
"net/url"
"os"
"path/filepath"
"sync"
"github.com/otofune/hlsq"
"github.com/otofune/hlsq/ctxlogger"
"github.com/otofune/hlsq/repeahttp"
"golang.org/x/sync/semaphore"
)
type persistentManager struct {
client *http.Client
destDir string
downloadSW *semaphore.Weighted
downloaded sync.Map
playlistMutex sync.Mutex
}
func newPersistentManager(client *http.Client, destDir string, parallelism int64) persistentManager {
return persistentManager{
client: client,
destDir: destDir,
downloadSW: semaphore.NewWeighted(parallelism),
}
}
func (pm *persistentManager) saveURLTo(ctx context.Context, u *url.URL, path string) error {
logger := ctxlogger.ExtractLogger(ctx)
if _, loaded := pm.downloaded.LoadOrStore(u.String(), struct{}{}); loaded {
logger.Debugf("skip %s cuz already got\n", path)
return nil // skip already got
}
logger.Debugf("waiting lock for saving %s\n", path)
if err := pm.downloadSW.Acquire(ctx, 1); err != nil {
return err
}
defer pm.downloadSW.Release(1)
logger.Debugf("acquired lock for saving %s\n", path)
f, err := os.OpenFile(filepath.Join(pm.destDir, path), os.O_WRONLY|os.O_CREATE, 0o644)
if err != nil {
return err
}
defer f.Close()
resp, err := repeahttp.Get(ctx, pm.client, u)
if err != nil {
return err
}
defer resp.Body.Close()
if _, err := io.Copy(f, resp.Body); err != nil {
return err
}
logger.Debugf("saved %s\n", path)
return nil
}
func (pm *persistentManager) savePlaylist(ctx context.Context, playlist hlsq.MediaSegments, playlistClosed bool) error {
logger := ctxlogger.ExtractLogger(ctx)
pm.playlistMutex.Lock()
logger.Debugf("Saving play.m3u8 with lock")
f, err := os.OpenFile(filepath.Join(pm.destDir, "play.m3u8"), os.O_WRONLY|os.O_CREATE, 0o644)
if err != nil {
return err
}
if _, err := f.WriteString(playlist.String(playlistClosed)); err != nil {
return err
}
if err := f.Close(); err != nil {
return err
}
logger.Debugf("Saved play.m3u8")
pm.playlistMutex.Unlock()
return nil
}