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

Switch map to sync.Map to make it safe in multi-thread application? #16

Open
spacewander opened this issue Oct 28, 2022 · 8 comments
Open

Comments

@spacewander
Copy link

Currently, libcoraza uses map to store the states:

var wafMap = make(map[uint64]*coraza.WAF)

map is not thread-safe, if we want to use libcoraza in a multi-thread C/C++ application, it would be better to switch to sync.Map.

Compared with the whole operation, the extra cost from the sync.Map is acceptable. And seems the use cases in libcoraza satisfy "a given key is only ever written once but read many times" so it may be better than using extra sync.RWMutex.

More info can be found in https://pkg.go.dev/sync#Map

@potats0
Copy link
Contributor

potats0 commented Jun 26, 2023

CGO is not yet compatible with multithreaded programming, which may cause deadlock.
I've ported libcoraza to openresty successfully.

@spacewander
Copy link
Author

Nice to hear some ideas.

CGO is not yet compatible with multithreaded programming, which may cause deadlock.

Are there any more details?

I've ported libcoraza to openresty successfully.

Would you compare this with nginx + modsecurity? What's the advantage?

@potats0
Copy link
Contributor

potats0 commented Jun 26, 2023

https://www.v2ex.com/t/568117

libcoroza is easy to develop . I've developed a log4j module for detecting more evasive exploit.But porting to libmodsecurity is hard. Learning C++ is quite challenging.

@potats0
Copy link
Contributor

potats0 commented Jun 26, 2023

performance
img_v2_aca433b8-a086-4856-a3a9-dd892b5f8dfg
img_v2_72034b71-49d0-478d-93c8-feae744712ag

@spacewander
Copy link
Author

https://www.v2ex.com/t/568117

I guess it's because the Go's threads disappear after the Nginx calls fork. Loading the Go shared lib in the init_worker phase may solve this problem.

libcoroza is easy to develop . I've developed a log4j module for detecting more evasive exploit.But porting to libmodsecurity is hard. Learning C++ is quite challenging.

I am glad to hear that.

@potats0
Copy link
Contributor

potats0 commented Jun 27, 2023

sure loading the
Go shared library in init-worker phrase can resolve problem.I've completed openresty lua module. I want to merge into apisix.How should I do

@spacewander
Copy link
Author

You can open an issue there and discuss with them.

@potats0
Copy link
Contributor

potats0 commented Jun 28, 2023

image

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

No branches or pull requests

2 participants