From 9a912d5385c2c1a16af2e1a753d25358124ed467 Mon Sep 17 00:00:00 2001 From: Tim Cooper Date: Mon, 11 Oct 2021 18:29:36 -0500 Subject: [PATCH] remove global variables from core/storageproviders (#1444) --- core/storage.go | 4 ++-- core/storageproviders/local.go | 12 ++++++++---- core/storageproviders/s3Storage.go | 29 ++++++++++++++++++----------- 3 files changed, 28 insertions(+), 17 deletions(-) diff --git a/core/storage.go b/core/storage.go index 962a1ce6a..90900d945 100644 --- a/core/storage.go +++ b/core/storage.go @@ -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 { diff --git a/core/storageproviders/local.go b/core/storageproviders/local.go index b45e90ca4..05203cfea 100644 --- a/core/storageproviders/local.go +++ b/core/storageproviders/local.go @@ -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) } }() diff --git a/core/storageproviders/s3Storage.go b/core/storageproviders/s3Storage.go index 27b62c7b7..875170c66 100644 --- a/core/storageproviders/s3Storage.go +++ b/core/storageproviders/s3Storage.go @@ -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())