Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spawning multiple goroutine for Notification and Sharing http client #1504

Merged
merged 11 commits into from
Sep 6, 2023

Conversation

saurabh2253
Copy link
Contributor

This fixes the case when there are all integrations configured and few were not getting processed.

Fixes #
https://github.com/deepfence/enterprise-roadmap/issues/1943

Changes proposed in this pull request:

  • Add timeout to http requests
  • Spawn a goroutine for each integration
  • Share http client across all goroutines.

…multiple goroutine for Notification and Sharing http client
Copy link
Contributor

@gnmahanth gnmahanth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Comment on lines +33 to +36
wg := new(sync.WaitGroup)
wg.Add(len(integrations))
for _, integrationRow := range integrations {
switch integrationRow.Resource {
case utils.ScanTypeDetectedNode[utils.NEO4J_VULNERABILITY_SCAN]:
processIntegration[model.Vulnerability](msg, integrationRow)
case utils.ScanTypeDetectedNode[utils.NEO4J_SECRET_SCAN]:
processIntegration[model.Secret](msg, integrationRow)
case utils.ScanTypeDetectedNode[utils.NEO4J_MALWARE_SCAN]:
processIntegration[model.Malware](msg, integrationRow)
case utils.ScanTypeDetectedNode[utils.NEO4J_COMPLIANCE_SCAN]:
processIntegration[model.Compliance](msg, integrationRow)
// cloud compliance scans
integrationRow.Resource = utils.ScanTypeDetectedNode[utils.NEO4J_CLOUD_COMPLIANCE_SCAN]
processIntegration[model.CloudCompliance](msg, integrationRow)
}
go processIntegrationRow(wg, integrationRow, msg)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure we need to do this at all.
What is this solving?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead if len(integrations) number of go routines we can start limited number of them like 5

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would go one step further and limit to 1, removing go routine entirely ;)
Unless we have a good reason, here it feels we are making the code more complex for little benefit, we are actually stressing the DB access & memory usage more than necessary.
Instead we should optimize the sending of HTTP requests (batch + go routines)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok that should minimize frequent neo4j access

@noboruma
Copy link
Collaborator

noboruma commented Sep 6, 2023

I have found a couple of issues (memory leak, broken batching) in the current code base.
Let's merge this in and revamp the code to make it more generic & fix those issues

Comment on lines 45 to 55
// Send out bytes in buffer immediately if the limit exceeded after adding this event
if buffer.Len()+len(jsonBytes) > MaxContentLength {
s.sendRequest(buffer)
fmt.Println("Splunk sent buffer:")
fmt.Println(buffer.String())
buffer.Reset()
}

// Check the response status code
if resp.StatusCode != http.StatusOK {
fmt.Println("Failed to send data to Splunk", resp.Status)
continue
}
resp.Body.Close()
}
if buffer.Len() > 0 {
s.sendRequest(buffer)
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic here is hard to follow - we should have only one sendRequest call.
Also, we are losing the jsonBytes data entirely. I suggest we use this generic batcher I am adding:
#1516

@saurabh2253 saurabh2253 merged commit 255e802 into main Sep 6, 2023
@saurabh2253 saurabh2253 deleted the integration branch September 6, 2023 11:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants