feat(web): try to reconnect to TDBMS server on failure to get response
This commit is contained in:
parent
e7be627292
commit
4d2326986f
@ -7,13 +7,29 @@ import (
|
||||
|
||||
// TDBMS connection struct
|
||||
type TDBMSConnection struct {
|
||||
conn net.Conn
|
||||
domain string
|
||||
addr string
|
||||
conn net.Conn
|
||||
}
|
||||
|
||||
// Connect to TDBMS server
|
||||
func (tdbms *TDBMSConnection) Connect(domain, addr string) error {
|
||||
c, err := net.Dial(domain, addr)
|
||||
tdbms.conn = c
|
||||
if err == nil {
|
||||
tdbms.domain = domain
|
||||
tdbms.addr = addr
|
||||
tdbms.conn = c
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// Reconnect to TDBMS server
|
||||
func (tdbms *TDBMSConnection) Reconnect() error {
|
||||
tdbms.Close()
|
||||
c, err := net.Dial(tdbms.domain, tdbms.addr)
|
||||
if err == nil {
|
||||
tdbms.conn = c
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
@ -139,10 +139,21 @@ func HandlerTDBMS(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
response = tdbms.Query(request.TRDB, request.TRC, request.TRB)
|
||||
if response == nil {
|
||||
http.Error(w, "Failed to execute request", http.StatusInternalServerError)
|
||||
defer log.Println("Failed to execute request")
|
||||
return
|
||||
log.Println("Failed to get TDBMS response. Trying to reconnect to TDBMS server...")
|
||||
err = tdbms.Reconnect()
|
||||
if err != nil {
|
||||
http.Error(w, "TDBMS is down", http.StatusServiceUnavailable)
|
||||
defer log.Println("TDBMS is down")
|
||||
return
|
||||
}
|
||||
response = tdbms.Query(request.TRDB, request.TRC, request.TRB)
|
||||
if response == nil {
|
||||
http.Error(w, "Failed to get TDBMS response", http.StatusInternalServerError)
|
||||
defer log.Println("Failed to get TDBMS response")
|
||||
return
|
||||
}
|
||||
}
|
||||
log.Println("Got TDBMS response")
|
||||
if request.TRDB == "$TFM" && (request.TRC == 0b10000 || request.TRC == 0b101000) {
|
||||
var json_response JSON
|
||||
err = json.Unmarshal(response, &json_response)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user