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

Add tags cache to app.proxyman.ohm.Select() #2927

Merged
merged 3 commits into from Jan 12, 2024
Merged

Conversation

vrnobody
Copy link
Contributor

负载均衡从outbounds manager获取tags时,很多时候返回的tags是相同的。这个PR给ohm.Select()添加缓存,减少sort次数。

@@ -146,6 +152,11 @@ func (m *Manager) Select(selectors []string) []string {
m.access.RLock()
defer m.access.RUnlock()

key := strings.Join(selectors, ",")
if cache, ok := m.tagsCache[key]; ok {
Copy link
Member

Choose a reason for hiding this comment

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

根据之前的讨论 outbound tags 会变 所以这个 cache 应该不行
#2898 (comment)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

这里tagsCache的key是selectors他是源于config.json的routing.balancers.selector数组。用在上面那个issue里面就是["proxy"]。这个值通常不怎么变。然后ohm.Select()这个函数是根据上面这个值从taggedOutbounds里面匹配tag的前缀,所以taggedOutbounds不变的情况下,返回的tags也是相同的,只是golang的map是无序的,导致tags里面的值的顺序会发生变化,但是sort之后就不变了。

然后是issue里面讨论的通过api增删outbounds的情况。这个PR在ohm.AddHandler()和ohm.RemoveHandler()里面都加了m.tagsCache = make(map[string][]string)对cache重置,这时会重新挑选outbound tags存入cache。

用issue讨论的情况实测结果
Xray 1.8.7 (Xray, Penetrates Everything.) Custom (go1.21.6 windows/amd64)
A unified platform for anti-censorship.
2024/01/12 08:55:29 [Info] infra/conf/serial: Reading config: stdin:
2024/01/12 08:55:29 [Warning] core: Xray 1.8.7 started
2024/01/12 08:55:36 127.0.0.1:8290 accepted //browser.events.data.msn.cn:443 [http -> proxyA]
2024/01/12 08:55:36 127.0.0.1:8291 accepted //ntp.msn.cn:443 [http -> proxyB]
2024/01/12 08:55:37 127.0.0.1:8296 accepted //assets.msn.cn:443 [http -> proxyC]
2024/01/12 08:55:37 127.0.0.1:8298 accepted //edgeservices.bing.com:443 [http -> proxyA]
2024/01/12 08:55:37 127.0.0.1:8299 accepted //assets.msn.cn:443 [http -> proxyB]
2024/01/12 08:55:37 127.0.0.1:8302 accepted //browser.events.data.msn.cn:443 [http -> proxyC]
2024/01/12 08:55:37 127.0.0.1:8304 accepted //img-s-msn-com.akamaized.net:443 [http -> proxyA]
2024/01/12 08:55:37 127.0.0.1:8305 accepted //sb.scorecardresearch.com:443 [http -> proxyB]
2024/01/12 08:55:37 127.0.0.1:8306 accepted //th.bing.com:443 [http -> proxyC]
2024/01/12 08:55:37 127.0.0.1:8307 accepted //api.msn.cn:443 [http -> proxyA]
2024/01/12 08:55:37 127.0.0.1:8309 accepted //c.msn.cn:443 [http -> proxyB]
2024/01/12 08:55:38 127.0.0.1:8321 accepted //assets.msn.com:443 [http -> proxyC]
2024/01/12 08:55:39 127.0.0.1:8326 accepted //c.bing.com:443 [http -> proxyA]
2024/01/12 08:55:52 127.0.0.1:8366 accepted //edge.microsoft.com:443 [http -> proxyB]
2024/01/12 08:55:53 127.0.0.1:8370 accepted //prod-eastasia.access-point.cloudmessaging.edge.microsoft.com:443 [http -> proxyC]
2024/01/12 08:55:53 127.0.0.1:8373 accepted //edge.microsoft.com:443 [http -> proxyA]
2024/01/12 08:55:54 127.0.0.1:8375 accepted //www.bingapis.com:443 [http -> proxyB]
2024/01/12 08:55:54 127.0.0.1:8378 accepted //bzib.nelreports.net:443 [http -> proxyC]
2024/01/12 08:55:56 127.0.0.1:8385 accepted //cn.bing.com:443 [http -> proxyA]
2024/01/12 08:56:01 127.0.0.1:8393 accepted //sb.scorecardresearch.com:443 [http -> proxyB]

// 执行 xray.exe api rmo -s=127.0.0.1:10085 "proxyB"
2024/01/12 08:56:15 127.0.0.1:8416 accepted tcp:127.0.0.1:0 [api -> api]

// 执行 xray.exe api ado -s=127.0.0.1:10085 add.json
2024/01/12 08:56:27 127.0.0.1:8454 accepted tcp:127.0.0.1:0 [api -> api]

2024/01/12 08:56:35 127.0.0.1:8480 accepted //bing.com:443 [http -> proxyD]
2024/01/12 08:56:35 127.0.0.1:8479 accepted //bing.com:443 [http -> proxyE]
2024/01/12 08:56:35 127.0.0.1:8484 accepted //r.bing.com:443 [http -> proxyA]
2024/01/12 08:56:35 127.0.0.1:8483 accepted //r.bing.com:443 [http -> proxyC]
2024/01/12 08:56:36 127.0.0.1:8487 accepted //s.cn.bing.net:443 [http -> proxyD]
2024/01/12 08:56:38 127.0.0.1:8495 accepted //c.msn.cn:443 [http -> proxyE]
2024/01/12 08:56:38 127.0.0.1:8496 accepted //browser.events.data.msn.cn:443 [http -> proxyA]
2024/01/12 08:56:39 127.0.0.1:8501 accepted //login.microsoftonline.com:443 [http -> proxyC]
2024/01/12 08:56:39 127.0.0.1:8503 accepted //storage.live.com:443 [http -> proxyD]
2024/01/12 08:56:39 127.0.0.1:8504 accepted //browser.events.data.msn.cn:443 [http -> proxyE]
2024/01/12 08:56:39 127.0.0.1:8507 accepted //www2.bing.com:443 [http -> proxyA]
2024/01/12 08:56:40 127.0.0.1:8511 accepted //aadcdn.msftauth.net:443 [http -> proxyC]
2024/01/12 08:56:40 127.0.0.1:8513 accepted //login.live.com:443 [http -> proxyD]
2024/01/12 08:56:41 127.0.0.1:8519 accepted //www.msn.cn:443 [http -> proxyE]
2024/01/12 08:56:41 127.0.0.1:8522 accepted //login.live.com:443 [http -> proxyA]
2024/01/12 08:56:41 127.0.0.1:8524 accepted //assets.msn.cn:443 [http -> proxyC]
2024/01/12 08:56:44 127.0.0.1:8531 accepted //img-s.msn.cn:443 [http -> proxyD]
2024/01/12 08:56:44 127.0.0.1:8532 accepted //img-s.msn.cn:443 [http -> proxyE]
2024/01/12 08:56:44 127.0.0.1:8535 accepted //ts4.cn.mm.bing.net:443 [http -> proxyA]
2024/01/12 08:56:44 127.0.0.1:8537 accepted //ts4.cn.mm.bing.net:443 [http -> proxyC]
2024/01/12 08:56:44 127.0.0.1:8538 accepted //ts4.cn.mm.bing.net:443 [http -> proxyD]
2024/01/12 08:56:44 127.0.0.1:8539 accepted //ts4.cn.mm.bing.net:443 [http -> proxyE]
2024/01/12 08:56:44 127.0.0.1:8540 accepted //ts4.cn.mm.bing.net:443 [http -> proxyA]
2024/01/12 08:56:44 127.0.0.1:8541 accepted //ts4.cn.mm.bing.net:443 [http -> proxyC]
2024/01/12 08:56:45 127.0.0.1:8547 accepted //img-s.msn.cn:443 [http -> proxyD]
2024/01/12 08:56:45 127.0.0.1:8548 accepted //img-s.msn.cn:443 [http -> proxyE]

Copy link
Member

Choose a reason for hiding this comment

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

懂了 感谢说明!

@yuhan6665 yuhan6665 merged commit 7f7f57d into XTLS:main Jan 12, 2024
34 checks passed
@deorth-kku
Copy link
Contributor

https://github.com/vrnobody/Xray-core/blob/9151ed6c216cb9a298f1a70a001dceba1c24fabf/app/proxyman/outbound/outbound.go#L152-L172
我有个问题,这里access锁的是个RLock,但是有写入map的操作,不会出现并发写的问题吗?
实际上我这里已经遇到了
fatal error: concurrent map writes

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.

None yet

3 participants