forked from dmachard/DNS-collector
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: be more aggressive to close conn (dmachard#308)
* be more aggressive to close conn * fix race * fix for windows
- Loading branch information
Showing
8 changed files
with
64 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package netlib | ||
|
||
import ( | ||
"io" | ||
"net" | ||
) | ||
|
||
// thanks to https://stackoverflow.com/questions/28967701/golang-tcp-socket-cant-close-after-get-file, | ||
// call conn.CloseRead() before calling conn.Close() | ||
func Close(conn io.Closer, reset bool) error { | ||
type ReadCloser interface { | ||
CloseRead() error | ||
} | ||
|
||
// Agressive closing, send TCP RESET instead of FIN | ||
if reset { | ||
if tcpConn, ok := conn.(*net.TCPConn); ok { | ||
tcpConn.SetLinger(0) | ||
} | ||
} | ||
|
||
var errs []error | ||
if closer, ok := conn.(ReadCloser); ok { | ||
errs = append(errs, closer.CloseRead()) | ||
} | ||
errs = append(errs, conn.Close()) | ||
for _, err := range errs { | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} |
File renamed without changes.
File renamed without changes.