T
T
tioffs2018-12-09 16:27:02
go
tioffs, 2018-12-09 16:27:02

Go socket wild brakes 5 connections how so?

In general, the socket launches a command every second in Laravel, but it instantly works out less than 300ms.
Receives data from memcache and pushes them to the channels.
Server VDS 4gb RAM 2 cores, it can't fail because of 5 connections like this

var upgrader = websocket.Upgrader{
  CheckOrigin:     func(r *http.Request) bool { return true },
  ReadBufferSize:  8192,
  WriteBufferSize: 8192,
}
var clients = make(map[*websocket.Conn]bool)
var mc = memcache.New("127.0.0.1:11211")

func echoHandler(w http.ResponseWriter, r *http.Request) {
  conn, err := upgrader.Upgrade(w, r, nil)
  if err != nil {
    return
  }
  clients[conn] = true
  mapD := map[string]interface{}{"user": len(clients)}
  mapB, _ := json.Marshal(mapD)
  for {
    for client := range clients {
      ErrorSendOnline := client.WriteMessage(1, mapB)
      if ErrorSendOnline != nil {
        client.Close()
        delete(clients, client)
      }

    }
    messageType, p, err := conn.ReadMessage()
    if err != nil {
      return
    }
    err = conn.WriteMessage(messageType, p)
    if err != nil {
      return
    }

  }
}

func getCache() {
  c := time.Tick(1000 * time.Millisecond)
  for range c {

    exec.Command("php", "/var/www/elrush/artisan", "commandMe").Run()
  }
}
func getData() {
  c := time.Tick(1000 * time.Millisecond)
  for range c {
    if len(clients) > 0 {
      val, errorMc := mc.Get("tem")
      for client := range clients {
        if errorMc == nil {
          errorSendCache := client.WriteMessage(1, val.Value)
          if errorSendCache != nil {
            client.Close()
            delete(clients, client)
          }
          mapD := map[string]interface{}{"user": len(clients)}
          mapB, _ := json.Marshal(mapD)
          ErrorSendOnline := client.WriteMessage(1, mapB)
          if ErrorSendOnline != nil {
            client.Close()
            delete(clients, client)
          }
        }
      }

      if errorMc == nil {
        mc.Delete("item")
      }

    }
}

func main() {
  http.HandleFunc("/", echoHandler)
       go getCache() //горутина
       go getData() //горутина
  err := http.ListenAndServe(":8080", nil)
  if err != nil {
    panic("Error: " + err.Error())
  }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
tioffs, 2018-12-10
@tioffs

The issue is resolved, all the brakes are due to memcached, it copes very badly with the automatic deletion of keys after N time, if you use just updating the keys, then everything is fine.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question