Few fixes to terms

This commit is contained in:
Jonathan Leibiusky @xetorthio
2017-09-08 12:20:29 -03:00
parent 45137438db
commit f5c337e03d

View File

@@ -53,7 +53,7 @@ type info struct {
type manager struct { type manager struct {
sendCh chan info sendCh chan info
receiveCh chan info receiveCh chan info
terminals map[string]terminal terminals map[string]*terminal
errorCh chan *types.Instance errorCh chan *types.Instance
instances map[string]*types.Instance instances map[string]*types.Instance
sync.Mutex sync.Mutex
@@ -72,17 +72,45 @@ func (m *manager) connect(instance *types.Instance) error {
return nil return nil
} }
return m.connectTerminal(instance)
}
func (m *manager) connectTerminal(instance *types.Instance) error {
m.Lock()
defer m.Unlock()
conn, err := core.InstanceGetTerminal(instance) conn, err := core.InstanceGetTerminal(instance)
if err != nil { if err != nil {
return err return err
} }
chw := make(chan []byte, 10) chw := make(chan []byte, 10)
t := terminal{conn: conn, write: chw, instance: instance} t := terminal{conn: conn, write: chw, instance: instance}
m.terminals[instance.Name] = t m.terminals[instance.Name] = &t
t.Go(m.receiveCh, m.errorCh) t.Go(m.receiveCh, m.errorCh)
return nil return nil
} }
func (m *manager) disconnectTerminal(instance *types.Instance) {
m.Lock()
defer m.Unlock()
t := m.terminals[instance.Name]
if t != nil {
if t.write != nil {
close(t.write)
}
if t.conn != nil {
t.conn.Close()
}
delete(m.terminals, instance.Name)
}
}
func (m *manager) getTerminal(instanceName string) *terminal {
return m.terminals[instanceName]
}
func (m *manager) trackInstance(instance *types.Instance) { func (m *manager) trackInstance(instance *types.Instance) {
m.Lock() m.Lock()
defer m.Unlock() defer m.Unlock()
@@ -109,13 +137,7 @@ func (m *manager) disconnect(instance *types.Instance) {
return return
} }
t := m.terminals[instance.Name] m.disconnectTerminal(instance)
if t.write != nil {
close(t.write)
}
if t.conn != nil {
t.conn.Close()
}
m.untrackInstance(instance) m.untrackInstance(instance)
} }
@@ -123,16 +145,18 @@ func (m *manager) process() {
for { for {
select { select {
case i := <-m.sendCh: case i := <-m.sendCh:
t := m.terminals[i.name] t := m.getTerminal(i.name)
t.write <- i.data if t != nil {
t.write <- i.data
}
case instance := <-m.errorCh: case instance := <-m.errorCh:
// check if it still exists before reconnecting // check if it still exists before reconnecting
i := core.InstanceGet(&types.Session{Id: instance.SessionId}, instance.Name) i := core.InstanceGet(&types.Session{Id: instance.SessionId}, instance.Name)
if i == nil { if i == nil {
log.Println("Instance doest not exist anymore. Won't reconnect") log.Println("Instance doesn't exist anymore. Won't reconnect")
continue continue
} }
log.Println("reconnecting") log.Println("Reconnecting")
m.connect(instance) m.connect(instance)
} }
} }
@@ -147,7 +171,7 @@ func NewManager(s *types.Session) (*manager, error) {
m := &manager{ m := &manager{
sendCh: make(chan info), sendCh: make(chan info),
receiveCh: make(chan info), receiveCh: make(chan info),
terminals: make(map[string]terminal), terminals: make(map[string]*terminal),
errorCh: make(chan *types.Instance), errorCh: make(chan *types.Instance),
instances: make(map[string]*types.Instance), instances: make(map[string]*types.Instance),
} }