Skip to content

Commit 953b8fb

Browse files
committed
build: vendor packages
* github.com/gaukas/[email protected] => github.com/refraction-networking/water/internal/wazerofs * github.com/gaukas/[email protected] => github.com/refraction-networking/water/internal/io Signed-off-by: Gaukas Wang <[email protected]>
1 parent d4faf1b commit 953b8fb

27 files changed

+888
-514
lines changed

core.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
"github.com/tetratelabs/wazero/experimental/sys"
1818
"github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1"
1919

20-
"github.com/gaukas/wazerofs/memfs"
20+
"github.com/refraction-networking/water/internal/wazerofs/memfs"
2121
expsysfs "github.com/tetratelabs/wazero/experimental/sysfs"
2222
)
2323

go.mod

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ retract (
1010
replace github.com/tetratelabs/wazero => github.com/refraction-networking/wazero v1.7.3-w
1111

1212
require (
13-
github.com/gaukas/wazerofs v0.1.0
13+
github.com/blang/vfs v1.0.0
1414
github.com/tetratelabs/wazero v1.7.3
15-
google.golang.org/protobuf v1.33.0
15+
google.golang.org/protobuf v1.34.2
1616
)
17-
18-
require github.com/blang/vfs v1.0.0 // indirect

go.sum

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
github.com/blang/vfs v1.0.0 h1:AUZUgulCDzbaNjTRWEP45X7m/J10brAptZpSRKRZBZc=
22
github.com/blang/vfs v1.0.0/go.mod h1:jjuNUc/IKcRNNWC9NUCvz4fR9PZLPIKxEygtPs/4tSI=
3-
github.com/gaukas/wazerofs v0.1.0 h1:wIkW1bAxSnpaaVkQ5LOb1tm1BXdVap3eKjJpVWIqt2E=
4-
github.com/gaukas/wazerofs v0.1.0/go.mod h1:+JECB9Fwt0taPqSgHckG9lmT3tcoVK+9VJozTsq9UlI=
53
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
64
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
75
github.com/refraction-networking/wazero v1.7.3-w h1:Br3UuVPrKAD3pUSIlpT1+iBIYMbs8h2wS4d0ziU9Yoc=
86
github.com/refraction-networking/wazero v1.7.3-w/go.mod h1:ytl6Zuh20R/eROuyDaGPkp82O9C/DJfXAwJfQ3X6/7Y=
97
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
108
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
11-
google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI=
12-
google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
9+
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=
10+
google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw=

internal/io/LICENSE

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
BSD 3-Clause License
2+
3+
Copyright (c) 2018, the respective contributors, as shown by the AUTHORS file.
4+
All rights reserved.
5+
6+
Redistribution and use in source and binary forms, with or without
7+
modification, are permitted provided that the following conditions are met:
8+
9+
* Redistributions of source code must retain the above copyright notice, this
10+
list of conditions and the following disclaimer.
11+
12+
* Redistributions in binary form must reproduce the above copyright notice,
13+
this list of conditions and the following disclaimer in the documentation
14+
and/or other materials provided with the distribution.
15+
16+
* Neither the name of the copyright holder nor the names of its
17+
contributors may be used to endorse or promote products derived from
18+
this software without specific prior written permission.
19+
20+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

internal/io/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# io: Easy to use I/O object and function implementations
2+
3+
Copied from `github.com/gaukas/[email protected]`.

internal/io/conn/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## package `io/conn`
2+
3+
This package provides abstractions for connections build on top of other types.

internal/io/conn/channelconn.go

Lines changed: 324 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,324 @@
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

Comments
 (0)