remove global variables from core/storageproviders (#1444)

This commit is contained in:
Tim Cooper 2021-10-11 18:29:36 -05:00 committed by GitHub
parent 8098304967
commit 9a912d5385
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 17 deletions

View file

@ -9,9 +9,9 @@ func setupStorage() error {
s3Config := data.GetS3Config()
if s3Config.Enabled {
_storage = &storageproviders.S3Storage{}
_storage = storageproviders.NewS3Storage()
} else {
_storage = &storageproviders.LocalStorage{}
_storage = storageproviders.NewLocalStorage()
}
if err := _storage.Setup(); err != nil {

View file

@ -11,18 +11,22 @@ import (
// LocalStorage represents an instance of the local storage provider for HLS video.
type LocalStorage struct {
// Cleanup old public HLS content every N min from the webroot.
onlineCleanupTicker *time.Ticker
}
// Cleanup old public HLS content every N min from the webroot.
var _onlineCleanupTicker *time.Ticker
// NewLocalStorage returns a new LocalStorage instance.
func NewLocalStorage() *LocalStorage {
return &LocalStorage{}
}
// Setup configures this storage provider.
func (s *LocalStorage) Setup() error {
// NOTE: This cleanup timer will have to be disabled to support recordings in the future
// as all HLS segments have to be publicly available on disk to keep a recording of them.
_onlineCleanupTicker = time.NewTicker(1 * time.Minute)
s.onlineCleanupTicker = time.NewTicker(1 * time.Minute)
go func() {
for range _onlineCleanupTicker.C {
for range s.onlineCleanupTicker.C {
transcoder.CleanupOldContent(config.HLSStoragePath)
}
}()

View file

@ -22,10 +22,6 @@ import (
"github.com/grafov/m3u8"
)
// If we try to upload a playlist but it is not yet on disk
// then keep a reference to it here.
var _queuedPlaylistUpdates = make(map[string]string)
// S3Storage is the s3 implementation of a storage provider.
type S3Storage struct {
sess *session.Session
@ -38,9 +34,20 @@ type S3Storage struct {
s3AccessKey string
s3Secret string
s3ACL string
// If we try to upload a playlist but it is not yet on disk
// then keep a reference to it here.
queuedPlaylistUpdates map[string]string
uploader *s3manager.Uploader
}
var _uploader *s3manager.Uploader
// NewS3Storage returns a new S3Storage instance.
func NewS3Storage() *S3Storage {
return &S3Storage{
queuedPlaylistUpdates: make(map[string]string),
}
}
// Setup sets up the s3 storage for saving the video to s3.
func (s *S3Storage) Setup() error {
@ -63,7 +70,7 @@ func (s *S3Storage) Setup() error {
s.sess = s.connectAWS()
_uploader = s3manager.NewUploader(s.sess)
s.uploader = s3manager.NewUploader(s.sess)
return nil
}
@ -93,7 +100,7 @@ func (s *S3Storage) SegmentWritten(localFilePath string) {
// them are in sync.
playlistPath := filepath.Join(filepath.Dir(localFilePath), "stream.m3u8")
if _, err := s.Save(playlistPath, 0); err != nil {
_queuedPlaylistUpdates[playlistPath] = playlistPath
s.queuedPlaylistUpdates[playlistPath] = playlistPath
if pErr, ok := err.(*os.PathError); ok {
log.Debugln(pErr.Path, "does not yet exist locally when trying to upload to S3 storage.")
return
@ -106,12 +113,12 @@ func (s *S3Storage) VariantPlaylistWritten(localFilePath string) {
// We are uploading the variant playlist after uploading the segment
// to make sure we're not referring to files in a playlist that don't
// yet exist. See SegmentWritten.
if _, ok := _queuedPlaylistUpdates[localFilePath]; ok {
if _, ok := s.queuedPlaylistUpdates[localFilePath]; ok {
if _, err := s.Save(localFilePath, 0); err != nil {
log.Errorln(err)
_queuedPlaylistUpdates[localFilePath] = localFilePath
s.queuedPlaylistUpdates[localFilePath] = localFilePath
}
delete(_queuedPlaylistUpdates, localFilePath)
delete(s.queuedPlaylistUpdates, localFilePath)
}
}
@ -152,7 +159,7 @@ func (s *S3Storage) Save(filePath string, retryCount int) (string, error) {
uploadInput.ACL = aws.String("public-read")
}
response, err := _uploader.Upload(uploadInput)
response, err := s.uploader.Upload(uploadInput)
if err != nil {
log.Traceln("error uploading:", filePath, err.Error())