Files

66 lines
1.3 KiB
Go
Raw Permalink Normal View History

//go:build linux || freebsd
// +build linux freebsd
package core
import (
"context"
"time"
)
// RequestContext holds metadata about the request across layers
type RequestContext struct {
// Request ID for logging
RequestID string
// Start of operation
StartTime time.Time
// Protocols used in the chain
ProtocolChain []string
// IP address to which was connected
ConnectedIP string
// IP version (4 or 6)
IPVersion int
// Number of attempts
AttemptCount int
// Context for cancellation
Context context.Context
}
// NewRequestContext creates a new request context
func NewRequestContext(ctx context.Context, requestID string) *RequestContext {
return &RequestContext{
RequestID: requestID,
StartTime: time.Now(),
ProtocolChain: make([]string, 0),
IPVersion: 0,
AttemptCount: 0,
Context: ctx,
}
}
// Duration returns the duration of operation
func (rc *RequestContext) Duration() time.Duration {
return time.Since(rc.StartTime)
}
// AddProtocol adds a protocol to the chain
func (rc *RequestContext) AddProtocol(protocol string) {
rc.ProtocolChain = append(rc.ProtocolChain, protocol)
}
// IsCancelled returns whether the context was cancelled
func (rc *RequestContext) IsCancelled() bool {
select {
case <-rc.Context.Done():
return true
default:
return false
}
}