Saturday, March 2, 2024
HomeGolangGolang Gorilla Websocket fails when used concurrently - Getting Assist

Golang Gorilla Websocket fails when used concurrently – Getting Assist


I’ve an app which makes use of the Gorilla Websockets at: “GitHub – gorilla/websocket: Package deal gorilla/websocket is a quick, well-tested and extensively used WebSocket implementation for Go.
My app opens the connection, sends an inventory of inventory symbols then, in a loop, reads the messages returned by that connection till it’s cancelled or errors out.
Utilizing Go routines, I wish to monitor a number of lists of shares concurrently. The error happens when the 2nd connection is opened and the primary connection is studying messages. In my code I determine the routines by iterationNumber and because of the manner go routines work there isn’t any assure which connection will likely be tried first. I get an error once I run this, so I extracted the required code to exemplify the problem. I’m fairly new to net sockets, so I’m in all probability simply doing one thing incorrectly. I’ll ship the instance code and outcomes:

bundle foremost

import (
	"fmt"
	"github.com/gorilla/websocket"
	"web/url"
	"sync"
)

kind Inventory struct {
	Image string `json:"image"`
	Sort   string `json:"kind"`
}

func foremost() {
	// URL and token for the WebSocket connection

	var wg *sync.WaitGroup = &sync.WaitGroup{}

	for i := 0; i < 2; i++ {
		wg.Add(1)
		go processStocks(i, wg)
	}

	wg.Wait()
}

func processStocks(interationNumber int, wg *sync.WaitGroup) {
	iterationStr := fmt.Sprintf("%d", interationNumber)

	u := url.URL{Scheme: "wss", Host: "ws.finnhub.io", RawQuery: "token=cbbb00iad3ibhoa1vbcg"}

	fmt.Printf("start processStocks() for iteration: %sn", iterationStr)

	// Checklist of inventory symbols
	shares := []Inventory{
		{"IBM", "subscribe"},
		// Add extra shares right here
	}

	defer wg.Performed()

	// Create a brand new WebSocket connection
	fmt.Printf("start Dial() for iteration: %sn", iterationStr)
	c, _, err := websocket.DefaultDialer.Dial(u.String(), nil)
	if err != nil {
		//cAddress := fmt.Sprintf("%p", c)
		fmt.Printf("failed dial error for iteration:%s deal with:%p: %vn", iterationStr, c, err)
	} else {
		fmt.Printf("profitable connection at deal with: %p for iteration: %s n", c, iterationStr)
	}
	defer func(c *websocket.Conn) {
		err := c.Shut()
		if err != nil {
			fmt.Printf("iteration %s Shut error: %vn", iterationStr, err)
		}
	}(c)

	// Ship the listing of inventory symbols
	for _, inventory := vary shares {
		fmt.Printf("start WriteJSON() for iteration: %s deal with: %pn", iterationStr, c)
		err := c.WriteJSON(inventory)
		if err != nil {
			fmt.Printf("iteration %s write error: %vn", iterationStr, err)
			return
		} else {
			fmt.Printf("profitable write at deal with: %p for iteration: %s n", c, iterationStr)
		}

	}

	// Limitless loop to learn the outcomes
	for {
		fmt.Printf("start ReadMessage() for iteration:%s deal with:%pn", iterationStr, c)
		_, message, err := c.ReadMessage()
		if err != nil {
			fmt.Printf("iteration %s learn error: %v deal with:%p: n", iterationStr, err, c)
			return
		} else {
			fmt.Printf("iteration %s profitable learn: [%s] deal with:%p: n", iterationStr, message, c)
		}
	}
}

the output:

start processStocks() for iteration: 1
start Dial() for iteration: 1
start processStocks() for iteration: 0
start Dial() for iteration: 0
profitable connection at deal with: 0x14000012580 for iteration: 0 
start WriteJSON() for iteration: 0 deal with: 0x14000012580
profitable write at deal with: 0x14000012580 for iteration: 0 
start ReadMessage() for iteration:0 deal with:0x14000012580

*iteration 0 learn error: websocket: shut 1006 (irregular closure): surprising EOF deal with:0x14000012580:* 

profitable connection at deal with: 0x140000126e0 for iteration: 1 
start WriteJSON() for iteration: 1 deal with: 0x140000126e0
profitable write at deal with: 0x140000126e0 for iteration: 1 
start ReadMessage() for iteration:1 deal with:0x140000126e0
iteration 1 profitable learn: [{"data":[{"c":["1","12"],"p":144.995,"s":"IBM","t":1691083530560,"v":1}],"kind":"commerce"}] deal with:0x140000126e0: 
start ReadMessage() for iteration:1 deal with:0x140000126e0

BTW, this has my key in it so you possibly can recreate the problem, however I will likely be altering it quickly and it’s free anyway.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments