1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-22 01:57:12 -05:00

correct handler running status

This commit is contained in:
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.
type Manager struct {
sync.RWMutex
access sync.RWMutex
untaggedHandler []core.InboundHandler
taggedHandlers map[string]core.InboundHandler
running bool
}
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 {
m.Lock()
defer m.Unlock()
m.access.Lock()
defer m.access.Unlock()
tag := handler.Tag()
if len(tag) > 0 {
@ -42,12 +43,17 @@ func (m *Manager) AddHandler(ctx context.Context, handler core.InboundHandler) e
} else {
m.untaggedHandler = append(m.untaggedHandler, handler)
}
if m.running {
return handler.Start()
}
return nil
}
func (m *Manager) GetHandler(ctx context.Context, tag string) (core.InboundHandler, error) {
m.RLock()
defer m.RUnlock()
m.access.RLock()
defer m.access.RUnlock()
handler, found := m.taggedHandlers[tag]
if !found {
@ -61,8 +67,8 @@ func (m *Manager) RemoveHandler(ctx context.Context, tag string) error {
return core.ErrNoClue
}
m.Lock()
defer m.Unlock()
m.access.Lock()
defer m.access.Unlock()
if handler, found := m.taggedHandlers[tag]; found {
handler.Close()
@ -74,6 +80,11 @@ func (m *Manager) RemoveHandler(ctx context.Context, tag string) error {
}
func (m *Manager) Start() error {
m.access.Lock()
defer m.access.Unlock()
m.running = true
for _, handler := range m.taggedHandlers {
if err := handler.Start(); err != nil {
return err
@ -89,6 +100,11 @@ func (m *Manager) Start() error {
}
func (m *Manager) Close() {
m.access.Lock()
defer m.access.Unlock()
m.running = false
for _, handler := range m.taggedHandlers {
handler.Close()
}

View File

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

View File

@ -2,6 +2,7 @@ package core
import (
"context"
"sync"
"v2ray.com/core/common"
"v2ray.com/core/common/uuid"
@ -30,8 +31,10 @@ type Instance struct {
clock syncClock
cmd syncCommander
access sync.Mutex
features []Feature
id uuid.UUID
running bool
}
// 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.
func (s *Instance) Close() {
s.access.Lock()
defer s.access.Unlock()
s.running = false
for _, f := range s.features {
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.
func (s *Instance) Start() error {
s.access.Lock()
defer s.access.Unlock()
s.running = true
for _, f := range s.features {
if err := f.Start(); err != nil {
return err
@ -139,7 +150,13 @@ func (s *Instance) RegisterFeature(feature interface{}, instance Feature) error
case Commander, *Commander:
s.cmd.Set(instance.(Commander))
}
s.access.Lock()
defer s.access.Unlock()
s.features = append(s.features, instance)
if s.running {
return instance.Start()
}
return nil
}