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

fix concurrent map write #125

Merged
merged 1 commit into from
Jul 28, 2022
Merged

fix concurrent map write #125

merged 1 commit into from
Jul 28, 2022

Conversation

lhcn
Copy link

@lhcn lhcn commented Jul 28, 2022

写了个webAPI发现并发大的时候会遇到 concurrent map writes。 可以用 sync.Map 或者sync.Mutex + map处理。 PR采用了前者,目前运行良好。

fatal error: concurrent map writes
goroutine 18613 [running]:
runtime.throw({0x77d5d8?, 0x5?})
        /tmp/go/src/runtime/panic.go:992 +0x71 fp=0xc0002b37d8 sp=0xc0002b37a8 pc=0x433411
runtime.mapassign_faststr(0xc0002dd4d0?, 0xc0002dc919?, {0xc0002dc919, 0xd})
        /tmp/go/src/runtime/map_faststr.go:212 +0x39c fp=0xc0002b3840 sp=0xc0002b37d8 pc=0x41297c
github.com/zu1k/nali/internal/db.Find(0x809408?, {0xc0002dc919, 0xd})
        /tmp/go/pkg/mod/github.com/zu1k/nali@v0.5.0/internal/db/db.go:81 +0xc9 fp=0xc0002b3898 sp=0xc0002b3840 pc=0x6e29c9
github.com/zu1k/nali/pkg/entity.ParseLine({0xc0002dc919, 0xd})
        /tmp/go/pkg/mod/github.com/zu1k/nali@v0.5.0/pkg/entity/parse.go:58 +0x39f fp=0xc0002b39f0 sp=0xc0002b3898 pc=0x6e429f
main.parseIp({0x809318, 0xc0004880e0}, 0x0?)
package main

import (
	"flag"
	"net/http"
	"os"

	"github.com/zu1k/nali/pkg/entity"
)

// https://github.com/zu1k/nali
func parseIp(w http.ResponseWriter, req *http.Request) {
	query := req.URL.Query()
	ip := query.Get("ip")
	res := entity.ParseLine(ip)
	w.Write([]byte(res.String()))
}

func main() {
	port := flag.String("p", "8004", "port")
	flag.Parse()
	cd, _ := os.Getwd()
	// fmt.Println(cd)
	// naliHome := path.Join(cd, ".nali")
	os.Setenv("NALI_HOME", cd)
	http.HandleFunc("/goapi/nali/parse", parseIp)
	http.ListenAndServe(":"+*port, nil)
	// entity.ParseLine("1.1.1.1")

}

@zu1k
Copy link
Owner

zu1k commented Jul 28, 2022

看起来没有问题,感谢。因为是做 cli 就没有考虑并发

@zu1k zu1k merged commit 3709ae8 into zu1k:master Jul 28, 2022
fcwys added a commit to fcwys/nali that referenced this pull request Oct 21, 2022
修复wry文件不完整时crash的问题

对 nali 的数据库和配置文件目录进行了规范,具体见 [文档-工作目录](https://github.com/zu1k/nali#%E5%B7%A5%E4%BD%9C%E7%9B%AE%E5%BD%95)

优先使用环境变量 NALI_HOME、NALI_CONFIG_HOME、NALI_DB_HOME 指定的目录
未指定 nali 特定环境变量的情况下默认使用 XDG 规范,配置文件目录在 $XDG_CONFIG_HOME/nali,数据库文件目录在 $XDG_DATA_HOME/nali
若未检测到 XDG 相关环境变量,将根据平台使用默认目录,具体见 XDG Base Directory 中 XDG_CONFIG_HOME 和 XDG_DATA_HOME 部分
初次运行此版本将会进行目录和数据的迁移,将 ~/.nali 下的配置文件和数据库转移到相应目录,并删除 ~/.nali 目录

支持使用环境变量指定的代理下载数据库,thanks to @jingjingxyk zu1k#126
修复了 pipe mtr 时无法获取内容和格式错乱的问题,thanks to @mzz2017 zu1k#132 , fix zu1k#12, zu1k#61, zu1k#85, zu1k#115, zu1k#123.
cache map 使用并发安全的版本,thanks to @lhcn zu1k#125
升级 Go 版本到 1.19,更新了依赖
不再支持 ip2region 旧数据库格式,目前仅支持 ip2region xdb 格式

去除了已过时的数据库下载代码
从 git 历史记录中去除了数据库文件

修复自动迁移导致生成空配置文件的bug
更新纯真IP数据库的下载地址
fcwys added a commit to fcwys/nali that referenced this pull request May 11, 2024
修复wry文件不完整时crash的问题

对 nali 的数据库和配置文件目录进行了规范,具体见 [文档-工作目录](https://github.com/zu1k/nali#%E5%B7%A5%E4%BD%9C%E7%9B%AE%E5%BD%95)

优先使用环境变量 NALI_HOME、NALI_CONFIG_HOME、NALI_DB_HOME 指定的目录
未指定 nali 特定环境变量的情况下默认使用 XDG 规范,配置文件目录在 $XDG_CONFIG_HOME/nali,数据库文件目录在 $XDG_DATA_HOME/nali
若未检测到 XDG 相关环境变量,将根据平台使用默认目录,具体见 XDG Base Directory 中 XDG_CONFIG_HOME 和 XDG_DATA_HOME 部分
初次运行此版本将会进行目录和数据的迁移,将 ~/.nali 下的配置文件和数据库转移到相应目录,并删除 ~/.nali 目录

支持使用环境变量指定的代理下载数据库,thanks to @jingjingxyk zu1k#126
修复了 pipe mtr 时无法获取内容和格式错乱的问题,thanks to @mzz2017 zu1k#132 , fix zu1k#12, zu1k#61, zu1k#85, zu1k#115, zu1k#123.
cache map 使用并发安全的版本,thanks to @lhcn zu1k#125
升级 Go 版本到 1.19,更新了依赖
不再支持 ip2region 旧数据库格式,目前仅支持 ip2region xdb 格式

去除了已过时的数据库下载代码
从 git 历史记录中去除了数据库文件

修复自动迁移导致生成空配置文件的bug
更新纯真IP数据库的下载地址
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