Skip to content

Commit

Permalink
Resize fifo to 1024K
Browse files Browse the repository at this point in the history
Signed-off-by: kkkkun <[email protected]>
  • Loading branch information
kkkkun committed Mar 22, 2024
1 parent 3e17f98 commit 81d32d8
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
9 changes: 9 additions & 0 deletions fifo.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ type fifo struct {
handle *handle
}

const (
F_SETPIPE_SZ = 1031
)

const FIFO_SIZE = 1024 * 1024

var leakCheckWg *sync.WaitGroup

// OpenFifoDup2 is same as OpenFifo, but additionally creates a copy of the FIFO file descriptor with dup2 syscall.
Expand Down Expand Up @@ -163,6 +169,9 @@ func openFifo(ctx context.Context, fn string, flag int, perm os.FileMode) (*fifo
})
return
}
// Resize fifo to 1024K
// https://github.com/torvalds/linux/commit/ff9da691c0498ff81fdd014e7a0731dab2337dac
unix.FcntlInt(file.Fd(), F_SETPIPE_SZ, FIFO_SIZE)
f.file = file
close(f.opened)
}()
Expand Down
33 changes: 33 additions & 0 deletions fifo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -497,3 +497,36 @@ func checkWgDone(wg *sync.WaitGroup) error {
return ctx.Err()
}
}

func TestFifoResize(t *testing.T) {
tmpdir, err := os.MkdirTemp("", "fifos")
assert.NoError(t, err)
defer os.RemoveAll(tmpdir)

leakCheckWg = &sync.WaitGroup{}
defer func() {
leakCheckWg = nil
}()

ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()

r, err := OpenFifo(ctx, filepath.Join(tmpdir, "f0"), syscall.O_RDONLY|syscall.O_CREAT|syscall.O_NONBLOCK, 0600)
assert.NoError(t, err)

w, err := OpenFifo(ctx, filepath.Join(tmpdir, "f0"), syscall.O_WRONLY|syscall.O_NONBLOCK, 0)
assert.NoError(t, err)

size := 1024*1024
bytes := make([]byte, size)
for i := 0; i < size; i++ {
bytes[i] = byte(i % 256)
}
_, err = w.Write(bytes)
assert.NoError(t, err)

b := make([]byte, size)
_, err = r.Read(b[:size])
assert.NoError(t, err)
assert.Equal(t, b[:size], bytes)
}

0 comments on commit 81d32d8

Please sign in to comment.