Browse Source

reordering and unifying drop connection

pull/3370/head
Juan Leni 6 years ago
parent
commit
3f3dcf3701
No known key found for this signature in database GPG Key ID: 23F1452155140419
2 changed files with 53 additions and 51 deletions
  1. +35
    -33
      privval/signer_dialer_endpoint.go
  2. +18
    -18
      privval/signer_listener_endpoint.go

+ 35
- 33
privval/signer_dialer_endpoint.go View File

@ -104,13 +104,7 @@ func (sd *SignerDialerEndpoint) Close() error {
defer sd.mtx.Unlock() defer sd.mtx.Unlock()
sd.Logger.Debug("SignerDialerEndpoint: Close") sd.Logger.Debug("SignerDialerEndpoint: Close")
if sd.conn != nil {
if err := sd.conn.Close(); err != nil {
sd.Logger.Error("OnStop", "err", cmn.ErrorWrap(err, "closing listener failed"))
sd.conn = nil
}
}
sd.dropConnection()
return nil return nil
} }
@ -121,7 +115,35 @@ func (sd *SignerDialerEndpoint) IsConnected() bool {
return sd.isConnected() return sd.isConnected()
} }
// IsConnected indicates if there is an active connection
func (sd *SignerDialerEndpoint) handleRequest() {
if !sd.IsRunning() {
return // Ignore error from listener closing.
}
sd.Logger.Info("SignerDialerEndpoint: connected", "timeout", sd.timeoutReadWrite)
req, err := sd.readMessage()
if err != nil {
if err != io.EOF {
sd.Logger.Error("SignerDialerEndpoint handleMessage", "err", err)
}
return
}
res, err := HandleValidatorRequest(req, sd.chainID, sd.privVal)
if err != nil {
// only log the error; we'll reply with an error in res
sd.Logger.Error("handleMessage handleMessage", "err", err)
}
err = sd.writeMessage(res)
if err != nil {
sd.Logger.Error("handleMessage writeMessage", "err", err)
return
}
}
func (sd *SignerDialerEndpoint) isConnected() bool { func (sd *SignerDialerEndpoint) isConnected() bool {
return sd.IsRunning() && sd.conn != nil return sd.IsRunning() && sd.conn != nil
} }
@ -176,32 +198,12 @@ func (sd *SignerDialerEndpoint) writeMessage(msg RemoteSignerMsg) (err error) {
return return
} }
func (sd *SignerDialerEndpoint) handleRequest() {
if !sd.IsRunning() {
return // Ignore error from listener closing.
}
sd.Logger.Info("SignerDialerEndpoint: connected", "timeout", sd.timeoutReadWrite)
req, err := sd.readMessage()
if err != nil {
if err != io.EOF {
sd.Logger.Error("SignerDialerEndpoint handleMessage", "err", err)
func (sd *SignerDialerEndpoint) dropConnection() {
if sd.conn != nil {
if err := sd.conn.Close(); err != nil {
sd.Logger.Error("SignerDialerEndpoint::dropConnection", "err", err)
} }
return
}
res, err := HandleValidatorRequest(req, sd.chainID, sd.privVal)
if err != nil {
// only log the error; we'll reply with an error in res
sd.Logger.Error("handleMessage handleMessage", "err", err)
}
err = sd.writeMessage(res)
if err != nil {
sd.Logger.Error("handleMessage writeMessage", "err", err)
return
sd.conn = nil
} }
} }


+ 18
- 18
privval/signer_listener_endpoint.go View File

@ -229,6 +229,24 @@ func (sl *SignerListenerEndpoint) ensureConnection(maxWait time.Duration) error
return nil return nil
} }
func (sl *SignerListenerEndpoint) acceptNewConnection() (net.Conn, error) {
sl.Logger.Debug("SignerListenerEndpoint: AcceptNewConnection")
if !sl.IsRunning() || sl.listener == nil {
return nil, fmt.Errorf("endpoint is closing")
}
// wait for a new conn
conn, err := sl.listener.Accept()
if err != nil {
sl.Logger.Debug("listener accept failed", "err", err)
return nil, err
}
sl.Logger.Info("SignerListenerEndpoint: New connection")
return conn, nil
}
func (sl *SignerListenerEndpoint) dropConnection() { func (sl *SignerListenerEndpoint) dropConnection() {
if sl.conn != nil { if sl.conn != nil {
if err := sl.conn.Close(); err != nil { if err := sl.conn.Close(); err != nil {
@ -275,21 +293,3 @@ func (sl *SignerListenerEndpoint) serviceLoop() {
} }
} }
} }
func (sl *SignerListenerEndpoint) acceptNewConnection() (net.Conn, error) {
sl.Logger.Debug("SignerListenerEndpoint: AcceptNewConnection")
if !sl.IsRunning() || sl.listener == nil {
return nil, fmt.Errorf("endpoint is closing")
}
// wait for a new conn
conn, err := sl.listener.Accept()
if err != nil {
sl.Logger.Debug("listener accept failed", "err", err)
return nil, err
}
sl.Logger.Info("SignerListenerEndpoint: New connection")
return conn, nil
}

Loading…
Cancel
Save