getNewConn is used to return a new connection
(addr net.Addr)
| 228 | |
| 229 | // getNewConn is used to return a new connection |
| 230 | func (p *connPool) getNewConn(addr net.Addr) (*conn, error) { |
| 231 | // Try to dial the conn |
| 232 | con, err := net.DialTimeout("tcp", addr.String(), 10*time.Second) |
| 233 | if err != nil { |
| 234 | return nil, err |
| 235 | } |
| 236 | |
| 237 | // Cast to TCPConn |
| 238 | if tcp, ok := con.(*net.TCPConn); ok { |
| 239 | tcp.SetKeepAlive(true) |
| 240 | tcp.SetNoDelay(true) |
| 241 | } |
| 242 | |
| 243 | // Write the multiplex byte to set the mode |
| 244 | if _, err := con.Write([]byte{byte(rpcInternal)}); err != nil { |
| 245 | con.Close() |
| 246 | return nil, err |
| 247 | } |
| 248 | |
| 249 | // Setup the logger |
| 250 | conf := yamux.DefaultConfig() |
| 251 | conf.LogOutput = p.logOutput |
| 252 | |
| 253 | // Create a multiplexed session |
| 254 | session, err := yamux.Client(con, conf) |
| 255 | if err != nil { |
| 256 | con.Close() |
| 257 | return nil, err |
| 258 | } |
| 259 | |
| 260 | // Wrap the connection |
| 261 | c := &conn{ |
| 262 | refCount: 1, |
| 263 | addr: addr, |
| 264 | session: session, |
| 265 | clients: list.New(), |
| 266 | lastUsed: time.Now(), |
| 267 | pool: p, |
| 268 | } |
| 269 | return c, nil |
| 270 | } |
| 271 | |
| 272 | // clearConn is used to clear any cached connection, potentially in response to an erro |
| 273 | func (p *connPool) clearConn(conn *conn) { |