Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes for disable keep alive #2

Merged
merged 1 commit into from
Apr 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified bins/client.exe
Binary file not shown.
Binary file modified bins/server.exe
Binary file not shown.
10 changes: 8 additions & 2 deletions client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
var allFailedCons map[int][]util.ConnInfo // Stores entire failed info
var failedCons *failedConnsStruct // Stores failed info for each iteration
var serverInfoMap *sync.Map
var disableKeepAlive bool
var keepAliveTimeOut int

type failedConnsStruct struct {
failedCons []util.ConnInfo
Expand Down Expand Up @@ -193,8 +195,8 @@ func invokeUdpClient(proto, address string, conns, iter int, args map[string]str

func invokeTcpClient(proto, address string, conns, iter int, args map[string]string, wg *sync.WaitGroup, ctx context.Context) {

disableKeepAlive, _ := strconv.ParseBool(args[util.AtribDisableKeepAlive])
keepAliveTimeOut, _ := strconv.Atoi(args[util.AtribTimeoutKeepAlive])
disableKeepAlive, _ = strconv.ParseBool(args[util.AtribDisableKeepAlive])
keepAliveTimeOut, _ = strconv.Atoi(args[util.AtribTimeoutKeepAlive])

var connMap = make(map[string]net.Conn)

Expand Down Expand Up @@ -335,6 +337,10 @@ func startTcpClient(clientName, remoteAddr string, c net.Conn, args map[string]s
return
}
msgSent := clientName + "- Req-" + strconv.Itoa(i) + "\n"
if i == 1 {
msgSent = fmt.Sprintf("dka:%t,tka:%d|%s", disableKeepAlive, keepAliveTimeOut, msgSent)
log.Println("Initial packet sent: ", msgSent)
}
_, sendErr := c.Write([]byte(msgSent))
if sendErr != nil {
if strings.Contains(sendErr.Error(), util.ErrMsgListenClosed) {
Expand Down
45 changes: 43 additions & 2 deletions server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,9 @@ func startHttpHandler() {
http.HandleFunc("/toggleprobe", toggleProbeHandler)
log.Println("Toggle Probe started on port : ", util.HttpPort)
http.HandleFunc("/failreadinessprobe", failReadinessProbeHandler)
log.Println("Fail Redainess Probe started on port : ", util.HttpPort)
log.Println("Fail Readiness Probe started on port : ", util.HttpPort)
http.HandleFunc("/passreadinessprobe", passReadinessProbeHandler)
log.Println("Pass Redainess Probe started on port : ", util.HttpPort)
log.Println("Pass Readiness Probe started on port : ", util.HttpPort)
http.HandleFunc("/telnet", telnetHandler)
log.Println("Telnet handler started on port : ", util.HttpPort)
http.HandleFunc("/list", apiListHandler)
Expand Down Expand Up @@ -295,6 +295,41 @@ func invokeTcpServer(proto, address, serverInfo string) {

}

func setKeepAlive(c net.Conn, msg string) {

strSplit := strings.Split(msg, "|")
if len(strSplit) < 2 {
return
}

tc, ok := c.(*net.TCPConn)
if !ok {
return
}

strSplit = strings.Split(strSplit[0], ",")
for _, v := range strSplit {
kvs := strings.Split(v, ":")
if len(kvs) < 2 {
continue
}
if strings.Contains(v, "dka") {
dka, _ := strconv.ParseBool(kvs[1])
tc.SetKeepAlive(!dka)
fmt.Println("Setting Disable Keep Alive :", dka)
if dka {
return
}
}
if strings.Contains(v, "tka") {
tka, _ := strconv.Atoi(kvs[1])
keepAliveTimeOutInt := time.Duration(tka) * time.Millisecond
tc.SetKeepAlivePeriod(keepAliveTimeOutInt)
fmt.Println("Setting Keep Alive Timeout :", tka)
}
}
}

func handleTcpConnection(conn net.Conn, serverInfo string) {
// Replacing for now
hostName, _ := os.Hostname()
Expand All @@ -305,9 +340,15 @@ func handleTcpConnection(conn net.Conn, serverInfo string) {
s := bufio.NewScanner(conn)
var msgSent, receivedMsg string
var sendError error
firstPacket := true

for s.Scan() {
receivedMsg = s.Text()
log.Print("-> ", string(receivedMsg))
if firstPacket && strings.Contains(receivedMsg, "dka") {
setKeepAlive(conn, receivedMsg)
firstPacket = false
}
msgSent = constructServerResp(receivedMsg, serverInfo)
_, sendError = conn.Write([]byte(msgSent))
if sendError != nil {
Expand Down
2 changes: 1 addition & 1 deletion util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

const (
Version = "v15.03.2023"
Version = "v04.04.2023"
MaxDropPackets = 100
HttpPort = 8090
ErrMsgConnForciblyClosed = "An existing connection was forcibly closed by the remote host"
Expand Down