tcpreader.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package gelf
  2. import (
  3. "bufio"
  4. "encoding/json"
  5. "fmt"
  6. "net"
  7. "time"
  8. )
  9. type TCPReader struct {
  10. listener *net.TCPListener
  11. conn net.Conn
  12. messages chan []byte
  13. }
  14. type connChannels struct {
  15. drop chan string
  16. confirm chan string
  17. }
  18. func newTCPReader(addr string) (*TCPReader, chan string, chan string, error) {
  19. var err error
  20. tcpAddr, err := net.ResolveTCPAddr("tcp", addr)
  21. if err != nil {
  22. return nil, nil, nil, fmt.Errorf("ResolveTCPAddr('%s'): %s", addr, err)
  23. }
  24. listener, err := net.ListenTCP("tcp", tcpAddr)
  25. if err != nil {
  26. return nil, nil, nil, fmt.Errorf("ListenTCP: %s", err)
  27. }
  28. r := &TCPReader{
  29. listener: listener,
  30. messages: make(chan []byte, 100), // Make a buffered channel with at most 100 messages
  31. }
  32. closeSignal := make(chan string, 1)
  33. doneSignal := make(chan string, 1)
  34. go r.listenUntilCloseSignal(closeSignal, doneSignal)
  35. return r, closeSignal, doneSignal, nil
  36. }
  37. func (r *TCPReader) accepter(connections chan net.Conn) {
  38. for {
  39. conn, err := r.listener.Accept()
  40. if err != nil {
  41. break
  42. }
  43. connections <- conn
  44. }
  45. }
  46. func (r *TCPReader) listenUntilCloseSignal(closeSignal chan string, doneSignal chan string) {
  47. defer func() { doneSignal <- "done" }()
  48. defer r.listener.Close()
  49. var conns []connChannels
  50. connectionsChannel := make(chan net.Conn, 1)
  51. go r.accepter(connectionsChannel)
  52. for {
  53. select {
  54. case conn := <-connectionsChannel:
  55. dropSignal := make(chan string, 1)
  56. dropConfirm := make(chan string, 1)
  57. channels := connChannels{drop: dropSignal, confirm: dropConfirm}
  58. go handleConnection(conn, r.messages, dropSignal, dropConfirm)
  59. conns = append(conns, channels)
  60. default:
  61. }
  62. select {
  63. case sig := <-closeSignal:
  64. if sig == "stop" || sig == "drop" {
  65. if len(conns) >= 1 {
  66. for _, s := range conns {
  67. if s.drop != nil {
  68. s.drop <- "drop"
  69. <-s.confirm
  70. conns = append(conns[:0], conns[1:]...)
  71. }
  72. }
  73. if sig == "stop" {
  74. return
  75. }
  76. } else if sig == "stop" {
  77. closeSignal <- "stop"
  78. }
  79. if sig == "drop" {
  80. doneSignal <- "done"
  81. }
  82. }
  83. default:
  84. }
  85. }
  86. }
  87. func (r *TCPReader) addr() string {
  88. return r.listener.Addr().String()
  89. }
  90. func handleConnection(conn net.Conn, messages chan<- []byte, dropSignal chan string, dropConfirm chan string) {
  91. defer func() { dropConfirm <- "done" }()
  92. defer conn.Close()
  93. reader := bufio.NewReader(conn)
  94. var b []byte
  95. var err error
  96. drop := false
  97. canDrop := false
  98. for {
  99. conn.SetDeadline(time.Now().Add(2 * time.Second))
  100. if b, err = reader.ReadBytes(0); err != nil {
  101. if drop {
  102. return
  103. }
  104. } else if len(b) > 0 {
  105. messages <- b
  106. canDrop = true
  107. if drop {
  108. return
  109. }
  110. } else if drop {
  111. return
  112. }
  113. select {
  114. case sig := <-dropSignal:
  115. if sig == "drop" {
  116. drop = true
  117. time.Sleep(1 * time.Second)
  118. if canDrop {
  119. return
  120. }
  121. }
  122. default:
  123. }
  124. }
  125. }
  126. func (r *TCPReader) readMessage() (*Message, error) {
  127. b := <-r.messages
  128. var msg Message
  129. if err := json.Unmarshal(b[:len(b)-1], &msg); err != nil {
  130. return nil, fmt.Errorf("json.Unmarshal: %s", err)
  131. }
  132. return &msg, nil
  133. }
  134. func (r *TCPReader) Close() {
  135. r.listener.Close()
  136. }