correct handler running status

pull/861/head
Darien Raymond 2018-02-07 12:34:15 +01:00
parent deaee9fa65
commit 8b5fe1a13b
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
3 changed files with 53 additions and 18 deletions

View File

@ -13,9 +13,10 @@ import (
// Manager is to manage all inbound handlers. // Manager is to manage all inbound handlers.
type Manager struct { type Manager struct {
sync.RWMutex access sync.RWMutex
untaggedHandler []core.InboundHandler untaggedHandler []core.InboundHandler
taggedHandlers map[string]core.InboundHandler taggedHandlers map[string]core.InboundHandler
running bool
} }
func New(ctx context.Context, config *proxyman.InboundConfig) (*Manager, error) { func New(ctx context.Context, config *proxyman.InboundConfig) (*Manager, error) {
@ -33,8 +34,8 @@ func New(ctx context.Context, config *proxyman.InboundConfig) (*Manager, error)
} }
func (m *Manager) AddHandler(ctx context.Context, handler core.InboundHandler) error { func (m *Manager) AddHandler(ctx context.Context, handler core.InboundHandler) error {
m.Lock() m.access.Lock()
defer m.Unlock() defer m.access.Unlock()
tag := handler.Tag() tag := handler.Tag()
if len(tag) > 0 { if len(tag) > 0 {
@ -42,12 +43,17 @@ func (m *Manager) AddHandler(ctx context.Context, handler core.InboundHandler) e
} else { } else {
m.untaggedHandler = append(m.untaggedHandler, handler) m.untaggedHandler = append(m.untaggedHandler, handler)
} }
if m.running {
return handler.Start()
}
return nil return nil
} }
func (m *Manager) GetHandler(ctx context.Context, tag string) (core.InboundHandler, error) { func (m *Manager) GetHandler(ctx context.Context, tag string) (core.InboundHandler, error) {
m.RLock() m.access.RLock()
defer m.RUnlock() defer m.access.RUnlock()
handler, found := m.taggedHandlers[tag] handler, found := m.taggedHandlers[tag]
if !found { if !found {
@ -61,8 +67,8 @@ func (m *Manager) RemoveHandler(ctx context.Context, tag string) error {
return core.ErrNoClue return core.ErrNoClue
} }
m.Lock() m.access.Lock()
defer m.Unlock() defer m.access.Unlock()
if handler, found := m.taggedHandlers[tag]; found { if handler, found := m.taggedHandlers[tag]; found {
handler.Close() handler.Close()
@ -74,6 +80,11 @@ func (m *Manager) RemoveHandler(ctx context.Context, tag string) error {
} }
func (m *Manager) Start() error { func (m *Manager) Start() error {
m.access.Lock()
defer m.access.Unlock()
m.running = true
for _, handler := range m.taggedHandlers { for _, handler := range m.taggedHandlers {
if err := handler.Start(); err != nil { if err := handler.Start(); err != nil {
return err return err
@ -89,6 +100,11 @@ func (m *Manager) Start() error {
} }
func (m *Manager) Close() { func (m *Manager) Close() {
m.access.Lock()
defer m.access.Unlock()
m.running = false
for _, handler := range m.taggedHandlers { for _, handler := range m.taggedHandlers {
handler.Close() handler.Close()
} }

View File

@ -13,10 +13,11 @@ import (
// Manager is to manage all outbound handlers. // Manager is to manage all outbound handlers.
type Manager struct { type Manager struct {
sync.RWMutex access sync.RWMutex
defaultHandler core.OutboundHandler defaultHandler core.OutboundHandler
taggedHandler map[string]core.OutboundHandler taggedHandler map[string]core.OutboundHandler
untaggedHandlers []core.OutboundHandler untaggedHandlers []core.OutboundHandler
running bool
} }
// New creates a new Manager. // New creates a new Manager.
@ -34,15 +35,16 @@ func New(ctx context.Context, config *proxyman.OutboundConfig) (*Manager, error)
return m, nil return m, nil
} }
// Start implements Application.Start // Start implements core.Feature
func (*Manager) Start() error { return nil } func (*Manager) Start() error { return nil }
// Close implements Application.Close // Close implements core.Feature
func (*Manager) Close() {} func (*Manager) Close() {}
func (m *Manager) GetDefaultHandler() core.OutboundHandler { func (m *Manager) GetDefaultHandler() core.OutboundHandler {
m.RLock() m.access.RLock()
defer m.RUnlock() defer m.access.RUnlock()
if m.defaultHandler == nil { if m.defaultHandler == nil {
return nil return nil
} }
@ -50,8 +52,8 @@ func (m *Manager) GetDefaultHandler() core.OutboundHandler {
} }
func (m *Manager) GetHandler(tag string) core.OutboundHandler { func (m *Manager) GetHandler(tag string) core.OutboundHandler {
m.RLock() m.access.RLock()
defer m.RUnlock() defer m.access.RUnlock()
if handler, found := m.taggedHandler[tag]; found { if handler, found := m.taggedHandler[tag]; found {
return handler return handler
} }
@ -59,8 +61,8 @@ func (m *Manager) GetHandler(tag string) core.OutboundHandler {
} }
func (m *Manager) AddHandler(ctx context.Context, handler core.OutboundHandler) error { func (m *Manager) AddHandler(ctx context.Context, handler core.OutboundHandler) error {
m.Lock() m.access.Lock()
defer m.Unlock() defer m.access.Unlock()
if m.defaultHandler == nil { if m.defaultHandler == nil {
m.defaultHandler = handler m.defaultHandler = handler
@ -80,8 +82,8 @@ func (m *Manager) RemoveHandler(ctx context.Context, tag string) error {
if len(tag) == 0 { if len(tag) == 0 {
return core.ErrNoClue return core.ErrNoClue
} }
m.Lock() m.access.Lock()
defer m.Unlock() defer m.access.Unlock()
delete(m.taggedHandler, tag) delete(m.taggedHandler, tag)
if m.defaultHandler.Tag() == tag { if m.defaultHandler.Tag() == tag {

View File

@ -2,6 +2,7 @@ package core
import ( import (
"context" "context"
"sync"
"v2ray.com/core/common" "v2ray.com/core/common"
"v2ray.com/core/common/uuid" "v2ray.com/core/common/uuid"
@ -30,8 +31,10 @@ type Instance struct {
clock syncClock clock syncClock
cmd syncCommander cmd syncCommander
access sync.Mutex
features []Feature features []Feature
id uuid.UUID id uuid.UUID
running bool
} }
// New returns a new V2Ray instance based on given configuration. // New returns a new V2Ray instance based on given configuration.
@ -99,6 +102,10 @@ func (s *Instance) ID() uuid.UUID {
// Close shutdown the V2Ray instance. // Close shutdown the V2Ray instance.
func (s *Instance) Close() { func (s *Instance) Close() {
s.access.Lock()
defer s.access.Unlock()
s.running = false
for _, f := range s.features { for _, f := range s.features {
f.Close() f.Close()
} }
@ -106,6 +113,10 @@ func (s *Instance) Close() {
// Start starts the V2Ray instance, including all registered features. When Start returns error, the state of the instance is unknown. // Start starts the V2Ray instance, including all registered features. When Start returns error, the state of the instance is unknown.
func (s *Instance) Start() error { func (s *Instance) Start() error {
s.access.Lock()
defer s.access.Unlock()
s.running = true
for _, f := range s.features { for _, f := range s.features {
if err := f.Start(); err != nil { if err := f.Start(); err != nil {
return err return err
@ -139,7 +150,13 @@ func (s *Instance) RegisterFeature(feature interface{}, instance Feature) error
case Commander, *Commander: case Commander, *Commander:
s.cmd.Set(instance.(Commander)) s.cmd.Set(instance.(Commander))
} }
s.access.Lock()
defer s.access.Unlock()
s.features = append(s.features, instance) s.features = append(s.features, instance)
if s.running {
return instance.Start()
}
return nil return nil
} }