github.com/geniusesgroup/libgo@v0.0.0-20220713101832-828057a9d3d4/connection/connection-metric.go (about)

     1  /* For license and copyright information please see LEGAL file in repository */
     2  
     3  package connection
     4  
     5  import (
     6  	"sync/atomic"
     7  	"time"
     8  
     9  	"../protocol"
    10  )
    11  
    12  // Metric store the connection metric data and impelement protocol.ConnectionMetrics
    13  type Metric struct {
    14  	lastUsage                   int64  // Last use of this connection
    15  	maxBandwidth                uint64 // Byte/Second and Connection can limit to a fixed number
    16  	bytesSent                   uint64 // Counts the bytes of packets sent.
    17  	packetsSent                 uint64 // Counts sent packets.
    18  	bytesReceived               uint64 // Counts the bytes of packets receive.
    19  	packetsReceived             uint64 // Counts received packets.
    20  	failedPacketsReceived       uint64 // Counts failed packets receive for firewalling server from some attack types!
    21  	notRequestedPacketsReceived uint64 // Counts not requested packets received for firewalling server from some attack types!
    22  	succeedStreamCount          uint64 // Count successful request.
    23  	failedStreamCount           uint64 // Count failed services call e.g. data validation failed, ...
    24  }
    25  
    26  func (m *Metric) LastUsage() protocol.TimeUnixMilli   { return protocol.TimeUnixMilli(m.lastUsage) }
    27  func (m *Metric) MaxBandwidth() uint64                { return m.maxBandwidth }
    28  func (m *Metric) BytesSent() uint64                   { return m.bytesSent }
    29  func (m *Metric) PacketsSent() uint64                 { return m.packetsSent }
    30  func (m *Metric) BytesReceived() uint64               { return m.bytesReceived }
    31  func (m *Metric) PacketsReceived() uint64             { return m.packetsReceived }
    32  func (m *Metric) FailedPacketsReceived() uint64       { return m.failedPacketsReceived }
    33  func (m *Metric) NotRequestedPacketsReceived() uint64 { return m.notRequestedPacketsReceived }
    34  func (m *Metric) SucceedStreamCount() uint64          { return m.succeedStreamCount }
    35  func (m *Metric) FailedStreamCount() uint64           { return m.failedStreamCount }
    36  
    37  // StreamSucceed store successfull service call occur on this connection
    38  func (m *Metric) StreamSucceed() {
    39  	atomic.StoreInt64(&m.lastUsage, time.Now().Unix())
    40  	atomic.AddUint64(&m.succeedStreamCount, 1)
    41  	// TODO::: Any other job??
    42  }
    43  
    44  // StreamFailed store failed service request occur on this connection!
    45  // Base on the connection it can other action to prevent any attack! e.g. tel router to block
    46  func (m *Metric) StreamFailed() {
    47  	atomic.StoreInt64(&m.lastUsage, time.Now().Unix())
    48  	atomic.AddUint64(&m.failedStreamCount, 1)
    49  	// TODO::: Any other job??
    50  }
    51  
    52  func (m *Metric) PacketReceived(packetLength uint64) {
    53  	atomic.StoreInt64(&m.lastUsage, time.Now().Unix())
    54  	atomic.AddUint64(&m.packetsReceived, 1)
    55  	atomic.AddUint64(&m.bytesReceived, packetLength)
    56  }
    57  
    58  func (m *Metric) PacketSent(packetLength uint64) {
    59  	atomic.StoreInt64(&m.lastUsage, time.Now().Unix())
    60  	atomic.AddUint64(&m.packetsSent, 1)
    61  	atomic.AddUint64(&m.bytesSent, packetLength)
    62  }