mirror of
https://github.com/Buriburizaem0n/nezha_domains.git
synced 2026-02-05 05:00:05 +00:00
refactor: simplify server & service manipulation (#993)
* refactor: simplify server & service manipulation * update * fix * update for nat, ddns & notification * chore * update cron * update dependencies * use of function iterators * update default dns servers
This commit is contained in:
@@ -74,33 +74,37 @@ func getRealIp(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo,
|
||||
return handler(ctx, req)
|
||||
}
|
||||
|
||||
func DispatchTask(serviceSentinelDispatchBus <-chan model.Service) {
|
||||
func DispatchTask(serviceSentinelDispatchBus <-chan *model.Service) {
|
||||
workedServerIndex := 0
|
||||
list := singleton.ServerShared.GetSortedList()
|
||||
for task := range serviceSentinelDispatchBus {
|
||||
if task == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
round := 0
|
||||
endIndex := workedServerIndex
|
||||
singleton.SortedServerLock.RLock()
|
||||
// 如果已经轮了一整圈又轮到自己,没有合适机器去请求,跳出循环
|
||||
for round < 1 || workedServerIndex < endIndex {
|
||||
// 如果到了圈尾,再回到圈头,圈数加一,游标重置
|
||||
if workedServerIndex >= len(singleton.SortedServerList) {
|
||||
if workedServerIndex >= len(list) {
|
||||
workedServerIndex = 0
|
||||
round++
|
||||
continue
|
||||
}
|
||||
// 如果服务器不在线,跳过这个服务器
|
||||
if singleton.SortedServerList[workedServerIndex].TaskStream == nil {
|
||||
if list[workedServerIndex].TaskStream == nil {
|
||||
workedServerIndex++
|
||||
continue
|
||||
}
|
||||
// 如果此任务不可使用此服务器请求,跳过这个服务器(有些 IPv6 only 开了 NAT64 的机器请求 IPv4 总会出问题)
|
||||
if (task.Cover == model.ServiceCoverAll && task.SkipServers[singleton.SortedServerList[workedServerIndex].ID]) ||
|
||||
(task.Cover == model.ServiceCoverIgnoreAll && !task.SkipServers[singleton.SortedServerList[workedServerIndex].ID]) {
|
||||
if (task.Cover == model.ServiceCoverAll && task.SkipServers[list[workedServerIndex].ID]) ||
|
||||
(task.Cover == model.ServiceCoverIgnoreAll && !task.SkipServers[list[workedServerIndex].ID]) {
|
||||
workedServerIndex++
|
||||
continue
|
||||
}
|
||||
if task.Cover == model.ServiceCoverIgnoreAll && task.SkipServers[singleton.SortedServerList[workedServerIndex].ID] {
|
||||
server := singleton.SortedServerList[workedServerIndex]
|
||||
if task.Cover == model.ServiceCoverIgnoreAll && task.SkipServers[list[workedServerIndex].ID] {
|
||||
server := list[workedServerIndex]
|
||||
singleton.UserLock.RLock()
|
||||
var role uint8
|
||||
if u, ok := singleton.UserInfoMap[server.UserID]; !ok {
|
||||
@@ -110,13 +114,13 @@ func DispatchTask(serviceSentinelDispatchBus <-chan model.Service) {
|
||||
}
|
||||
singleton.UserLock.RUnlock()
|
||||
if task.UserID == server.UserID || role == model.RoleAdmin {
|
||||
singleton.SortedServerList[workedServerIndex].TaskStream.Send(task.PB())
|
||||
list[workedServerIndex].TaskStream.Send(task.PB())
|
||||
}
|
||||
workedServerIndex++
|
||||
continue
|
||||
}
|
||||
if task.Cover == model.ServiceCoverAll && !task.SkipServers[singleton.SortedServerList[workedServerIndex].ID] {
|
||||
server := singleton.SortedServerList[workedServerIndex]
|
||||
if task.Cover == model.ServiceCoverAll && !task.SkipServers[list[workedServerIndex].ID] {
|
||||
server := list[workedServerIndex]
|
||||
singleton.UserLock.RLock()
|
||||
var role uint8
|
||||
if u, ok := singleton.UserInfoMap[server.UserID]; !ok {
|
||||
@@ -126,33 +130,29 @@ func DispatchTask(serviceSentinelDispatchBus <-chan model.Service) {
|
||||
}
|
||||
singleton.UserLock.RUnlock()
|
||||
if task.UserID == server.UserID || role == model.RoleAdmin {
|
||||
singleton.SortedServerList[workedServerIndex].TaskStream.Send(task.PB())
|
||||
list[workedServerIndex].TaskStream.Send(task.PB())
|
||||
}
|
||||
workedServerIndex++
|
||||
continue
|
||||
}
|
||||
}
|
||||
singleton.SortedServerLock.RUnlock()
|
||||
}
|
||||
}
|
||||
|
||||
func DispatchKeepalive() {
|
||||
singleton.Cron.AddFunc("@every 20s", func() {
|
||||
singleton.SortedServerLock.RLock()
|
||||
defer singleton.SortedServerLock.RUnlock()
|
||||
for i := 0; i < len(singleton.SortedServerList); i++ {
|
||||
if singleton.SortedServerList[i] == nil || singleton.SortedServerList[i].TaskStream == nil {
|
||||
singleton.CronShared.AddFunc("@every 20s", func() {
|
||||
list := singleton.ServerShared.GetSortedList()
|
||||
for _, s := range list {
|
||||
if s == nil || s.TaskStream == nil {
|
||||
continue
|
||||
}
|
||||
singleton.SortedServerList[i].TaskStream.Send(&proto.Task{Type: model.TaskTypeKeepalive})
|
||||
s.TaskStream.Send(&proto.Task{Type: model.TaskTypeKeepalive})
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func ServeNAT(w http.ResponseWriter, r *http.Request, natConfig *model.NAT) {
|
||||
singleton.ServerLock.RLock()
|
||||
server := singleton.ServerList[natConfig.ServerID]
|
||||
singleton.ServerLock.RUnlock()
|
||||
server, _ := singleton.ServerShared.Get(natConfig.ServerID)
|
||||
if server == nil || server.TaskStream == nil {
|
||||
w.WriteHeader(http.StatusServiceUnavailable)
|
||||
w.Write([]byte("server not found or not connected"))
|
||||
|
||||
Reference in New Issue
Block a user