Fixed: race condition in update config

This commit is contained in:
Dreamacro 2018-06-15 00:49:52 +08:00
parent 2897ce916a
commit a74856dbf3

View file

@ -24,6 +24,7 @@ type Tunnel struct {
proxys map[string]C.Proxy proxys map[string]C.Proxy
observable *observable.Observable observable *observable.Observable
logCh chan interface{} logCh chan interface{}
configLock *sync.RWMutex
} }
func (t *Tunnel) Add(req C.ServerAdapter) { func (t *Tunnel) Add(req C.ServerAdapter) {
@ -31,6 +32,9 @@ func (t *Tunnel) Add(req C.ServerAdapter) {
} }
func (t *Tunnel) UpdateConfig() (err error) { func (t *Tunnel) UpdateConfig() (err error) {
t.configLock.Lock()
defer t.configLock.Unlock()
cfg, err := C.GetConfig() cfg, err := C.GetConfig()
if err != nil { if err != nil {
return return
@ -113,6 +117,9 @@ func (t *Tunnel) handleConn(localConn C.ServerAdapter) {
} }
func (t *Tunnel) match(addr *C.Addr) C.Proxy { func (t *Tunnel) match(addr *C.Addr) C.Proxy {
t.configLock.RLock()
defer t.configLock.RUnlock()
for _, rule := range t.rules { for _, rule := range t.rules {
if rule.IsMatch(addr) { if rule.IsMatch(addr) {
a, ok := t.proxys[rule.Adapter()] a, ok := t.proxys[rule.Adapter()]
@ -134,6 +141,7 @@ func newTunnel() *Tunnel {
proxys: make(map[string]C.Proxy), proxys: make(map[string]C.Proxy),
observable: observable.NewObservable(logCh), observable: observable.NewObservable(logCh),
logCh: logCh, logCh: logCh,
configLock: &sync.RWMutex{},
} }
go tunnel.process() go tunnel.process()
go tunnel.subscribeLogs() go tunnel.subscribeLogs()