Skip to content

Commit 982a7ca

Browse files
committed
Bug fix.
1 parent d9aba2d commit 982a7ca

3 files changed

Lines changed: 18 additions & 38 deletions

File tree

pkg/geoip/geoip.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,13 @@ const (
4545
)
4646

4747
var (
48-
db *maxminddb.Reader
49-
dbMu sync.RWMutex
50-
dbOnce sync.Once // 向后兼容:保证至少初始化一次
51-
dbPath string // 当前加载的数据库路径
52-
dbFmt dbFormat // 当前数据库格式
53-
initDone bool // 标记是否已通过 Init() 显式初始化
54-
confPath string // 记录 Init 传入的路径,供 ensureInit 使用
48+
db *maxminddb.Reader
49+
dbMu sync.RWMutex
50+
dbOnce sync.Once // 向后兼容:保证至少初始化一次
51+
dbPath string // 当前加载的数据库路径
52+
dbFmt dbFormat // 当前数据库格式
53+
initDone bool // 标记是否已通过 Init() 显式初始化
54+
confPath string // 记录 Init 传入的路径,供 ensureInit 使用
5555
)
5656

5757
// Init 初始化 GeoIP 数据库
@@ -103,7 +103,7 @@ func initLocked(path string) error {
103103
}
104104
log.Printf("NG>> GeoIP: 内嵌数据库不可用(%v),需要在配置中指定 geoipdb 路径", err)
105105
initDone = true // 标记已尝试初始化,避免重复尝试
106-
return nil // 不返回 error,允许程序继续运行
106+
return nil // 不返回 error,允许程序继续运行
107107
}
108108

109109
db = reader

pkg/geoip/geoip_test.go

Lines changed: 0 additions & 24 deletions
This file was deleted.

service/rpc/server.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -819,10 +819,12 @@ func (s *ServerHandler) ReportSystemInfo(c context.Context, r *pb.Host) (*pb.Rec
819819
// 小幅度的 BootTime 变化不认为是重启,继续正常累加流量
820820
}
821821

822-
// 不要冲掉国家码
822+
// 不要冲掉国家码(需要加读锁保护)
823+
singleton.ServerLock.RLock()
823824
if singleton.ServerList[clientID].Host != nil {
824825
host.CountryCode = singleton.ServerList[clientID].Host.CountryCode
825826
}
827+
singleton.ServerLock.RUnlock()
826828

827829
// 保存完整Host信息到数据库,用于重启后恢复(在锁外序列化)
828830
hostJSON, err := utils.Json.Marshal(host)
@@ -900,13 +902,15 @@ func (s *ServerHandler) LookupGeoIP(c context.Context, r *pb.GeoIP) (*pb.GeoIP,
900902
return nil, err
901903
}
902904

903-
// 将地区码写入到 Host
904-
singleton.ServerLock.RLock()
905-
defer singleton.ServerLock.RUnlock()
906-
if singleton.ServerList[clientID].Host == nil {
905+
// 将地区码写入到 Host(使用写锁,避免与 ReportSystemInfo 竞争)
906+
singleton.ServerLock.Lock()
907+
server := singleton.ServerList[clientID]
908+
if server == nil || server.Host == nil {
909+
singleton.ServerLock.Unlock()
907910
return nil, fmt.Errorf("host not found")
908911
}
909-
singleton.ServerList[clientID].Host.CountryCode = location
912+
server.Host.CountryCode = location
913+
singleton.ServerLock.Unlock()
910914

911915
return &pb.GeoIP{Ip: ip, CountryCode: location}, nil
912916
}

0 commit comments

Comments
 (0)