Skip to content

Commit 33a266d

Browse files
committed
Fix attempt for too many open files
1 parent 570288d commit 33a266d

2 files changed

Lines changed: 14 additions & 5 deletions

File tree

country/country.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ func init() {
4646
if err != nil {
4747
log.Fatalf("Cannot open %s: %s", path, err)
4848
}
49+
defer f.Close()
4950
fipsIndex = make(map[string]string, 265)
5051
r := csv.NewReader(f)
5152
r.Comma = ' '

util.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"os"
1414
"runtime"
1515
"strings"
16+
"time"
1617

1718
"github.com/securityfirst/umbrella-api/models"
1819
"github.com/securityfirst/umbrella-api/utils"
@@ -130,19 +131,26 @@ func colorLog(toLog interface{}, col ...color.Attribute) {
130131
log.Printf(info(fmt.Sprintf("%s[%s:%d] %v", runtime.FuncForPC(pc).Name(), fn, line, fmt.Sprint(toLog))))
131132
}
132133

133-
func makeRequest(uri string, method string, requestBody io.Reader) (response []byte, err error) {
134+
var client = http.Client{Timeout: time.Second * 20}
135+
136+
func makeRequest(uri string, method string, requestBody io.Reader) ([]byte, error) {
134137
req, err := http.NewRequest(strings.ToUpper("GET"), uri, requestBody)
135-
client := &http.Client{}
138+
if err != nil {
139+
utils.CheckErr(err)
140+
return nil, err
141+
}
142+
req.Close = true
136143
resp, err := client.Do(req)
137144
if err != nil {
138-
return response, err
145+
utils.CheckErr(err)
146+
return nil, err
139147
}
140148
defer resp.Body.Close()
141-
response, err = ioutil.ReadAll(resp.Body)
149+
bytes, err := ioutil.ReadAll(resp.Body)
142150
if err != nil {
143151
utils.CheckErr(err)
144152
}
145-
return response, err
153+
return bytes, err
146154
}
147155

148156
func difference(slice1 []int, slice2 []int) []int {

0 commit comments

Comments
 (0)