2020-04-13 12:15:12 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-04-13 21:48:49 +00:00
|
|
|
"os"
|
2020-04-13 12:15:12 +00:00
|
|
|
"sync"
|
2020-04-13 21:48:49 +00:00
|
|
|
"time"
|
2020-04-13 12:15:12 +00:00
|
|
|
)
|
|
|
|
|
2020-04-19 20:13:09 +00:00
|
|
|
func startFileMonitor(sleepTime time.Duration) {
|
2020-04-13 21:48:49 +00:00
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
/* Sleep so we don't take up all the precious CPU time :) */
|
2020-04-19 20:13:09 +00:00
|
|
|
time.Sleep(sleepTime)
|
2020-04-15 10:57:42 +00:00
|
|
|
|
2020-04-17 15:39:25 +00:00
|
|
|
/* Check global file cache freshness */
|
2020-04-19 19:44:48 +00:00
|
|
|
checkCacheFreshness()
|
2020-04-13 21:48:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* We shouldn't have reached here */
|
2020-04-22 18:42:54 +00:00
|
|
|
Config.LogSystemFatal("FileCache monitor escaped run loop!\n")
|
2020-04-13 21:48:49 +00:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2020-04-19 19:44:48 +00:00
|
|
|
func checkCacheFreshness() {
|
|
|
|
/* Before anything, get cache write lock (in case we have to delete) */
|
2020-04-22 18:42:54 +00:00
|
|
|
Config.FileCache.CacheMutex.Lock()
|
2020-04-15 10:57:42 +00:00
|
|
|
|
|
|
|
/* Iterate through paths in cache map to query file last modified times */
|
2020-04-22 18:42:54 +00:00
|
|
|
for path := range Config.FileCache.CacheMap.Map {
|
2020-04-15 10:57:42 +00:00
|
|
|
stat, err := os.Stat(path)
|
|
|
|
if err != nil {
|
2020-04-19 19:44:48 +00:00
|
|
|
/* Log file as not in cache, then delete */
|
2020-04-22 18:42:54 +00:00
|
|
|
Config.LogSystemError("Failed to stat file in cache: %s\n", path)
|
|
|
|
Config.FileCache.CacheMap.Remove(path)
|
2020-04-15 10:57:42 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
timeModified := stat.ModTime().UnixNano()
|
|
|
|
|
2020-04-19 19:44:48 +00:00
|
|
|
/* Get file pointer, no need for lock as we have write lock */
|
2020-04-22 18:42:54 +00:00
|
|
|
file := Config.FileCache.CacheMap.Get(path)
|
2020-04-15 10:57:42 +00:00
|
|
|
|
|
|
|
/* If the file is marked as fresh, but file on disk newer, mark as unfresh */
|
|
|
|
if file.IsFresh() && file.LastRefresh() < timeModified {
|
|
|
|
file.SetUnfresh()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-15 11:43:02 +00:00
|
|
|
/* Done! We can release cache read lock */
|
2020-04-22 18:42:54 +00:00
|
|
|
Config.FileCache.CacheMutex.Unlock()
|
2020-04-14 10:49:37 +00:00
|
|
|
}
|
|
|
|
|
2020-04-15 18:41:00 +00:00
|
|
|
/* TODO: see if there is more efficienct setup */
|
2020-04-15 10:57:42 +00:00
|
|
|
type FileCache struct {
|
2020-04-22 18:42:54 +00:00
|
|
|
CacheMap *FixedMap
|
|
|
|
CacheMutex sync.RWMutex
|
|
|
|
FileSizeMax int64
|
2020-04-13 12:15:12 +00:00
|
|
|
}
|
|
|
|
|
2020-04-22 18:42:54 +00:00
|
|
|
func (fc *FileCache) Init(size int, fileSizeMax float64) {
|
|
|
|
fc.CacheMap = NewFixedMap(size)
|
|
|
|
fc.CacheMutex = sync.RWMutex{}
|
|
|
|
fc.FileSizeMax = int64(BytesInMegaByte * fileSizeMax)
|
2020-04-13 12:15:12 +00:00
|
|
|
}
|
|
|
|
|
2020-04-23 19:39:31 +00:00
|
|
|
func (fc *FileCache) FetchRegular(path string, connHost ConnHost) ([]byte, *GophorError) {
|
2020-04-17 15:39:25 +00:00
|
|
|
return fc.Fetch(path, func(path string) FileContents {
|
|
|
|
contents := new(RegularFileContents)
|
|
|
|
contents.path = path
|
|
|
|
return contents
|
2020-04-23 19:39:31 +00:00
|
|
|
}, connHost)
|
2020-04-17 15:39:25 +00:00
|
|
|
}
|
|
|
|
|
2020-04-23 19:39:31 +00:00
|
|
|
func (fc *FileCache) FetchGophermap(path string, connHost ConnHost) ([]byte, *GophorError) {
|
2020-04-17 15:39:25 +00:00
|
|
|
return fc.Fetch(path, func(path string) FileContents {
|
|
|
|
contents := new(GophermapContents)
|
|
|
|
contents.path = path
|
|
|
|
return contents
|
2020-04-23 19:39:31 +00:00
|
|
|
}, connHost)
|
2020-04-17 15:39:25 +00:00
|
|
|
}
|
|
|
|
|
2020-04-23 19:39:31 +00:00
|
|
|
func (fc *FileCache) Fetch(path string, newFileContents func(string) FileContents, connHost ConnHost) ([]byte, *GophorError) {
|
2020-04-15 11:43:02 +00:00
|
|
|
/* Get cache map read lock then check if file in cache map */
|
2020-04-14 10:49:37 +00:00
|
|
|
fc.CacheMutex.RLock()
|
2020-04-19 19:44:48 +00:00
|
|
|
file := fc.CacheMap.Get(path)
|
2020-04-13 12:15:12 +00:00
|
|
|
|
2020-04-15 18:41:00 +00:00
|
|
|
/* TODO: work on efficiency */
|
2020-04-19 19:44:48 +00:00
|
|
|
if file != nil {
|
2020-04-15 11:43:02 +00:00
|
|
|
/* File in cache -- before doing anything get file read lock */
|
2020-04-19 19:44:48 +00:00
|
|
|
file.RLock()
|
2020-04-14 10:49:37 +00:00
|
|
|
|
2020-04-15 11:43:02 +00:00
|
|
|
/* Check file is marked as fresh */
|
2020-04-19 19:44:48 +00:00
|
|
|
if !file.IsFresh() {
|
2020-04-15 11:43:02 +00:00
|
|
|
/* File not fresh! Swap file read for write-lock */
|
2020-04-19 19:44:48 +00:00
|
|
|
file.RUnlock()
|
|
|
|
file.Lock()
|
2020-04-14 10:49:37 +00:00
|
|
|
|
2020-04-15 11:43:02 +00:00
|
|
|
/* Reload file contents from disk */
|
2020-04-19 19:44:48 +00:00
|
|
|
gophorErr := file.LoadContents()
|
2020-04-13 21:48:49 +00:00
|
|
|
if gophorErr != nil {
|
2020-04-14 10:49:37 +00:00
|
|
|
/* Error loading contents, unlock all mutex then return error */
|
2020-04-19 19:44:48 +00:00
|
|
|
file.Unlock()
|
2020-04-14 10:49:37 +00:00
|
|
|
fc.CacheMutex.RUnlock()
|
2020-04-13 21:48:49 +00:00
|
|
|
return nil, gophorErr
|
|
|
|
}
|
2020-04-14 10:49:37 +00:00
|
|
|
|
2020-04-15 11:43:02 +00:00
|
|
|
/* Updated! Swap back file write for read lock */
|
2020-04-19 19:44:48 +00:00
|
|
|
file.Unlock()
|
|
|
|
file.RLock()
|
2020-04-13 21:48:49 +00:00
|
|
|
}
|
|
|
|
} else {
|
2020-04-15 11:43:02 +00:00
|
|
|
/* Before we do ANYTHING, we need to check file-size on disk */
|
|
|
|
stat, err := os.Stat(path)
|
|
|
|
if err != nil {
|
2020-04-17 17:12:36 +00:00
|
|
|
/* Error stat'ing file, unlock read mutex then return error */
|
|
|
|
fc.CacheMutex.RUnlock()
|
2020-04-15 11:43:02 +00:00
|
|
|
return nil, &GophorError{ FileStatErr, err }
|
|
|
|
}
|
2020-04-13 21:48:49 +00:00
|
|
|
|
2020-04-17 15:39:25 +00:00
|
|
|
/* Create new file contents object using supplied function */
|
|
|
|
contents := newFileContents(path)
|
|
|
|
|
|
|
|
/* Create new file wrapper around contents */
|
2020-04-19 20:13:09 +00:00
|
|
|
file = NewFile(contents)
|
2020-04-13 12:15:12 +00:00
|
|
|
|
2020-04-15 11:43:02 +00:00
|
|
|
/* NOTE: file isn't in cache yet so no need to lock file write mutex
|
|
|
|
* before loading from disk
|
|
|
|
*/
|
2020-04-13 21:48:49 +00:00
|
|
|
gophorErr := file.LoadContents()
|
|
|
|
if gophorErr != nil {
|
2020-04-17 17:12:36 +00:00
|
|
|
/* Error loading contents, unlock read mutex then return error */
|
|
|
|
fc.CacheMutex.RUnlock()
|
2020-04-13 21:48:49 +00:00
|
|
|
return nil, gophorErr
|
|
|
|
}
|
|
|
|
|
2020-04-15 11:43:02 +00:00
|
|
|
/* Compare file size (in MB) to CacheFileSizeMax, if larger just get file
|
|
|
|
* contents, unlock all mutex and don't bother caching.
|
|
|
|
*/
|
2020-04-22 18:42:54 +00:00
|
|
|
if stat.Size() > fc.FileSizeMax {
|
2020-04-23 19:39:31 +00:00
|
|
|
b := file.Contents(connHost)
|
2020-04-15 11:43:02 +00:00
|
|
|
fc.CacheMutex.RUnlock()
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
2020-04-19 20:14:36 +00:00
|
|
|
/* File not in cache -- Swap cache map read for write lock. */
|
2020-04-15 11:43:02 +00:00
|
|
|
fc.CacheMutex.RUnlock()
|
|
|
|
fc.CacheMutex.Lock()
|
|
|
|
|
2020-04-19 19:44:48 +00:00
|
|
|
/* Put file in the FixedMap */
|
|
|
|
fc.CacheMap.Put(path, file)
|
2020-04-14 10:49:37 +00:00
|
|
|
|
2020-04-15 11:43:02 +00:00
|
|
|
/* Before unlocking cache mutex, lock file read for upcoming call to .Contents() */
|
|
|
|
file.RLock()
|
|
|
|
|
2020-04-14 10:49:37 +00:00
|
|
|
/* Swap cache lock back to read */
|
|
|
|
fc.CacheMutex.Unlock()
|
|
|
|
fc.CacheMutex.RLock()
|
2020-04-13 12:15:12 +00:00
|
|
|
}
|
|
|
|
|
2020-04-15 11:43:02 +00:00
|
|
|
/* Read file contents into new variable for return, then unlock file read lock */
|
2020-04-23 19:39:31 +00:00
|
|
|
b := file.Contents(connHost)
|
2020-04-19 19:44:48 +00:00
|
|
|
file.RUnlock()
|
2020-04-14 10:49:37 +00:00
|
|
|
|
2020-04-15 11:43:02 +00:00
|
|
|
/* Finally we can unlock the cache map read lock, we are done :) */
|
2020-04-14 10:49:37 +00:00
|
|
|
fc.CacheMutex.RUnlock()
|
2020-04-13 12:15:12 +00:00
|
|
|
|
2020-04-13 21:48:49 +00:00
|
|
|
return b, nil
|
2020-04-13 12:15:12 +00:00
|
|
|
}
|