-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclose.go
More file actions
61 lines (55 loc) · 1.68 KB
/
Copy pathclose.go
File metadata and controls
61 lines (55 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package gonnect
import "net"
// PreCloser is implemented by connections that split shutdown into a
// concurrent phase and a final phase.
//
// PreClose can be called more than once and can be called while I/O operations
// are still active. It should do only the work that is necessary to make active
// reads or writes stop. It must not release buffers or other state that active
// I/O operations can still use.
type PreCloser interface {
PreClose() error
}
// PostCloser is implemented by connections that split shutdown into a
// concurrent phase and a final phase.
//
// PostClose can be called more than once, but callers must not call it in
// parallel. It should run after all active I/O operations stopped. It can
// release buffers and other state that PreClose intentionally left in place.
type PostCloser interface {
PostClose() error
}
// TwoStepCloser is implemented by connections that support PreClose and
// PostClose.
//
// Close implementations for these connections should do both phases.
type TwoStepCloser interface {
PreCloser
PostCloser
}
// PreClose starts closing c.
//
// If c implements PreCloser, PreClose calls c.PreClose. Otherwise it calls
// c.Close. A nil connection is accepted and returns nil.
func PreClose(c net.Conn) error {
if c == nil {
return nil
}
if closer, ok := c.(PreCloser); ok {
return closer.PreClose()
}
return c.Close()
}
// PostClose finishes closing c.
//
// If c implements PostCloser, PostClose calls c.PostClose. Otherwise it calls
// c.Close. A nil connection is accepted and returns nil.
func PostClose(c net.Conn) error {
if c == nil {
return nil
}
if closer, ok := c.(PostCloser); ok {
return closer.PostClose()
}
return c.Close()
}