Skip to content

Update example #906

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

Merged
merged 1 commit into from
Apr 1, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,17 @@ task: [build] go build -o app main.go
task: [run] ./app -server &
task: [run] sleep 1
task: [run] ./app
01:59:18.896796 [C] Send (hello)
01:59:18.896882 [S] Recv (hello)
01:59:18.897098 [S] Send (HELLO)
01:59:18.897149 [C] Recv (HELLO)
01:59:18.897174 [C] close
01:59:18.897177 [S] disconnect
01:59:18.897205 [S] close
02:42:06.032066 [C] Send (hello)
02:42:06.032128 [S] Recv (hello)
02:42:06.032345 [S] Send (HELLO)
02:42:06.032365 [C] Recv (HELLO)
02:42:06.032378 [C] SEND FIN (shutdown(SHUT_WR))
02:42:06.032382 [S] disconnect
02:42:06.032410 [S] close
02:42:06.032413 [C] disconnect
02:42:06.032436 [C] close
task: [run] pkill -INT -f './app -server'
01:59:18.909751 [S] Shutdown...
02:42:06.043557 [S] Shutdown...
```

## 参考情報
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"net"
"os"
"os/signal"
"time"
)

type (
Expand Down Expand Up @@ -117,26 +118,29 @@ func runServer() error {
}
log.Printf("[S] Send (%s)", message)

// Graceful shutdown
{
unixConn, ok := conn.(*net.UnixConn)
if !ok {
errCh <- fmt.Errorf("conn.(*net.UnixConn) failed")
return
}
// FIN待機用のタイムアウト設定
err = conn.SetReadDeadline(time.Now().Add(2 * time.Second))
if err != nil {
log.Printf("[S] SetReadDeadline error: %v", err)
errCh <- err
return
}

err = unixConn.CloseWrite()
if err != nil {
errCh <- err
return
for {
clear(buf)
if n, err = conn.Read(buf); n == 0 || errors.Is(err, io.EOF) {
log.Println("[S] disconnect")
break
}

for {
clear(buf)
if n, err = conn.Read(buf); n == 0 || errors.Is(err, io.EOF) {
log.Println("[S] disconnect")
if err != nil {
if netErr, ok := err.(net.Error); ok && netErr.Timeout() {
log.Println("[S] timeout (client FIN)")
break
}

errCh <- err
return
}
}
}()
Expand Down Expand Up @@ -180,5 +184,28 @@ func runClient() error {
message := string(buf[:n])
log.Printf("[C] Recv (%s)", message)

// Graceful shutdown
{
unixConn, ok := conn.(*net.UnixConn)
if !ok {
return fmt.Errorf("conn.(*net.UnixConn) failed")

}

err = unixConn.CloseWrite()
if err != nil {
return err
}
log.Println("[C] SEND FIN (shutdown(SHUT_WR))")

for {
clear(buf)
if n, err = conn.Read(buf); n == 0 || errors.Is(err, io.EOF) {
log.Println("[C] disconnect")
break
}
}
}

return nil
}
Loading