github.com/emcfarlane/larking@v0.0.0-20220605172417-1704b45ee6c3/stats.go (about)

     1  // Copyright 2022 Edward McFarlane. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package larking
     6  
     7  import (
     8  	"time"
     9  
    10  	"google.golang.org/grpc/stats"
    11  )
    12  
    13  const (
    14  	payloadLen = 1
    15  	sizeLen    = 4
    16  	headerLen  = payloadLen + sizeLen
    17  )
    18  
    19  func outPayload(client bool, msg interface{}, data, payload []byte, t time.Time) *stats.OutPayload {
    20  	return &stats.OutPayload{
    21  		Client:     client,
    22  		Payload:    msg,
    23  		Data:       data,
    24  		Length:     len(data),
    25  		WireLength: len(payload) + headerLen,
    26  		SentTime:   t,
    27  	}
    28  }
    29  
    30  func inPayload(client bool, msg interface{}, data, payload []byte, t time.Time) *stats.InPayload {
    31  	return &stats.InPayload{
    32  		Client:     true,
    33  		RecvTime:   t,
    34  		Payload:    msg,
    35  		Data:       data,
    36  		Length:     len(data),
    37  		WireLength: len(payload) + headerLen,
    38  	}
    39  }
    40  
    41  // strAddr is a net.Addr backed by either a TCP "ip:port" string, or
    42  // the empty string if unknown.
    43  type strAddr string
    44  
    45  func (a strAddr) Network() string {
    46  	if a != "" {
    47  		// Per the documentation on net/http.Request.RemoteAddr, if this is
    48  		// set, it's set to the IP:port of the peer (hence, TCP):
    49  		// https://golang.org/pkg/net/http/#Request
    50  		//
    51  		// If we want to support Unix sockets later, we can
    52  		// add our own grpc-specific convention within the
    53  		// grpc codebase to set RemoteAddr to a different
    54  		// format, or probably better: we can attach it to the
    55  		// context and use that from serverHandlerTransport.RemoteAddr.
    56  		return "tcp"
    57  	}
    58  	return ""
    59  }
    60  func (a strAddr) String() string { return string(a) }