@@ -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