Skip to content

Commit

Permalink
Make whitespace more idiomatic
Browse files Browse the repository at this point in the history
Remove extranous newlines to make the code more like idiomatic Go, and
split some structs over multiple lines to make them easier to read.
  • Loading branch information
mattbostock committed Jun 18, 2018
1 parent df0b142 commit d73e296
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 80 deletions.
2 changes: 0 additions & 2 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,9 @@ type Cache interface {
}

func getCache(t *TricksterHandler) Cache {

switch t.Config.Caching.CacheType {
case ctFilesystem:
return &FilesystemCache{Config: t.Config.Caching.Filesystem, T: t}

case ctRedis:
return &RedisCache{Config: t.Config.Caching.Redis, T: t}
}
Expand Down
3 changes: 0 additions & 3 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@ func NewConfig() *Config {
}

func defaultOriginConfig() PrometheusOriginConfig {

return PrometheusOriginConfig{
OriginURL: "http://prometheus:9090/",
APIPath: "/api/v1/",
Expand All @@ -147,5 +146,3 @@ func (c *Config) LoadFile(path string) error {
_, err := toml.DecodeFile(path, &c)
return err
}

//
15 changes: 1 addition & 14 deletions filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ type FilesystemCache struct {

// Connect instantiates the FilesystemCache mutex map and starts the Expired Entry Reaper goroutine
func (c *FilesystemCache) Connect() error {

level.Info(c.T.Logger).Log("event", "filesystem cache setup", "cachePath", c.Config.CachePath)

if err := mustMakeDirectory(c.Config.CachePath); err != nil {
Expand All @@ -51,9 +50,7 @@ func (c *FilesystemCache) Connect() error {

// Store places an object in the cache using the specified key and ttl
func (c *FilesystemCache) Store(cacheKey string, data string, ttl int64) error {

expFile, dataFile := c.getFileNames(cacheKey)

expiration := []byte(strconv.FormatInt(time.Now().Unix()+ttl, 10))

level.Debug(c.T.Logger).Log("event", "filesystem cache store", "key", cacheKey, "expFile", expFile, "dataFile", dataFile)
Expand All @@ -73,7 +70,6 @@ func (c *FilesystemCache) Store(cacheKey string, data string, ttl int64) error {

// Retrieve looks for an object in cache and returns it (or an error if not found)
func (c *FilesystemCache) Retrieve(cacheKey string) (string, error) {

_, dataFile := c.getFileNames(cacheKey)
level.Debug(c.T.Logger).Log("event", "filesystem cache retrieve", "key", cacheKey, "dataFile", dataFile)

Expand All @@ -86,22 +82,17 @@ func (c *FilesystemCache) Retrieve(cacheKey string) (string, error) {
}

return string(content), nil

}

// Reap continually iterates through the cache to find expired elements and removes them
func (c *FilesystemCache) Reap() {

for {
now := time.Now().Unix()

files, err := ioutil.ReadDir(c.Config.CachePath)
if err == nil {

for _, file := range files {

if strings.HasSuffix(file.Name(), ".expiration") {

cacheKey := strings.Replace(file.Name(), ".expiration", "", 1)
expFile, dataFile := c.getFileNames(cacheKey)
mtx := c.getMutex(cacheKey)
Expand All @@ -110,7 +101,6 @@ func (c *FilesystemCache) Reap() {
if err == nil {
expiration, err := strconv.ParseInt(string(content), 10, 64)
if err != nil || expiration < now {

level.Debug(c.T.Logger).Log("event", "filesystem cache reap", "key", cacheKey, "dataFile", dataFile)

// Get a lock
Expand All @@ -128,17 +118,14 @@ func (c *FilesystemCache) Reap() {

// Unlock
c.T.ChannelCreateMtx.Unlock()

}
}
mtx.Unlock()
}

}
}

time.Sleep(time.Duration(c.T.Config.Caching.ReapSleepMS) * time.Millisecond)

}
}

Expand All @@ -153,7 +140,6 @@ func (c *FilesystemCache) getFileNames(cacheKey string) (string, string) {
}

func (c *FilesystemCache) getMutex(cacheKey string) *sync.Mutex {

var mtx *sync.Mutex
var ok bool
c.mapMutex.Lock()
Expand All @@ -177,5 +163,6 @@ func mustMakeDirectory(path string) error {
if err != nil || !writeable(path) {
return fmt.Errorf(`[%s] directory is not writeable by the trickster`, path)
}

return nil
}
5 changes: 0 additions & 5 deletions flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ func loadConfiguration(c *Config, arguments []string) error {
}

func loadEnvVars(c *Config) {

// Origin
if x := os.Getenv(evOrigin); x != "" {
c.DefaultOriginURL = x
Expand Down Expand Up @@ -95,7 +94,6 @@ func loadEnvVars(c *Config) {

// loadFlags loads configuration from command line flags.
func loadFlags(c *Config, arguments []string) {

var path string
var version bool
var origin string
Expand All @@ -119,13 +117,10 @@ func loadFlags(c *Config, arguments []string) {
if len(origin) > 0 {
c.DefaultOriginURL = origin
}

if proxyListenPort > 0 {
c.ProxyServer.ListenPort = proxyListenPort
}

if metricsListenPort > 0 {
c.Metrics.ListenPort = metricsListenPort
}

}
Loading

0 comments on commit d73e296

Please sign in to comment.