github.com/puellanivis/breton@v0.2.16/lib/files/socketfiles/option.go (about)

     1  package socketfiles
     2  
     3  import (
     4  	"github.com/puellanivis/breton/lib/files"
     5  )
     6  
     7  // WithIgnoreErrors means that a write to a given files.File will silently drop errors.
     8  //
     9  // Requires the files.File to implement `interface{ IgnoreErrors(bool) bool }`, or else no action is taken.
    10  //
    11  // Really only useful for writing to Broadcast or UDP addresses before they are opened by a listener.
    12  func WithIgnoreErrors(state bool) files.Option {
    13  	type errorIgnorer interface {
    14  		IgnoreErrors(bool) bool
    15  	}
    16  
    17  	return func(f files.File) (files.Option, error) {
    18  		var save bool
    19  
    20  		if w, ok := f.(errorIgnorer); ok {
    21  			save = w.IgnoreErrors(state)
    22  		}
    23  
    24  		return WithIgnoreErrors(save), nil
    25  	}
    26  }
    27  
    28  // WithPacketSize chunks each Write to a specified size.
    29  //
    30  // Requires the files.File to implement `interface{ SetPacketSize(int) int }`, or else no action is taken.
    31  func WithPacketSize(sz int) files.Option {
    32  	type packetSizeSetter interface {
    33  		SetPacketSize(int) int
    34  	}
    35  
    36  	return func(f files.File) (files.Option, error) {
    37  		var save int
    38  
    39  		if w, ok := f.(packetSizeSetter); ok {
    40  			save = w.SetPacketSize(sz)
    41  		}
    42  
    43  		return WithPacketSize(save), nil
    44  	}
    45  }