pool.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package main
  2. import (
  3. "sync"
  4. "time"
  5. )
  6. //气象环境,实时MQTT订阅
  7. //车速阈值,固定配置
  8. //车牌、车速,
  9. type EnvData struct {
  10. Time time.Time `json:"-"`
  11. Tianqi string `json:"tq"` //天气
  12. Wendu string `json:"wd"` //温度
  13. Zhishu string `json:"zs"` //控制质量
  14. Aqi string `json:"aqi"` //指数
  15. Fengxiang string `json:"fx"` //风向
  16. Fengji string `json:"fj"` //风急
  17. Shidu string `json:"sd"`//hidu
  18. PM25 string `json:"pm25"` //PM2.5
  19. PM10 string `json:"pm10"` //PM10
  20. }
  21. type DisplayData struct {
  22. EnvData
  23. Chesu int `json:"cs"` //测速
  24. Chesufazhi int `json:"csfz"` //测速阈值
  25. }
  26. func (o * DisplayData) Reset(){
  27. o.Chesu = 0
  28. o.Chesufazhi = 0
  29. }
  30. var _datapoolonce sync.Once
  31. var _datapoolsingle *DisplayDataPool
  32. func GetDataPool() *DisplayDataPool {
  33. _datapoolonce.Do(func() {
  34. _datapoolsingle = &DisplayDataPool{
  35. pool: sync.Pool{New: func() interface{} { return &DisplayData{} }}}
  36. })
  37. return _datapoolsingle
  38. }
  39. type DisplayDataPool struct {
  40. pool sync.Pool
  41. }
  42. func (o *DisplayDataPool) GetData() *DisplayData {
  43. p := o.pool.Get()
  44. return p.(*DisplayData)
  45. }
  46. func (o *DisplayDataPool) Release(p *DisplayData) {
  47. if p != nil {
  48. o.pool.Put(p)
  49. }
  50. }