github-vet / github-vet/rangeloop-pointer-findings

gevgev/numerxdatapusher: numerxdatapusher.go; 134 LoC

Open
#14,729 0 comments 0 reactions 0 assignees View on GitHub
fresh large
Dominant language
No language data
Stars
0
Forks
0
PR merge metrics
PR metrics pending

Description

Found a possible issue in [gevgev/numerxdatapusher](https://www.github.com/gevgev/numerxdatapusher) at [numerxdatapusher.go](https://github.com/gevgev/numerxdatapusher/blob/0c250d80afe1c9b946a996ce93100250fdc5fbb8/numerxdatapusher.go#L523-L656)

Below is the message reported by the analyzer for this snippet of code. Beware that the analyzer only reports the first issue it finds, so please do not limit your consideration to the contents of the below message.

> range-loop variable eachFile used in defer or goroutine at line 548

[Click here to see the code in its original context.](https://github.com/gevgev/numerxdatapusher/blob/0c250d80afe1c9b946a996ce93100250fdc5fbb8/numerxdatapusher.go#L523-L656)

Click here to show the 134 line(s) of Go which triggered the analyzer.

```go
for _, eachFile := range files {
// if we still have available goroutine in the pool (out of concurrency )
sem <- true

// fire one file to be processed in a goroutine
wg.Add(1)

log.Println("About to process: ", eachFile)
go func(fileName string) {
// Signal end of processing at the end
defer func() { <-sem }()

var extraParams map[string]string = make(map[string]string)

switch requestType {
case RQ_Viewership:
extraParams["timestamp"] = "event_date"
extraParams["format"] = "event_date,timestamp,regex (.*),$1 00:00:00" //"event_date,timestamp,regex%20(.*),%241%2000:00:00"
extraParams["csvHeaderLine"] = "1"

case RQ_MetaChanMap, RQ_MetaBilling, RQ_MetaProgram, RQ_MetaEventMap:
extraParams["key"] = KeyValues[requestType]
extraParams["csvHeaderLine"] = "1"
}

request, err := newfileUploadRequest(baseUrl, string(requestType), extraParams, eachFile)
if err != nil {
// Wrong parameters/request - do not try again
log.Println(err)
failedJobsChan <- JobType{
JobId: err.Error(),
Filename: eachFile,
}
return
}

if verbose {
log.Println("POST RQ URL: ", request.URL)
log.Println("POST RQ Headers: ", request.Header)
log.Println("POST RQ Body: ", request)
}

client := &http.Client{}

postRequestSucceeded := false
var resp *http.Response
retryNo := retryNumber // retryNo for http 500 Server errors re-tries
RETRY_LABEL:
for attemptNumber := 0; attemptNumber < retryNumber; attemptNumber++ {
resp, err = client.Do(request)
if err != nil { // timeout re-tries are handled here
if verbose {
log.Printf("Attempt # %d for %s failed.\n", attemptNumber+1, eachFile)
}
time.Sleep(timeout)
} else {
postRequestSucceeded = true
break
}
}

if !postRequestSucceeded {
log.Println(err)
failedJobsChan <- JobType{
JobId: time.Now().String() + ":" + err.Error(),
Filename: eachFile,
}
return
} else {

// JSON {"id" : "0.0.LqO~iOvJV3sdUOd8"}
defer resp.Body.Close()

if verbose {
log.Println("POST Status code: ", resp.StatusCode)
log.Println("POST Headers: ", resp.Header)
}
bodyContent, err := ioutil.ReadAll(resp.Body)

if verbose {
log.Printf("POST - File:[%s] Response body: %s\n", eachFile, string(bodyContent))
log.Println("POST - Status RS Content: error? :", err)
log.Println("POST - Status RS Content: body: ", bodyContent)
}
if resp.StatusCode == 200 {
// get the id of the job on numerX server
// sent this Id to the StatusChecker channel
jobId, err := GetJobId(bodyContent)
if err != nil {
log.Printf("Error [%v] for submitting %v \n", err, eachFile)
} else {
if verbose {
log.Printf("Posted file [%s] with Id {%s}, about to start checking on status update\n", eachFile, jobId)
}

newJob := JobType{
JobId: jobId,
Filename: eachFile,
}
jobsInProcessChann <- newJob
}
} else if resp.StatusCode == 500 {
// if 500 - re POST
retryNo--
if retryNo <= 0 {
failedJobsChan <- JobType{
JobId: "http " + strconv.Itoa(resp.StatusCode),
Filename: eachFile,
}
return
} else {
resp.Body.Close()
if verbose {
log.Printf("Attempt # %d for %s failed.\n", retryNumber-retryNo, eachFile)
}
time.Sleep(timeout)
goto RETRY_LABEL
}

} else { // all other HTTP response codes
log.Println("Error Status [%v] for submitting %v \n", err, string(bodyContent))
if verbose {
log.Println("Error Status [%v] for submitting %v \n", err, string(bodyContent))
}
failedJobsChan <- JobType{
JobId: "",
Filename: eachFile,
}
return
}
}

}(eachFile)
}

```

Leave a reaction on this issue to contribute to the project by classifying this instance as a **Bug** :-1:, **Mitigated** :+1:, or **Desirable Behavior** :rocket:
See the descriptions of the classifications [here](https://github.com/github-vet/rangeclosure-findings#how-can-i-help) for more information.

commit ID: 0c250d80afe1c9b946a996ce93100250fdc5fbb8

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.