mirror of
https://github.com/yusing/godoxy.git
synced 2026-04-25 02:09:01 +02:00
fixed loadbalancer with idlewatcher, fixed reload issue
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
package provider
|
||||
|
||||
import (
|
||||
"github.com/yusing/go-proxy/internal/common"
|
||||
E "github.com/yusing/go-proxy/internal/error"
|
||||
"github.com/yusing/go-proxy/internal/proxy/entry"
|
||||
"github.com/yusing/go-proxy/internal/route"
|
||||
"github.com/yusing/go-proxy/internal/task"
|
||||
"github.com/yusing/go-proxy/internal/watcher"
|
||||
@@ -32,31 +34,52 @@ func (handler *EventHandler) Handle(parent task.Task, events []watcher.Event) {
|
||||
return
|
||||
}
|
||||
|
||||
oldRoutes.RangeAll(func(k string, v *route.Route) {
|
||||
if !newRoutes.Has(k) {
|
||||
handler.Remove(v)
|
||||
if common.IsDebug {
|
||||
eventsLog := E.NewBuilder("events")
|
||||
for _, event := range events {
|
||||
eventsLog.Addf("event %s, actor: name=%s, id=%s", event.Action, event.ActorName, event.ActorID)
|
||||
}
|
||||
handler.provider.l.Debug(eventsLog.String())
|
||||
oldRoutesLog := E.NewBuilder("old routes")
|
||||
oldRoutes.RangeAll(func(k string, r *route.Route) {
|
||||
oldRoutesLog.Addf(k)
|
||||
})
|
||||
handler.provider.l.Debug(oldRoutesLog.String())
|
||||
newRoutesLog := E.NewBuilder("new routes")
|
||||
newRoutes.RangeAll(func(k string, r *route.Route) {
|
||||
newRoutesLog.Addf(k)
|
||||
})
|
||||
handler.provider.l.Debug(newRoutesLog.String())
|
||||
}
|
||||
|
||||
oldRoutes.RangeAll(func(k string, oldr *route.Route) {
|
||||
newr, ok := newRoutes.Load(k)
|
||||
if !ok {
|
||||
handler.Remove(oldr)
|
||||
} else if handler.matchAny(events, newr) {
|
||||
handler.Update(parent, oldr, newr)
|
||||
} else if entry.ShouldNotServe(newr) {
|
||||
handler.Remove(oldr)
|
||||
}
|
||||
})
|
||||
newRoutes.RangeAll(func(k string, newr *route.Route) {
|
||||
if oldRoutes.Has(k) {
|
||||
for _, ev := range events {
|
||||
if handler.match(ev, newr) {
|
||||
old, ok := oldRoutes.Load(k)
|
||||
if !ok { // should not happen
|
||||
panic("race condition")
|
||||
}
|
||||
handler.Update(parent, old, newr)
|
||||
return
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if !(oldRoutes.Has(k) || entry.ShouldNotServe(newr)) {
|
||||
handler.Add(parent, newr)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func (handler *EventHandler) matchAny(events []watcher.Event, route *route.Route) bool {
|
||||
for _, event := range events {
|
||||
if handler.match(event, route) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (handler *EventHandler) match(event watcher.Event, route *route.Route) bool {
|
||||
switch handler.provider.t {
|
||||
switch handler.provider.GetType() {
|
||||
case ProviderTypeDocker:
|
||||
return route.Entry.Container.ContainerID == event.ActorID ||
|
||||
route.Entry.Container.ContainerName == event.ActorName
|
||||
@@ -70,14 +93,15 @@ func (handler *EventHandler) match(event watcher.Event, route *route.Route) bool
|
||||
func (handler *EventHandler) Add(parent task.Task, route *route.Route) {
|
||||
err := handler.provider.startRoute(parent, route)
|
||||
if err != nil {
|
||||
handler.errs.Add(err)
|
||||
handler.errs.Add(E.FailWith("add "+route.Entry.Alias, err))
|
||||
} else {
|
||||
handler.added = append(handler.added, route.Entry.Alias)
|
||||
}
|
||||
}
|
||||
|
||||
func (handler *EventHandler) Remove(route *route.Route) {
|
||||
route.Finish("route removal")
|
||||
route.Finish("route removed")
|
||||
handler.provider.routes.Delete(route.Entry.Alias)
|
||||
handler.removed = append(handler.removed, route.Entry.Alias)
|
||||
}
|
||||
|
||||
@@ -85,7 +109,7 @@ func (handler *EventHandler) Update(parent task.Task, oldRoute *route.Route, new
|
||||
oldRoute.Finish("route update")
|
||||
err := handler.provider.startRoute(parent, newRoute)
|
||||
if err != nil {
|
||||
handler.errs.Add(err)
|
||||
handler.errs.Add(E.FailWith("update "+newRoute.Entry.Alias, err))
|
||||
} else {
|
||||
handler.updated = append(handler.updated, newRoute.Entry.Alias)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user