cluster_packet.go 939 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package mhayaProto
  2. import (
  3. "fmt"
  4. "sync"
  5. "time"
  6. )
  7. var (
  8. clusterPacketPool = &sync.Pool{
  9. New: func() interface{} {
  10. return new(ClusterPacket)
  11. },
  12. }
  13. )
  14. func GetClusterPacket() *ClusterPacket {
  15. pkg := clusterPacketPool.Get().(*ClusterPacket)
  16. pkg.BuildTime = time.Now().UnixMilli()
  17. return pkg
  18. }
  19. func BuildClusterPacket(source, target, funcName string) *ClusterPacket {
  20. clusterPacket := GetClusterPacket()
  21. clusterPacket.SourcePath = source
  22. clusterPacket.TargetPath = target
  23. clusterPacket.FuncName = funcName
  24. return clusterPacket
  25. }
  26. func (x *ClusterPacket) Recycle() {
  27. x.BuildTime = 0
  28. x.SourcePath = ""
  29. x.TargetPath = ""
  30. x.FuncName = ""
  31. x.ArgBytes = nil
  32. x.Session = nil
  33. clusterPacketPool.Put(x)
  34. }
  35. func (x *ClusterPacket) PrintLog() string {
  36. return fmt.Sprintf("source = %s, target = %s, funcName = %s, bytesLen = %d, session = %v",
  37. x.SourcePath,
  38. x.TargetPath,
  39. x.FuncName,
  40. len(x.ArgBytes),
  41. x.Session,
  42. )
  43. }