-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* enable profiler Signed-off-by: David Wertenteil <[email protected]> * Update main.go * modify gh actions Signed-off-by: David Wertenteil <[email protected]> * fixed wf * ermove units * fixed main * add build wf Signed-off-by: David Wertenteil <[email protected]> * add pprof Signed-off-by: David Wertenteil <[email protected]> * remove env * start profiler on 6060 * add simple db * fixed lock * adding mutex per bucket * remove files after SBOM creation * clear after filtered sbom creation * release lock * adding logs * fixed lock * change map to pointer * add test flow * commented out otel tracing Signed-off-by: Amir Malka <[email protected]> * adding e2e test * add push * build only on main * update tests * remove pointers * fixed test file * do not remove after 2 min * fixed filepath * add demo file * update conf * fixed container stopped logs * cleanup code * cleanup * rename file handlers * update logs --------- Signed-off-by: David Wertenteil <[email protected]> Signed-off-by: Amir Malka <[email protected]> Co-authored-by: Amir Malka <[email protected]>
- Loading branch information
Showing
13 changed files
with
161 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 3 additions & 1 deletion
4
pkg/filehandler/file_handler_interface.go → pkg/filehandler/interface.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package filehandler | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"node-agent/pkg/filehandler" | ||
"sync" | ||
) | ||
|
||
const initFileListLength = 5000 | ||
|
||
type InMemoryFileHandler struct { | ||
mutex sync.RWMutex | ||
m map[string]*sync.RWMutex | ||
files map[string]map[string]bool | ||
} | ||
|
||
var _ filehandler.FileHandler = (*InMemoryFileHandler)(nil) | ||
|
||
func CreateInMemoryFileHandler() (*InMemoryFileHandler, error) { | ||
return &InMemoryFileHandler{ | ||
m: make(map[string]*sync.RWMutex), | ||
files: make(map[string]map[string]bool, 20), | ||
}, nil | ||
} | ||
|
||
func (s *InMemoryFileHandler) AddFile(ctx context.Context, bucket, file string) error { | ||
|
||
// Acquire a read lock first | ||
s.mutex.RLock() | ||
bucketLock, ok := s.m[bucket] | ||
bucketFiles, okF := s.files[bucket] | ||
s.mutex.RUnlock() | ||
|
||
// If the bucket doesn't exist, acquire a write lock to create the new bucket | ||
if !ok || !okF { | ||
s.mutex.Lock() | ||
// Double-check the bucket's existence to ensure another goroutine didn't already create it | ||
bucketLock, ok = s.m[bucket] | ||
if !ok { | ||
bucketLock = &sync.RWMutex{} | ||
s.m[bucket] = bucketLock | ||
} | ||
|
||
bucketFiles, okF = s.files[bucket] | ||
if !okF { | ||
bucketFiles = make(map[string]bool, initFileListLength) | ||
s.files[bucket] = bucketFiles | ||
} | ||
s.mutex.Unlock() | ||
} | ||
|
||
// Acquire a write lock if the bucket already exists | ||
bucketLock.Lock() | ||
defer bucketLock.Unlock() | ||
|
||
bucketFiles[file] = true | ||
|
||
return nil | ||
} | ||
|
||
func (s *InMemoryFileHandler) Close() { | ||
// Nothing to do | ||
} | ||
|
||
func shallowCopyMapStringBool(m map[string]bool) map[string]bool { | ||
if m == nil { | ||
return nil | ||
} | ||
mCopy := make(map[string]bool, len(m)) | ||
for k, v := range m { | ||
mCopy[k] = v | ||
} | ||
return mCopy | ||
} | ||
|
||
func (s *InMemoryFileHandler) GetFiles(ctx context.Context, bucket string) (map[string]bool, error) { | ||
s.mutex.RLock() | ||
bucketLock, ok := s.m[bucket] | ||
bucketFiles, okFiles := s.files[bucket] | ||
s.mutex.RUnlock() | ||
|
||
if !ok || !okFiles { | ||
return map[string]bool{}, fmt.Errorf("bucket does not exist for container %s", bucket) | ||
} | ||
|
||
bucketLock.RLock() | ||
defer bucketLock.RUnlock() | ||
|
||
return shallowCopyMapStringBool(bucketFiles), nil | ||
} | ||
func (s *InMemoryFileHandler) RemoveBucket(ctx context.Context, bucket string) error { | ||
|
||
s.mutex.Lock() | ||
bucketLock, ok := s.m[bucket] | ||
if ok { | ||
bucketLock.Lock() | ||
defer bucketLock.Unlock() | ||
} | ||
|
||
delete(s.m, bucket) | ||
delete(s.files, bucket) | ||
s.mutex.Unlock() | ||
|
||
return nil | ||
} |
Oops, something went wrong.