-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
//go:build darwin | ||
|
||
package consumer | ||
|
||
import ( | ||
"io" | ||
|
||
"github.com/replicate/pget/pkg/logging" | ||
) | ||
|
||
var _ Consumer = &VMSpliceConsumer{} | ||
|
||
type VMSpliceConsumer struct { | ||
} | ||
|
||
func (v VMSpliceConsumer) Consume(reader io.Reader, destPath string) error { | ||
logger := logging.GetLogger() | ||
logger.Warn().Msg("'vmsplice' is not supported on darwin, falling back to StdoutConsumer") | ||
return StdoutConsumer{}.Consume(reader, destPath) | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
//go:build linux | ||
|
||
package consumer | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"os" | ||
"syscall" | ||
"unsafe" | ||
|
||
"github.com/dustin/go-humanize" | ||
) | ||
|
||
var _ Consumer = &VMSpliceConsumer{} | ||
Check failure on line 15 in pkg/consumer/linux_vmsplice.go GitHub Actions / Lint
Check failure on line 15 in pkg/consumer/linux_vmsplice.go GitHub Actions / Lint
Check failure on line 15 in pkg/consumer/linux_vmsplice.go GitHub Actions / Lint
Check failure on line 15 in pkg/consumer/linux_vmsplice.go GitHub Actions / build
Check failure on line 15 in pkg/consumer/linux_vmsplice.go GitHub Actions / Test
Check failure on line 15 in pkg/consumer/linux_vmsplice.go GitHub Actions / Test
|
||
|
||
type VMSpliceConsumer struct{} | ||
|
||
func (v VMSpliceConsumer) Consume(reader io.Reader, destPath string) error { | ||
// Create the buffer once and reuse it, this is zero additional allocations | ||
buffer := make([]byte, humanize.MiByte) | ||
for { | ||
length, err := reader.Read(buffer) | ||
if err != nil { | ||
if err == io.EOF { | ||
return nil | ||
} | ||
return fmt.Errorf("error reading from reader: %w", err) | ||
} | ||
if length > 0 { | ||
_, _, err := syscall.Syscall6( | ||
syscall.SYS_VMSPLICE, | ||
os.Stdout.Fd(), | ||
uintptr(unsafe.Pointer( | ||
&syscall.Iovec{ | ||
Base: &buffer[0], | ||
Len: uint64(length), | ||
})), 1, 0, 0, 0) | ||
if err != 0 { | ||
return fmt.Errorf("error splicing %s to stdout: %w", buffer, err) | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package consumer | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"os" | ||
) | ||
|
||
var _ Consumer = &StdoutConsumer{} | ||
Check failure on line 9 in pkg/consumer/stdout.go GitHub Actions / Lint
Check failure on line 9 in pkg/consumer/stdout.go GitHub Actions / Lint
Check failure on line 9 in pkg/consumer/stdout.go GitHub Actions / Lint
Check failure on line 9 in pkg/consumer/stdout.go GitHub Actions / build
Check failure on line 9 in pkg/consumer/stdout.go GitHub Actions / Test
|
||
|
||
type StdoutConsumer struct { | ||
} | ||
|
||
func (s StdoutConsumer) Consume(reader io.Reader, destPath string) error { | ||
_, err := io.Copy(os.Stdout, reader) | ||
if err != nil { | ||
return fmt.Errorf("error writing to stdout: %w", err) | ||
} | ||
return nil | ||
} |