Skip to content

Commit

Permalink
fix: verifying parse errors
Browse files Browse the repository at this point in the history
  • Loading branch information
WendelHime committed Nov 22, 2024
1 parent f13e7b0 commit 41e7b01
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -743,10 +743,9 @@ func (client *Client) initDialers(proxies map[string]*commonconfig.ProxyConfig)
}
},
SaveBanditRewards: func(metrics map[string]dialer.BanditMetrics) {
// check if file exists
dir := filepath.Join(configDir, "bandit")
if err := os.MkdirAll(dir, 0644); err != nil {
log.Errorf("Unable to create bandit directory: %v", err)
log.Errorf("unable to create bandit directory: %v", err)
return
}
file := filepath.Join(dir, "rewards.csv")
Expand All @@ -756,7 +755,7 @@ func (client *Client) initDialers(proxies map[string]*commonconfig.ProxyConfig)
csv.WriteString(fmt.Sprintf("%s,%f,%d\n", dialerName, metric.Reward, metric.Count))
}
if err := os.WriteFile(file, []byte(csv.String()), 0644); err != nil {
log.Errorf("Unable to write bandit rewards to file: %v", err)
log.Errorf("unable to write bandit rewards to file: %v", err)
}
},
LoadLastBanditRewards: func() map[string]dialer.BanditMetrics {
Expand All @@ -767,7 +766,7 @@ func (client *Client) initDialers(proxies map[string]*commonconfig.ProxyConfig)
}
data, err := os.ReadFile(file)
if err != nil {
log.Errorf("Unable to read bandit rewards from file: %v", err)
log.Errorf("unable to read bandit rewards from file: %v", err)
return nil
}
lines := strings.Split(string(data), "\n")
Expand All @@ -780,8 +779,16 @@ func (client *Client) initDialers(proxies map[string]*commonconfig.ProxyConfig)
if len(parts) != 3 {
continue
}
reward, _ := strconv.ParseFloat(parts[1], 64)
count, _ := strconv.Atoi(parts[2])
reward, err := strconv.ParseFloat(parts[1], 64)
if err != nil {
log.Errorf("unable to parse reward from %s: %v", parts[1], err)
continue
}
count, err := strconv.Atoi(parts[2])
if err != nil {
log.Errorf("unable to parse count from %s: %v", parts[2], err)
continue
}
metrics[parts[0]] = dialer.BanditMetrics{
Reward: reward,
Count: count,
Expand Down

0 comments on commit 41e7b01

Please sign in to comment.