added close-on-conn-error-event.

added the client token in connection output over ctl
This commit is contained in:
hyung-hwan 2025-03-05 00:44:00 +09:00
parent bec93289f5
commit b6fb296608
3 changed files with 16 additions and 10 deletions

View File

@ -24,6 +24,7 @@ import "time"
type json_in_client_conn struct {
ServerAddrs []string `json:"server-addrs"` // multiple addresses for round-robin connection re-attempts
ClientToken string `json:"client-token"`
CloseOnConnErrorEvent bool `json:"close-on-conn-error-event"`
}
type json_in_client_route struct {
@ -50,6 +51,7 @@ type json_out_client_conn struct {
CurrentServerIndex int `json:"current-server-index"`
ServerAddr string `json:"server-addr"` // actual server address
ClientAddr string `json:"client-addr"`
ClientToken string `json:"client-token"`
Routes []json_out_client_route `json:"routes"`
}
@ -251,6 +253,7 @@ func (ctl *client_ctl_client_conns) ServeHTTP(w http.ResponseWriter, req *http.R
CurrentServerIndex: cts.cfg.Index,
ServerAddr: cts.remote_addr,
ClientAddr: cts.local_addr,
ClientToken: cts.Token,
Routes: jsp,
})
cts.route_mtx.Unlock()
@ -279,6 +282,7 @@ func (ctl *client_ctl_client_conns) ServeHTTP(w http.ResponseWriter, req *http.R
cc.ServerAddrs = in_cc.ServerAddrs
cc.ClientToken = in_cc.ClientToken
cc.CloseOnConnErrorEvent = in_cc.CloseOnConnErrorEvent
cts, err = c.start_service(&cc) // TODO: this can be blocking. do we have to resolve addresses before calling this? also not good because resolution succeed or fail at each attempt. however ok as ServeHTTP itself is in a goroutine?
if err != nil {
status_code = WriteJsonRespHeader(w, http.StatusInternalServerError)
@ -358,6 +362,7 @@ func (ctl *client_ctl_client_conns_id) ServeHTTP(w http.ResponseWriter, req *htt
CurrentServerIndex: cts.cfg.Index,
ServerAddr: cts.local_addr,
ClientAddr: cts.remote_addr,
ClientToken: cts.Token,
Routes: jsp,
}
cts.route_mtx.Unlock()

View File

@ -47,6 +47,7 @@ type ClientConnConfig struct {
ServerSeedTmout time.Duration
ServerAuthority string // http2 :authority header
ClientToken string
CloseOnConnErrorEvent bool
}
type ClientConnConfigActive struct {
@ -131,6 +132,7 @@ type ClientConn struct {
Id ConnId
Sid string // id rendered in string
State ClientConnState
Token string
local_addr string
remote_addr string
@ -945,7 +947,6 @@ func (cts *ClientConn) RunTask(wg *sync.WaitGroup) {
var c_seed Seed
var s_seed *Seed
var p *peer.Peer
var client_token string
var ok bool
var err error
var opts []grpc.DialOption
@ -1009,18 +1010,18 @@ start_over:
cts.C.log.Write(cts.Sid, LOG_INFO, "Got packet stream from server[%d] %s", cts.cfg.Index, cts.cfg.ServerAddrs[cts.cfg.Index])
cts.State = CLIENT_CONN_CONNECTED
cts.Token = cts.cfg.ClientToken
if cts.Token == "" { cts.Token = cts.C.token }
cts.psc = &GuardedPacketStreamClient{Hodu_PacketStreamClient: psc}
client_token = cts.cfg.ClientToken
if client_token == "" { client_token = cts.C.token }
if client_token != "" {
err = cts.psc.Send(MakeConnDescPacket(client_token))
if cts.Token != "" {
err = cts.psc.Send(MakeConnDescPacket(cts.Token))
if err != nil {
cts.C.log.Write(cts.Sid, LOG_ERROR, "Failed to send conn-desc(%s) to server[%d] %s - %s", client_token, cts.cfg.Index, cts.cfg.ServerAddrs[cts.cfg.Index], err.Error())
cts.C.log.Write(cts.Sid, LOG_ERROR, "Failed to send conn-desc(%s) to server[%d] %s - %s", cts.Token, cts.cfg.Index, cts.cfg.ServerAddrs[cts.cfg.Index], err.Error())
goto reconnect_to_server
} else {
cts.C.log.Write(cts.Sid, LOG_DEBUG, "Sending conn-desc(%s) to server[%d] %s", client_token, cts.cfg.Index, cts.cfg.ServerAddrs[cts.cfg.Index])
cts.C.log.Write(cts.Sid, LOG_DEBUG, "Sending conn-desc(%s) to server[%d] %s", cts.Token, cts.cfg.Index, cts.cfg.ServerAddrs[cts.cfg.Index])
}
}
@ -1193,8 +1194,7 @@ start_over:
x, ok = pkt.U.(*Packet_ConnErr)
if ok {
cts.C.log.Write(cts.Sid, LOG_ERROR, "Received conn_error(%d, %s) event from %s", x.ConnErr.ErrorId, x.ConnErr.Text, cts.remote_addr)
// if no retry goto done.. othersise reconnect...
goto done
if cts.cfg.CloseOnConnErrorEvent { goto done }
} else {
cts.C.log.Write(cts.Sid, LOG_ERROR, "Invalid conn_error event from %s", cts.remote_addr)
}

View File

@ -769,7 +769,7 @@ func (cts *ServerConn) receive_from_stream(wg *sync.WaitGroup) {
// error
cts.S.cts_mtx.Unlock()
cts.S.log.Write(cts.Sid, LOG_ERROR, "Invalid conn_desc packet from %s - duplicate token '%s'", cts.RemoteAddr, x.Conn.Token)
cts.pss.Send(MakeConnErrorPacket(1, "duplicate token refused"))
cts.pss.Send(MakeConnErrorPacket(1, fmt.Sprintf("duplicate token refused - %s", x.Conn.Token)))
cts.ReqStop() // TODO: is this desirable to disconnect?
} else {
if cts.ClientToken != "" { delete(cts.S.cts_map_by_token, cts.ClientToken) }
@ -846,6 +846,7 @@ func (cts *ServerConn) RunTask(wg *sync.WaitGroup) {
done:
cts.ReqStop() // just in case
cts.route_wg.Wait()
cts.S.log.Write(cts.Sid, LOG_INFO, "End of connection task")
}
func (cts *ServerConn) ReqStop() {