-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c87fb67
commit 7e83587
Showing
5 changed files
with
92 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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{} | ||
|
||
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) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package consumer | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"os" | ||
) | ||
|
||
var _ Consumer = &StdoutConsumer{} | ||
|
||
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 | ||
} |