|
| 1 | +package conn |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "io" |
| 7 | + "net" |
| 8 | + "os" |
| 9 | + "runtime" |
| 10 | + "sync" |
| 11 | + "sync/atomic" |
| 12 | + "syscall" |
| 13 | + "time" |
| 14 | +) |
| 15 | + |
| 16 | +type ChannelConn struct { |
| 17 | + chanRX <-chan []byte // read from this channel, owned by the writing-side to this channel |
| 18 | + chanTX chan<- []byte // write to this channel, owned by this struct |
| 19 | + |
| 20 | + chanClose chan struct{} // notify close event to unblock blocking read/write operations |
| 21 | + closed atomic.Bool // true if the channel is closed |
| 22 | + |
| 23 | + readBuf []byte // protected by readBufMutex, accessed only from readLocked and readLockedFromBuffer |
| 24 | + readBufMutex sync.Mutex |
| 25 | + |
| 26 | + pendingWrite atomic.Bool // indicates if there is an outstanding writer blocking, protecting TX channel |
| 27 | + |
| 28 | + nonblocking atomic.Bool |
| 29 | +} |
| 30 | + |
| 31 | +func NewChannelConn(rx <-chan []byte, tx chan<- []byte) *ChannelConn { |
| 32 | + return &ChannelConn{ |
| 33 | + chanRX: rx, |
| 34 | + chanTX: tx, |
| 35 | + chanClose: make(chan struct{}), |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +// ChannelConn implements [Conn]. |
| 40 | +var _ Conn = (*ChannelConn)(nil) |
| 41 | + |
| 42 | +// Read reads data from the channel. Implements [net.Conn]. |
| 43 | +func (c *ChannelConn) Read(b []byte) (n int, err error) { |
| 44 | + if c.closed.Load() { |
| 45 | + return 0, io.ErrClosedPipe |
| 46 | + } |
| 47 | + |
| 48 | + c.readBufMutex.Lock() |
| 49 | + defer c.readBufMutex.Unlock() |
| 50 | + |
| 51 | + return c.readLocked(b) |
| 52 | +} |
| 53 | + |
| 54 | +// read blocks until some data is available, or the channel is closed. |
| 55 | +func (c *ChannelConn) readLocked(b []byte) (int, error) { |
| 56 | + if len(c.readBuf) != 0 { // need to resume reading from the buffer |
| 57 | + return c.readLockedFromBuffer(b) |
| 58 | + } |
| 59 | + |
| 60 | + if c.nonblocking.Load() { |
| 61 | + for { |
| 62 | + select { |
| 63 | + case <-c.chanClose: |
| 64 | + return 0, io.ErrClosedPipe |
| 65 | + case c.readBuf = <-c.chanRX: |
| 66 | + if len(c.readBuf) != 0 { // buffer is empty, read from the channel OK |
| 67 | + return c.readLockedFromBuffer(b) |
| 68 | + } else { // empty read from channel |
| 69 | + if c.readBuf == nil { // closed channel |
| 70 | + return 0, io.EOF |
| 71 | + } else { // channel open, but empty read: other end testing if write will block |
| 72 | + continue |
| 73 | + } |
| 74 | + } |
| 75 | + default: |
| 76 | + return 0, syscall.EAGAIN |
| 77 | + } |
| 78 | + } |
| 79 | + } else { |
| 80 | + for { |
| 81 | + select { |
| 82 | + case <-c.chanClose: |
| 83 | + return 0, io.ErrClosedPipe |
| 84 | + case c.readBuf = <-c.chanRX: |
| 85 | + if len(c.readBuf) != 0 { // buffer is empty, read from the channel OK |
| 86 | + return c.readLockedFromBuffer(b) |
| 87 | + } else { // empty read from channel |
| 88 | + if c.readBuf == nil { // closed channel |
| 89 | + return 0, io.EOF |
| 90 | + } else { // channel open, but empty read: other end testing if write will block |
| 91 | + continue |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +// readLockedFromBuffer reads from the buffer. Assumes the buffer is non-empty. |
| 100 | +func (c *ChannelConn) readLockedFromBuffer(b []byte) (n int, err error) { |
| 101 | + n = copy(b, c.readBuf) |
| 102 | + c.readBuf = c.readBuf[n:] |
| 103 | + return |
| 104 | +} |
| 105 | + |
| 106 | +// Write writes data to the channel. Implements [net.Conn]. |
| 107 | +func (c *ChannelConn) Write(b []byte) (n int, err error) { |
| 108 | + if c.nonblocking.Load() { |
| 109 | + if c.pendingWrite.CompareAndSwap(false, true) { |
| 110 | + defer c.pendingWrite.Store(false) |
| 111 | + n, err = c.writeFlagAcquired(b) |
| 112 | + } else { |
| 113 | + return 0, syscall.EAGAIN |
| 114 | + } |
| 115 | + } else { |
| 116 | + // retry until acquired the pending write flag |
| 117 | + for !c.pendingWrite.CompareAndSwap(false, true) { |
| 118 | + runtime.Gosched() |
| 119 | + } |
| 120 | + defer c.pendingWrite.Store(false) |
| 121 | + n, err = c.writeFlagAcquired(b) |
| 122 | + } |
| 123 | + |
| 124 | + return |
| 125 | +} |
| 126 | + |
| 127 | +// writeFlagAcquired writes data to the channel. Caller must |
| 128 | +// acquire the pending write flag before calling this function. |
| 129 | +func (c *ChannelConn) writeFlagAcquired(b []byte) (n int, err error) { |
| 130 | + if c.closed.Load() { // check if the channel is closed only after acquiring the pending write flag to prevent racing condition |
| 131 | + return 0, io.ErrClosedPipe |
| 132 | + } |
| 133 | + |
| 134 | + expectedLen := len(b) |
| 135 | + |
| 136 | + bCopy := make([]byte, expectedLen) |
| 137 | + if copy(bCopy, b) != expectedLen { |
| 138 | + return 0, io.ErrUnexpectedEOF |
| 139 | + } |
| 140 | + |
| 141 | + if c.nonblocking.Load() { |
| 142 | + select { |
| 143 | + case <-c.chanClose: |
| 144 | + return 0, io.ErrClosedPipe |
| 145 | + case c.chanTX <- bCopy: |
| 146 | + return expectedLen, nil |
| 147 | + default: |
| 148 | + return 0, syscall.EAGAIN |
| 149 | + } |
| 150 | + } else { |
| 151 | + select { |
| 152 | + case <-c.chanClose: |
| 153 | + return 0, io.ErrClosedPipe |
| 154 | + case c.chanTX <- bCopy: |
| 155 | + return expectedLen, nil |
| 156 | + } |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +func (c *ChannelConn) Close() error { |
| 161 | + if c.closed.CompareAndSwap(false, true) { |
| 162 | + close(c.chanClose) |
| 163 | + |
| 164 | + // acquire the pending write flag before closing the TX channel |
| 165 | + for !c.pendingWrite.CompareAndSwap(false, true) { |
| 166 | + runtime.Gosched() |
| 167 | + } |
| 168 | + close(c.chanTX) |
| 169 | + c.pendingWrite.Store(false) |
| 170 | + |
| 171 | + return nil |
| 172 | + } |
| 173 | + |
| 174 | + return io.ErrClosedPipe // double close |
| 175 | +} |
| 176 | + |
| 177 | +type channelAddr struct{} |
| 178 | + |
| 179 | +func (channelAddr) Network() string { return "channel" } |
| 180 | +func (channelAddr) String() string { return "channel" } |
| 181 | + |
| 182 | +// ChannelConn does not implement [NetworkConn]. |
| 183 | +var _ NetworkConn = (*ChannelConn)(nil) |
| 184 | + |
| 185 | +// LocalAddr returns the local network address. Implements [net.Conn]. |
| 186 | +func (*ChannelConn) LocalAddr() net.Addr { return channelAddr{} } |
| 187 | + |
| 188 | +// RemoteAddr returns the remote network address. Implements [net.Conn]. |
| 189 | +func (*ChannelConn) RemoteAddr() net.Addr { return channelAddr{} } |
| 190 | + |
| 191 | +// ChannelConn does not implement [DeadlineConn]. However, fake implementation |
| 192 | +// is provided such that it can be used as [net.Conn] in some cases when |
| 193 | +// deadlines are not used. |
| 194 | +// |
| 195 | +// TODO: properly implement [DeadlineConn]. |
| 196 | +var _ DeadlineConn = (*ChannelConn)(nil) |
| 197 | + |
| 198 | +// SetDeadline is not supported by ChannelConn. It will always return |
| 199 | +// [os.ErrNoDeadline]. |
| 200 | +// |
| 201 | +// TODO: properly implement the support for deadlines. |
| 202 | +func (*ChannelConn) SetDeadline(time.Time) error { |
| 203 | + return os.ErrNoDeadline |
| 204 | +} |
| 205 | + |
| 206 | +// SetReadDeadline is not supported by ChannelConn. It will always return |
| 207 | +// [os.ErrNoDeadline]. |
| 208 | +// |
| 209 | +// TODO: properly implement the support for read deadline. |
| 210 | +func (*ChannelConn) SetReadDeadline(time.Time) error { |
| 211 | + return os.ErrNoDeadline |
| 212 | +} |
| 213 | + |
| 214 | +// SetWriteDeadline is not supported by ChannelConn. It will always return |
| 215 | +// [os.ErrNoDeadline]. |
| 216 | +// |
| 217 | +// TODO: properly implement the support for write deadline. |
| 218 | +func (*ChannelConn) SetWriteDeadline(time.Time) error { |
| 219 | + return os.ErrNoDeadline |
| 220 | +} |
| 221 | + |
| 222 | +// ChannelConn implements [NonblockingConn]. |
| 223 | +var _ NonblockingConn = (*ChannelConn)(nil) |
| 224 | + |
| 225 | +// IsNonblock returns true if the connection is in non-blocking mode. |
| 226 | +func (c *ChannelConn) IsNonblock() bool { |
| 227 | + return c.nonblocking.Load() |
| 228 | +} |
| 229 | + |
| 230 | +// SetNonblock updates the non-blocking mode of the connection if applicable. |
| 231 | +func (c *ChannelConn) SetNonblock(nonblocking bool) (ok bool) { |
| 232 | + c.nonblocking.Store(nonblocking) |
| 233 | + return true |
| 234 | +} |
| 235 | + |
| 236 | +// ChannelConn implements [PollConn]. |
| 237 | +var _ PollConn = (*ChannelConn)(nil) |
| 238 | + |
| 239 | +func (c *ChannelConn) PollR(ctx context.Context) (bool, error) { |
| 240 | + if !c.nonblocking.Load() { |
| 241 | + return false, errors.New("polling is not supported in blocking mode") |
| 242 | + } |
| 243 | + |
| 244 | + if c.closed.Load() { |
| 245 | + return false, io.EOF |
| 246 | + } |
| 247 | + |
| 248 | + for !c.readBufMutex.TryLock() && ctx.Err() == nil { |
| 249 | + runtime.Gosched() |
| 250 | + } |
| 251 | + |
| 252 | + if ctx.Err() != nil { |
| 253 | + return false, ctx.Err() |
| 254 | + } |
| 255 | + |
| 256 | + defer c.readBufMutex.Unlock() |
| 257 | + |
| 258 | + if len(c.readBuf) != 0 { |
| 259 | + return true, nil |
| 260 | + } |
| 261 | + |
| 262 | + // We cannot check cap(c.chanRX) vs. len(c.chanRX) here because it is |
| 263 | + // possible that messages in the buffer being empty probes sent by the |
| 264 | + // other end to check if the write will block. Instead the universal |
| 265 | + // reading strategy below is used. |
| 266 | + |
| 267 | + for { |
| 268 | + select { |
| 269 | + case <-c.chanClose: |
| 270 | + return false, io.EOF |
| 271 | + case c.readBuf = <-c.chanRX: |
| 272 | + if len(c.readBuf) != 0 { |
| 273 | + return true, nil |
| 274 | + } else { |
| 275 | + if c.readBuf == nil { |
| 276 | + return false, io.EOF |
| 277 | + } else { |
| 278 | + continue |
| 279 | + } |
| 280 | + } |
| 281 | + case <-ctx.Done(): |
| 282 | + return false, ctx.Err() |
| 283 | + } |
| 284 | + } |
| 285 | +} |
| 286 | + |
| 287 | +func (c *ChannelConn) PollW(ctx context.Context) (bool, error) { |
| 288 | + if !c.nonblocking.Load() { |
| 289 | + return false, errors.New("polling is not supported in blocking mode") |
| 290 | + } |
| 291 | + |
| 292 | + // aquire the pending write flag before writing to the TX channel |
| 293 | + for !c.pendingWrite.CompareAndSwap(false, true) && ctx.Err() == nil { |
| 294 | + runtime.Gosched() |
| 295 | + } |
| 296 | + |
| 297 | + if ctx.Err() != nil { |
| 298 | + return false, ctx.Err() |
| 299 | + } |
| 300 | + |
| 301 | + defer c.pendingWrite.Store(false) |
| 302 | + |
| 303 | + if c.closed.Load() { |
| 304 | + return false, io.EOF |
| 305 | + } |
| 306 | + |
| 307 | + // Buffered channel: |
| 308 | + if cap(c.chanTX) > 0 { |
| 309 | + for ctx.Err() == nil && len(c.chanTX) >= cap(c.chanTX) { |
| 310 | + runtime.Gosched() |
| 311 | + } |
| 312 | + return len(c.chanTX) < cap(c.chanTX), ctx.Err() |
| 313 | + } |
| 314 | + |
| 315 | + // Unbuffered channel: |
| 316 | + select { |
| 317 | + case <-c.chanClose: |
| 318 | + return false, io.EOF |
| 319 | + case c.chanTX <- []byte{}: |
| 320 | + return true, nil |
| 321 | + case <-ctx.Done(): |
| 322 | + return false, ctx.Err() |
| 323 | + } |
| 324 | +} |
0 commit comments