Add mutex lock to cache map. Closes #78

pull/94/head
Miguel Mota 4 years ago
parent 0f7c22be6a
commit 11d6ec1878

@ -3,6 +3,7 @@ package cointop
import ( import (
"fmt" "fmt"
"strconv" "strconv"
"sync"
fcolor "github.com/fatih/color" fcolor "github.com/fatih/color"
gocui "github.com/miguelmota/gocui" gocui "github.com/miguelmota/gocui"
@ -22,8 +23,9 @@ type colorCache map[string]ISprintf
// Colorscheme is the struct for colorscheme // Colorscheme is the struct for colorscheme
type Colorscheme struct { type Colorscheme struct {
colors colorschemeColors colors colorschemeColors
cache colorCache cache colorCache
cacheMutex sync.RWMutex
} }
var fgcolorschemeColorsMap = map[string]fcolor.Attribute{ var fgcolorschemeColorsMap = map[string]fcolor.Attribute{
@ -62,8 +64,9 @@ var gocuiColorschemeColorsMap = map[string]gocui.Attribute{
// NewColorscheme ... // NewColorscheme ...
func NewColorscheme(colors colorschemeColors) *Colorscheme { func NewColorscheme(colors colorschemeColors) *Colorscheme {
return &Colorscheme{ return &Colorscheme{
colors: colors, colors: colors,
cache: make(colorCache), cache: make(colorCache),
cacheMutex: sync.RWMutex{},
} }
} }
@ -262,7 +265,9 @@ func (c *Colorscheme) toSprintf(name string) ISprintf {
} }
} }
c.cacheMutex.Lock()
c.cache[name] = fcolor.New(attrs...).SprintFunc() c.cache[name] = fcolor.New(attrs...).SprintFunc()
c.cacheMutex.Unlock()
return c.cache[name] return c.cache[name]
} }

@ -20,7 +20,7 @@ import (
var fileperm = os.FileMode(0644) var fileperm = os.FileMode(0644)
// ErrInvalidPriceAlert is error for invalid price alert value // ErrInvalidPriceAlert is error for invalid price alert value
var ErrInvalidPriceAlert = errors.New("Invalid price alert value") var ErrInvalidPriceAlert = errors.New("invalid price alert value")
// NOTE: this is to support previous default config filepaths // NOTE: this is to support previous default config filepaths
var possibleConfigPaths = []string{ var possibleConfigPaths = []string{
@ -326,7 +326,7 @@ func (ct *Cointop) loadTableColumnsFromConfig() error {
for _, ifc := range ifcs { for _, ifc := range ifcs {
if v, ok := ifc.(string); ok { if v, ok := ifc.(string); ok {
if !ct.ValidCoinsTableHeader(v) { if !ct.ValidCoinsTableHeader(v) {
return fmt.Errorf("Invalid table header name %q. Valid names are: %s", v, strings.Join(DefaultCoinTableHeaders, ",")) return fmt.Errorf("invalid table header name %q. Valid names are: %s", v, strings.Join(DefaultCoinTableHeaders, ","))
} }
columns = append(columns, v) columns = append(columns, v)
} }
@ -453,7 +453,7 @@ func (ct *Cointop) getColorschemeColors() (map[string]interface{}, error) {
return colors, nil return colors, nil
} }
return nil, fmt.Errorf("The colorscheme file %q was not found.\n%s", path, ColorschemeHelpString()) return nil, fmt.Errorf("the colorscheme file %q was not found.\n%s", path, ColorschemeHelpString())
} }
if _, err := toml.DecodeFile(path, &colors); err != nil { if _, err := toml.DecodeFile(path, &colors); err != nil {
@ -509,7 +509,7 @@ func (ct *Cointop) loadPortfolioFromConfig() error {
for _, ifc := range ifcs { for _, ifc := range ifcs {
if v, ok := ifc.(string); ok { if v, ok := ifc.(string); ok {
if !ct.ValidPortfolioTableHeader(v) { if !ct.ValidPortfolioTableHeader(v) {
return fmt.Errorf("Invalid table header name %q. Valid names are: %s", v, strings.Join(DefaultPortfolioTableHeaders, ",")) return fmt.Errorf("invalid table header name %q. Valid names are: %s", v, strings.Join(DefaultPortfolioTableHeaders, ","))
} }
columns = append(columns, v) columns = append(columns, v)
} }
@ -562,29 +562,6 @@ func (ct *Cointop) loadPortfolioFromConfig() error {
return nil return nil
} }
// InterfaceToFloat64 attempts to convert interface to float64
func (ct *Cointop) InterfaceToFloat64(value interface{}) (float64, error) {
var num float64
var err error
switch v := value.(type) {
case string:
num, err = strconv.ParseFloat(v, 64)
if err != nil {
return 0, err
}
case int:
num = float64(v)
case int32:
num = float64(v)
case int64:
num = float64(v)
case float64:
num = v
}
return num, nil
}
// LoadPriceAlertsFromConfig loads price alerts from config file to struct // LoadPriceAlertsFromConfig loads price alerts from config file to struct
func (ct *Cointop) loadPriceAlertsFromConfig() error { func (ct *Cointop) loadPriceAlertsFromConfig() error {
ct.debuglog("loadPriceAlertsFromConfig()") ct.debuglog("loadPriceAlertsFromConfig()")
@ -644,3 +621,26 @@ func (ct *Cointop) loadPriceAlertsFromConfig() error {
return nil return nil
} }
// InterfaceToFloat64 attempts to convert interface to float64
func (ct *Cointop) InterfaceToFloat64(value interface{}) (float64, error) {
var num float64
var err error
switch v := value.(type) {
case string:
num, err = strconv.ParseFloat(v, 64)
if err != nil {
return 0, err
}
case int:
num = float64(v)
case int32:
num = float64(v)
case int64:
num = float64(v)
case float64:
num = v
}
return num, nil
}

Loading…
Cancel
Save