Documentation
¶
Index ¶
- Constants
- Variables
- func Configure(config Config, debugLog bool)
- func ExampleMessageHandler(addr *net.UDPAddr, message []byte) error
- func FragmentData(data []byte) ([][]byte, error)
- func GetUDPReadBufferPool() *sync.Pool
- func InitializeDefaultConfig()
- func ReassembleData(packet []byte) ([]byte, error)
- type CondLogger
- type Config
- type FragmentHeader
- type ReassemblyError
- type UDPClientConn
- func (c *UDPClientConn) Close() error
- func (c *UDPClientConn) LocalAddr() net.Addr
- func (c *UDPClientConn) Read(b []byte) (n int, err error)
- func (c *UDPClientConn) RemoteAddr() net.Addr
- func (c *UDPClientConn) SetDeadline(t time.Time) error
- func (c *UDPClientConn) SetReadDeadline(t time.Time) error
- func (c *UDPClientConn) SetWriteDeadline(t time.Time) error
- func (c *UDPClientConn) Write(b []byte) (n int, err error)
Constants ¶
const ( DefaultMaxFragmentSize = 1400 - FragmentHeaderSize // Typical Ethernet MTU - UDP Header - Frag Header DefaultReassemblyTimeout = 5 * time.Second DefaultCleanupInterval = 10 * time.Second DefaultMaxConcurrentReassemblies = 10000 // Default limit for concurrent reassemblies MaxUDPSize = 65507 // Maximum standard UDP packet size (IPv4) FragmentHeaderSize = 10 // Size in bytes of the fragmentation header )
Default configuration values.
const (
FlagLastFragment uint16 = 1 << 0 // Flag bit to indicate the last fragment
)
Flags for FragmentHeader.
Variables ¶
var ErrReassemblyLimitExceeded = &ReassemblyError{Message: "maximum concurrent reassemblies limit reached"}
Specific error for exceeding the reassembly limit.
Functions ¶
func Configure ¶
Configure allows setting up the package with a custom configuration and debug logging.
func ExampleMessageHandler ¶
ExampleMessageHandler (No changes needed)
func FragmentData ¶
FragmentData divides the data into UDP fragments based on the configured MaxFragmentSize. (No changes needed in FragmentData for sharding)
func GetUDPReadBufferPool ¶ added in v1.1.0
GetUDPReadBufferPool returns the package's UDP read buffer pool.
func InitializeDefaultConfig ¶
func InitializeDefaultConfig()
InitializeDefaultConfig initializes the global configuration with default values.
func ReassembleData ¶
ReassembleData attempts to reassemble UDP fragments into the original data. Uses sharding to reduce mutex contention.
Types ¶
type CondLogger ¶ added in v1.1.0
type CondLogger struct {
// contains filtered or unexported fields
}
CondLogger provides conditional logging based on a boolean flag.
func NewCondLogger ¶ added in v1.1.0
NewCondLogger creates a new conditional logger.
func (*CondLogger) Printf ¶ added in v1.1.0
func (cl *CondLogger) Printf(format string, v ...interface{})
Printf logs only if debug logging is enabled.
func (*CondLogger) Println ¶ added in v1.1.0
func (cl *CondLogger) Println(v ...interface{})
Println logs only if debug logging is enabled.
func (*CondLogger) SetDebug ¶ added in v1.1.0
func (cl *CondLogger) SetDebug(debug bool)
SetDebug enables or disables debug logging.
type Config ¶
type Config struct {
MaxFragmentSize int // Maximum size of data payload in a fragment (after header)
ReassemblyTimeout time.Duration // Timeout after which reassembly of a message is abandoned
CleanupInterval time.Duration // Interval for periodic cleanup of timed-out reassembly buffers
MaxConcurrentReassemblies int64 // Maximum number of messages being reassembled simultaneously across all shards (limits total map entries) - Changed to int64 for atomic
}
Config structure to hold configuration parameters for the fragmentation/reassembly package.
type FragmentHeader ¶
type FragmentHeader struct {
MessageID uint32 // Unique identifier of the fragmented message
FragmentNumber uint16 // Fragment number (starts at 0)
TotalFragments uint16 // Total number of fragments for this message
Flags uint16 // Flags (e.g., last fragment)
}
FragmentHeader represents the header added to each UDP fragment.
type ReassemblyError ¶
type ReassemblyError struct {
Message string
}
ReassemblyError is a custom error type for reassembly related errors.
func (*ReassemblyError) Error ¶
func (e *ReassemblyError) Error() string
type UDPClientConn ¶ added in v1.1.0
type UDPClientConn struct {
// contains filtered or unexported fields
}
UDPClientConn provides a net.Conn interface over a UDP connection, handling fragmentation and reassembly automatically.
func DialUDPFrag ¶ added in v1.1.0
func DialUDPFrag(network, address string, logger *CondLogger) (*UDPClientConn, error)
DialUDPFrag establishes a "connected" UDP socket to the remote address and returns a UDPClientConn that handles fragmentation/reassembly. If logger is nil, the package's default logger will be used.
func (*UDPClientConn) Close ¶ added in v1.1.0
func (c *UDPClientConn) Close() error
Close closes the connection and stops the background reader.
func (*UDPClientConn) LocalAddr ¶ added in v1.1.0
func (c *UDPClientConn) LocalAddr() net.Addr
LocalAddr returns the local network address.
func (*UDPClientConn) Read ¶ added in v1.1.0
func (c *UDPClientConn) Read(b []byte) (n int, err error)
Read reads a fully reassembled message from the connection.
func (*UDPClientConn) RemoteAddr ¶ added in v1.1.0
func (c *UDPClientConn) RemoteAddr() net.Addr
RemoteAddr returns the remote network address.
func (*UDPClientConn) SetDeadline ¶ added in v1.1.0
func (c *UDPClientConn) SetDeadline(t time.Time) error
SetDeadline sets the read and write deadlines associated with the connection.
func (*UDPClientConn) SetReadDeadline ¶ added in v1.1.0
func (c *UDPClientConn) SetReadDeadline(t time.Time) error
SetReadDeadline sets the deadline for future Read calls. A zero value for t means Read will not time out.
func (*UDPClientConn) SetWriteDeadline ¶ added in v1.1.0
func (c *UDPClientConn) SetWriteDeadline(t time.Time) error
SetWriteDeadline sets the deadline for future Write calls. A zero value for t means Write will not time out.