github.com/mutagen-io/mutagen@v0.18.0-rc1/pkg/synchronization/endpoint/remote/rsync.go (about)

     1  package remote
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/mutagen-io/mutagen/pkg/encoding"
     7  	"github.com/mutagen-io/mutagen/pkg/stream"
     8  	"github.com/mutagen-io/mutagen/pkg/synchronization/rsync"
     9  )
    10  
    11  // protobufRsyncEncoder implements rsync.Encoder using Protocol Buffers.
    12  type protobufRsyncEncoder struct {
    13  	// encoder is the underlying Protocol Buffers encoder.
    14  	encoder *encoding.ProtobufEncoder
    15  	// flusher flushes the underlying stream.
    16  	flusher stream.Flusher
    17  	// error stores any previously encountered transmission error.
    18  	error error
    19  }
    20  
    21  // Encode implements rsync.Encoder.Encode.
    22  func (e *protobufRsyncEncoder) Encode(transmission *rsync.Transmission) error {
    23  	// Check for previous errors.
    24  	if e.error != nil {
    25  		return fmt.Errorf("previous error encountered: %w", e.error)
    26  	}
    27  
    28  	// Encode the transmission.
    29  	e.error = e.encoder.Encode(transmission)
    30  	return e.error
    31  }
    32  
    33  // Finalize implements rsync.Encoder.Finalize.
    34  func (e *protobufRsyncEncoder) Finalize() error {
    35  	// If an error has occurred, then there's nothing to do.
    36  	if e.error != nil {
    37  		return nil
    38  	}
    39  
    40  	// Otherwise, attempt to flush the compressor.
    41  	if err := e.flusher.Flush(); err != nil {
    42  		return fmt.Errorf("unable to flush encoded messages: %w", err)
    43  	}
    44  
    45  	// Success.
    46  	return nil
    47  }
    48  
    49  // protobufRsyncDecoder implements rsync.Decoder using Protocol Buffers.
    50  type protobufRsyncDecoder struct {
    51  	// decoder is the underlying Protocol Buffers decoder.
    52  	decoder *encoding.ProtobufDecoder
    53  }
    54  
    55  // Decode implements rsync.Decoder.Decode.
    56  func (d *protobufRsyncDecoder) Decode(transmission *rsync.Transmission) error {
    57  	// TODO: This is not particularly efficient because the Protocol Buffers
    58  	// decoding implementation doesn't reuse existing capacity in operation data
    59  	// buffers. This is something that needs to be fixed upstream, but we should
    60  	// file an issue. Once it's done, nothing on our end needs to change except
    61  	// to update the Protocol Buffers runtime.
    62  	return d.decoder.Decode(transmission)
    63  }
    64  
    65  // Finalize implements rsync.Decoder.Finalize.
    66  func (d *protobufRsyncDecoder) Finalize() error {
    67  	return nil
    68  }