Skip to content

Commit

Permalink
improving conn close
Browse files Browse the repository at this point in the history
  • Loading branch information
uoosef committed Oct 4, 2023
1 parent 2a5ab4f commit da8de31
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
6 changes: 4 additions & 2 deletions relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ func (client *Client) handleRequest() {

rConn, err := net.Dial(network, address)

defer func() {
_ = rConn.Close()
}()

if err != nil {
log.Println(fmt.Errorf("failed to connect to socket: %v", err))
return
Expand All @@ -134,8 +138,6 @@ func (client *Client) handleRequest() {
// transmit data
go Copy(client.conn, rConn)
Copy(rConn, client.conn)

_ = rConn.Close()
}

// Copy reads from src and writes to dst until either EOF is reached on src or
Expand Down
5 changes: 4 additions & 1 deletion udp.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,10 @@ func getOrCreateUDPChan(destination, header string) (chan []byte, error) {
go readFromConn(udpConn, udpReadChanFromConn)

go func() {
defer delete(udpToTCPChannels, channelID)
defer func() {
delete(udpToTCPChannels, channelID)
_ = udpConn.Close()
}()
for {
select {
case dataFromWS := <-udpToTCPChannels[channelID]:
Expand Down

0 comments on commit da8de31

Please sign in to comment.