From e3610870d1e8d155629ea1d8dbc9a13c1a0181e2 Mon Sep 17 00:00:00 2001 From: XGFan Date: Sat, 11 Dec 2021 11:22:38 +0800 Subject: [PATCH] replace same tag default outbound handler (#1437) * replace old default handler with same tag Signed-off-by: Guofan Xu * add warning log when replace outbound * fix lint Signed-off-by: Guofan Xu --- app/proxyman/outbound/outbound.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/app/proxyman/outbound/outbound.go b/app/proxyman/outbound/outbound.go index 71b464e3b..d771b606d 100644 --- a/app/proxyman/outbound/outbound.go +++ b/app/proxyman/outbound/outbound.go @@ -102,13 +102,18 @@ func (m *Manager) GetHandler(tag string) outbound.Handler { func (m *Manager) AddHandler(ctx context.Context, handler outbound.Handler) error { m.access.Lock() defer m.access.Unlock() + tag := handler.Tag() - if m.defaultHandler == nil { + if m.defaultHandler == nil || + (len(tag) > 0 && tag == m.defaultHandler.Tag()) { m.defaultHandler = handler } - tag := handler.Tag() if len(tag) > 0 { + if oldHandler, found := m.taggedHandler[tag]; found { + errors.New("will replace the existed outbound with the tag: " + tag).AtWarning().WriteToLog() + _ = oldHandler.Close() + } m.taggedHandler[tag] = handler } else { m.untaggedHandlers = append(m.untaggedHandlers, handler)