Connection returns the nth connection (starting from 0) in this transfer instance if it is initialized and otherwise initializes a new connection and saves it in the nth position. In all cases, nil is returned with an error if n is greater than the maximum number of connections, including when the
(n int)
| 99 | // if n is greater than the maximum number of connections, including when |
| 100 | // the connection array itself is nil. |
| 101 | func (st *SSHTransfer) Connection(n int) (*PktlineConnection, error) { |
| 102 | st.lock.RLock() |
| 103 | if n >= len(st.conn) { |
| 104 | st.lock.RUnlock() |
| 105 | return nil, errors.New(tr.Tr.Get("pure SSH connection unavailable (#%d)", n)) |
| 106 | } |
| 107 | if st.conn[n] != nil { |
| 108 | defer st.lock.RUnlock() |
| 109 | return st.conn[n], nil |
| 110 | } |
| 111 | st.lock.RUnlock() |
| 112 | |
| 113 | st.lock.Lock() |
| 114 | defer st.lock.Unlock() |
| 115 | if st.conn[n] != nil { |
| 116 | return st.conn[n], nil |
| 117 | } |
| 118 | conn, _, err := st.spawnConnection(n) |
| 119 | if err != nil { |
| 120 | return nil, err |
| 121 | } |
| 122 | st.conn[n] = conn |
| 123 | return conn, nil |
| 124 | } |
| 125 | |
| 126 | // ConnectionCount returns the number of connections this object has. |
| 127 | func (st *SSHTransfer) ConnectionCount() int { |
no test coverage detected