Golang: Recursive function for reconnecting a TCP client... Bad idea? - go

I have this working TCP client code. When it fails to Write or Read on the TCP connection, it creates a new connection with the recursive function tcpReconnect().
Is this safe or will it fill up the RAM? It is possible that it may be trying to reconnect over several days (weekend or holidays). This is code is part of a Driver that monitors the state of an industrial Machine.
Maybe there is a better solution for this problem. I was not able to find one.
PS: I don't like polling
package main
import (
"fmt"
"net"
"time"
)
var pollTime = 1000 //ms
var host = "127.0.0.1"
var port = "11000"
func main() {
finished := make(chan bool)
go Driver()
<-finished
}
func tcpReconnect() net.Conn {
newConn, err := net.Dial("tcp", host+":"+port)
if err != nil {
fmt.Println("Failed to reconnect:", err.Error())
time.Sleep(time.Millisecond * time.Duration(2000))
newConn = tcpReconnect()
}
return newConn
}
func Driver() {
var conn net.Conn
conn, err := net.Dial("tcp", host+":"+port)
if err != nil {
fmt.Println("Failed to initialize Connection, trying to reconnect:", err.Error())
conn = tcpReconnect()
}
for {
_, err = conn.Write([]byte("11|5546|STATUS" + "\r\n"))
if err != nil {
println("Write to server failed:", err.Error())
println("Trying reset the connection...")
conn = tcpReconnect()
}
var replyBuffer = make([]byte, 256)
_, err = conn.Read(replyBuffer)
if err != nil {
println("Read from server failed:", err.Error())
println("Trying reset the connection...")
conn = tcpReConnect()
}
var reply string
for i, val := range replyBuffer {
if val == 13 { //13 is CR and marks the end of the message
reply = string(replyBuffer[:i])
break
}
}
fmt.Printf("reply from server=%s\n", reply)
time.Sleep(time.Millisecond * time.Duration(pollTime))
}
}

This is what I came up with. Credits go to #tkausl and #ThunderCat
func Driver() {
for {
conn, err := net.Dial("tcp", host+":"+port)
if err != nil {
fmt.Println("Failed to connect:", err.Error())
fmt.Println("Trying reset the connection...")
time.Sleep(time.Millisecond * time.Duration(2000))
} else {
for {
_, err = conn.Write([]byte("11|5546|STATUS" + "\r\n"))
if err != nil {
fmt.Println("Write to server failed:", err.Error())
fmt.Println("Trying reset the connection...")
break
}
var replyBuffer = make([]byte, 256)
_, err = conn.Read(replyBuffer)
if err != nil {
fmt.Println("Read from server failed:", err.Error())
fmt.Println("Trying reset the connection...")
break
}
var reply string
for i, val := range replyBuffer {
if val == 13 { //13 is CR and marks the end of the message
reply = string(replyBuffer[:i])
break
}
}
fmt.Printf("reply from server=_%s_\n", reply)
time.Sleep(time.Millisecond * time.Duration(pollTime))
}
}
}
}

Related

gobwas-ws clean conn close

I use high-level structures to create a ws server.
func wsHandler(w http.ResponseWriter, r *http.Request) {
re := regexp.MustCompile(`^\/ws\/([0-9a-zA-Z]+)\/*`)
match := re.FindStringSubmatch(r.URL.Path)
if len(match) != 2 {
http.NotFound(w, r)
return
}
conn, _, _, err := ws.UpgradeHTTP(r, w)
if err != nil {
log.Printf("gobwas/ws: %s", err)
}
go func() {
defer conn.Close()
channels.Lock()
channels.Channels[match[1]] = append(channels.Channels[match[1]], &conn)
channels.Unlock()
for {
msg, op, err := wsutil.ReadClientData(conn)
if err != nil {
if err != io.EOF {
log.Printf("gobwas/ws/wsutil: %s", err)
}
break
}
if len(msg) > 0 {
go sendMessage(&conn, op, match[1], msg)
}
}
deleteConn(&conn, match[1])
}()
}
Then, when I implement a graceful disconnection, then with the usual conn.Close (), the connection simply breaks outside the protocol. I use the following code to close:
func onShutdown() {
channels.RLock()
for _, channel := range channels.Channels {
for _, conn := range channel {
if err := ws.WriteFrame(*conn, ws.NewCloseFrame(ws.NewCloseFrameBody(ws.StatusNormalClosure, "Server shutdown"))); err != nil {
fmt.Println(err)
}
(*conn).Close()
}
}
channels.RUnlock()
}
And even if I wait for a response from the client and then close the connection, then anyway, the client fixes the disconnection. (The client is the browser and JS WebSocket).
Sample JS code:
var socket = new WebSocket(`${location.protocol === "https:" ? "wss:" : "ws:"}//${window.location.host}/ws/valid`);
socket.onclose = function (event) {
if (event.wasClean) {
console.log('Clean');
} else {
console.log('disconnect');
}
console.log('Code: ' + event.code + ' reason: ' + event.reason);
};

Simple server client communication not working

This seemingly simple example is not working as expected and I feel bad for asking but here goes:
There's a client that retries connecting to the server, sends a message, and then waits for a response:
func client() {
var conn net.Conn
var err error
// retry server until it is up
for {
conn, err = net.Dial("tcp", ":8081")
if err == nil {
break
}
log.Println(err)
time.Sleep(time.Second)
}
// write to server
_, err = conn.Write([]byte("request"))
if err != nil {
log.Println(err)
return
}
// block & read from server
var buf []byte
n, err := conn.Read(buf)
if err != nil {
log.Println(err)
return
}
log.Printf("From server: %s\n", buf[:n])
}
It connects to a server which for each connection, reads and interprets the sent data, and sends a response if needed:
func server() {
ln, _ := net.Listen("tcp", ":8081")
for {
conn, _ := ln.Accept()
go handleConn(conn)
}
}
func handleConn(conn net.Conn) {
var buf []byte
n, err := conn.Read(buf)
if err != nil {
return
}
log.Printf("Server got: %s\n", buf)
if string(buf[:n]) == "request" {
_, _ = conn.Write([]byte("response"))
}
}
All driven by the main function:
func main() {
go client()
server()
}
Error handling is omitted for brevity. The expected behavior is that the client will connect to the server and send the message "request" and then block on the read. The server receives "request" and sends the message "response" back to the same connection. The client unblock, prints the received message and exits. Instead, when the program is run, the following is printed:
2019/09/01 22:24:02 From server:
2019/09/01 22:24:02 Server got:
Suggesting that no data was exchanged, and that the client did not block.
The looping in client is strange!
The looping not make sense if read/write is out.
But the error is only this:
//var buf []byte <--- this read 0 bytes
buf := make([]byte, 1024)
n, err := conn.Read(buf)
A proposal for you:
package main
import (
"log"
"net"
"time"
)
func client() {
var conn net.Conn
var err error
// retry server until it is up
for {
log.Printf("Connecting...")
conn, err = net.Dial("tcp", ":8082")
if err != nil {
log.Println(err)
break
}
time.Sleep(time.Second)
// write to server
log.Printf("Writing...")
_, err = conn.Write([]byte("request"))
if err != nil {
log.Println(err)
return
}
// block & read from server
log.Printf("Reading...")
var buf []byte
n, err := conn.Read(buf)
if err != nil {
log.Println(err)
return
}
log.Printf("From server: %s\n", buf[:n])
}
}
func server() {
ln, _ := net.Listen("tcp", ":8082")
for {
conn, _ := ln.Accept()
go handleConn(conn)
}
}
func handleConn(conn net.Conn) {
buf := make([]byte, 1024)
n, err := conn.Read(buf)
if err != nil {
return
}
log.Printf("Server got: [%d bytes] %s\n", n, buf)
if string(buf[:n]) == "request" {
_, _ = conn.Write([]byte("response"))
}
conn.Close()
}
func main() {
go client()
server()
}

TCP connection returns 'broken pipe' error when used multiple times

This question relates to go and its net package.
I wrote a simple tcp server handles some RPC. the client is using a chan net.Conn to manage all tcp connection on the client side. Server is running with a tcp listener.
here's the code:
client:
package server
import (
"errors"
"log"
"net"
)
var tcpPool chan net.Conn
func NewClient(connections int, address string) {
tcpPool = make(chan net.Conn, connections)
for i := 0; i < connections; i++ {
conn, err := net.Dial("tcp4", address)
if err != nil {
log.Panic(err)
}
tcpPool <- conn
}
}
func SendMessage(msg []byte) ([]byte, error) {
conn := getConn()
log.Println("check conn: ", conn)
log.Println("msg: ", msg)
defer releaseConn(conn)
// send message
n, err := conn.Write(msg)
if err != nil {
log.Panic(err)
} else if n < len(msg) {
log.Panic(errors.New("Message did not send in full"))
}
// receiving a message
inBytes := make([]byte, 0)
for {
// bufsize 1024, read bufsize bytes each time
b := make([]byte, bufSize)
res, err := conn.Read(b)
log.Println("server sends >>>>>>>>>>>>: ", res)
if err != nil {
b[0] = ReError
break
}
inBytes = append(inBytes, b[:res]...)
// message finished.
if res < bufSize {
break
}
}
// check replied message
if len(inBytes) == 0 {
return []byte{}, errors.New("empty buffer error")
}
log.Println("SendMessage gets: ", inBytes)
return inBytes, nil
}
func releaseConn(conn net.Conn) error {
log.Println("return conn to pool")
select {
case tcpPool <- conn:
return nil
}
}
func getConn() (conn net.Conn) {
log.Println("Take one from pool")
select {
case conn := <-tcpPool:
return conn
}
}
server
func StartTCPServer(network, addr string) error {
listener, err := net.Listen(network, addr)
if err != nil {
return errors.Wrapf(err, "Unable to listen on address %s\n", addr)
}
log.Println("Listen on", listener.Addr().String())
defer listener.Close()
for {
log.Println("Accept a connection request.")
conn, err := listener.Accept()
if err != nil {
log.Println("Failed accepting a connection request:", err)
continue
}
log.Println("Handle incoming messages.")
go onConn(conn)
}
}
//onConn recieves a tcp connection and waiting for incoming messages
func onConn(conn net.Conn) {
inBytes := make([]byte, 0)
defer func() {
if e := recover(); e != nil {
//later log
if err, ok := e.(error); ok {
println("recover", err.Error())
}
}
conn.Close()
}()
// load msg
for {
buf := make([]byte, bufSize)
res, err := conn.Read(buf)
log.Println("server reading: ", res)
inBytes = append(inBytes, buf[:res]...)
if err != nil || res < bufSize {
break
}
}
var req RPCRequest
err := json.Unmarshal(inBytes, &req)
if err != nil {
log.Panic(err)
}
log.Println("rpc request: ", req)
var query UserRequest
err = json.Unmarshal(req.Query, &query)
if err != nil {
log.Panic(err)
}
log.Println("rpc request query: ", query)
// call method to process request
// good now we can proceed to function call
// some actual function calls gets a output
// outBytes, err := json.Marshal(out)
conn.Write(outBytes)
}
I think this is very standard. but for some reason, I can only send message on the client side one, and then the follow 2nd and 3rd start to show some irregularity.
1st ---> success, gets response
2nd ---> client can send but nothing gets back, logs on server side shows no in coming message
3rd ---> if I send from client side one more time, it shows broken pipe error..
There are some bad handling way.
First, the flag to insure the msg from server finished is depending on io.EOF,not length
// message finished.
if res < 512 {
break
}
instead of this, reader returns an io.EOF is the only symbol that shows message finished.
Second, chan type has its property to block and not need to use select.by the way, you really need to start a goroutine to release. The same requirement for getConn
func releaseConn(conn net.Conn) {
go func(){
tcpPool <- conn
}()
}
func getConn() net.Conn {
con := <-tcpPool
return con
}
Third, listener should not be close, code below is bad
defer listener.Close()
The most important reason is
on the client side,
res, err := conn.Read(b) this receive the reply from the server.
when nothing reply ,it block rather than io.EOF, nor some error else.
It means ,you cann't box a lasting communicating part into a function send().
You can do a single thing to use sendmsg() to send, but never use sendmsg() to handle the reply.
you can handle reply like this
var receive chan string
func init() {
receive = make(chan string, 10)
}
func ReceiveMessage(con net.Conn) {
// receiving a message
inBytes := make([]byte, 0, 1000)
var b = make([]byte, 512)
for {
// bufsize 1024, read bufsize bytes each time
res, err := con.Read(b)
if err != nil {
if err == io.EOF {
break
}
fmt.Println(err.Error())
break
}
inBytes = append(inBytes, b[:res]...)
msg := string(inBytes)
fmt.Println("receive msg from server:" + msg)
receive <- msg
}
}
I found several problem in your code, but I can't tell which one leads your failure.
This is my code according to what you write and did some fixing.
client.go:
package main
import (
"fmt"
"io"
"log"
"net"
)
var tcpPool chan net.Conn
var receive chan string
func init() {
receive = make(chan string, 10)
}
func NewClient(connections int, address string) {
tcpPool = make(chan net.Conn, connections)
for i := 0; i < connections; i++ {
conn, err := net.Dial("tcp", address)
if err != nil {
log.Panic(err)
}
tcpPool <- conn
}
}
func SendMessage(con net.Conn, msg []byte) error {
// send message
_, err := con.Write(msg)
if err != nil {
log.Panic(err)
}
return nil
}
func ReceiveMessage(con net.Conn) {
// receiving a message
inBytes := make([]byte, 0, 1000)
var b = make([]byte, 512)
for {
// bufsize 1024, read bufsize bytes each time
res, err := con.Read(b)
if err != nil {
if err == io.EOF {
break
}
fmt.Println(err.Error())
break
}
inBytes = append(inBytes, b[:res]...)
msg := string(inBytes)
fmt.Println("receive msg from server:" + msg)
receive <- msg
}
}
func getConn() net.Conn {
con := <-tcpPool
return con
}
func main() {
NewClient(20, "localhost:8101")
con := <-tcpPool
e := SendMessage(con, []byte("hello, i am client"))
if e != nil {
fmt.Println(e.Error())
return
}
go ReceiveMessage(con)
var msg string
for {
select {
case msg = <-receive:
fmt.Println(msg)
}
}
}
server.go
package main
import (
"fmt"
"io"
"net"
)
func StartTCPServer(network, addr string) error {
listener, err := net.Listen(network, addr)
if err != nil {
return err
}
for {
conn, err := listener.Accept()
if err != nil {
fmt.Println(err.Error())
continue
}
onConn(conn)
}
}
//onConn recieves a tcp connection and waiting for incoming messages
func onConn(conn net.Conn) {
inBytes := make([]byte, 0)
// load msg
for {
buf := make([]byte, 512)
res, err := conn.Read(buf)
if err != nil {
if err == io.EOF {
return
}
fmt.Println(err.Error())
return
}
inBytes = append(inBytes, buf[:res]...)
fmt.Println("receive from client:" + string(inBytes))
conn.Write([]byte("hello"))
}
}
func main() {
if e := StartTCPServer("tcp", ":8101"); e != nil {
fmt.Println(e.Error())
return
}
}
this works and no error.
By the way, I can't see where either on the client side or the server side you do con.Close(). It's nessasary to close it.This means a connection once got from the pool, you don't put it back. When you think a connection is over, then close it and build a new connection to fill the pool rather than put it back,beause it's a fatal operation to put a closed con back to the pool.

Golang amqp reconnect

I want to test the restart connection to the rabbitmq server.
On wrote small script to test.
http://play.golang.org/p/l3ZWzG0Qqb
But it's not working.
In step 10, I close the channel and connection. And open them again. And re-create chan amqp.Confirmation ( :75) . And continue the cycle.
But after that, from the chan confirms nothing return.
UPD: code here.
package main
import (
"fmt"
"github.com/streadway/amqp"
"log"
"os"
"time"
)
const SERVER = "amqp://user:pass#localhost:5672/"
const EXCHANGE_NAME = "publisher.test.1"
const EXCHANGE_TYPE = "direct"
const ROUTING_KEY = "publisher.test"
var Connection *amqp.Connection
var Channel *amqp.Channel
func setup(url string) (*amqp.Connection, *amqp.Channel, error) {
conn, err := amqp.Dial(url)
if err != nil {
return nil, nil, err
}
ch, err := conn.Channel()
if err != nil {
return nil, nil, err
}
return conn, ch, nil
}
func main() {
url := SERVER
Connection, Channel, err := setup(url)
if err != nil {
fmt.Println("err publisher setup:", err)
return
}
confirms := Channel.NotifyPublish(make(chan amqp.Confirmation, 1))
if err := Channel.Confirm(false); err != nil {
log.Fatalf("confirm.select destination: %s", err)
}
for i := 1; i <= 3000000; i++ {
log.Println(i)
if err != nil {
fmt.Println("err consume:", err)
return
}
if err := Channel.Publish(EXCHANGE_NAME, ROUTING_KEY, false, false, amqp.Publishing{
Body: []byte(fmt.Sprintf("%d", i)),
}); err != nil {
fmt.Println("err publish:", err)
log.Printf("%+v", err)
os.Exit(1)
return
}
// only ack the source delivery when the destination acks the publishing
confirmed := <-confirms
if confirmed.Ack {
log.Printf("confirmed delivery with delivery tag: %d", confirmed.DeliveryTag)
} else {
log.Printf("failed delivery of delivery tag: %d", confirmed.DeliveryTag)
// TODO. Reconnect will be here
}
if i == 10 {
Channel.Close()
Connection.Close()
while := true
for while {
log.Println("while")
time.Sleep(time.Second * 1)
Connection, Channel, err = setup(url)
if err == nil {
while = false
confirms = Channel.NotifyPublish(make(chan amqp.Confirmation, 1))
log.Printf("%+v", confirms)
}
}
}
time.Sleep(time.Millisecond * 300)
}
os.Exit(1)
}
You should put channel in confirm mode. by calling the channel.Confirm() method.
After closing the connection and even after getting new channel on the same connection, you should call Confirm() method again, since the channel is different from the old channel, and the default for all new channel is not to send confirm.

A function by type? How refactor it?

I'm reading json data from a file and sending it to a remote server using gob encoding,
but I'm not happy with my code, I tried several ways to get a more generic function, but I'm failed, the only way that my code works is having identical functions for every type.
I tried using switch for types, but in the same way is needed repeat code in order to unmarshall and encode gob data
Please, could somebody help me to understand how improve that?
Two types:
type Data1 struct{
ID int
Message string
}
type Data2 struct{
Serial int
Height float64
Loss float64
Temp float64
Oil float64
}
Function for Data1 type
func SenderData1(address string, buff *filebuffer.Buffer) {
var conn net.Conn
var err error
var line string
var obj Data1
for {
line, err = buff.Pop()
if err != nil {
log.Critical("Error Poping:", err.Error())
continue
}
if len(line) == 0 {
time.Sleep(1 * time.Second)
continue
}
if err := json.Unmarshal([]byte(line), &obj); err != nil {
log.Critical("Error Unmarshaling:", err.Error())
continue
}
for {
log.Info("Trying to connect with Server...")
conn, err = net.Dial(PROTO, address)
// If err try to connect again
if err != nil {
log.Error("Error connecting:", err.Error())
time.Sleep(1 * time.Second)
continue
}
// If connected break the loop
break
}
log.Debug("Sending ", obj, " to:", address)
encoder := gob.NewEncoder(conn)
err := encoder.Encode(obj)
if err != nil {
log.Critical("Error Encoding Gob:", err.Error())
}
// Timer between every sending, ie. Reading from buffer
time.Sleep(300 * time.Millisecond)
conn.Close()
}
}
The same function but for Data2 type
func SenderData2(address string, buff *filebuffer.Buffer) {
var conn net.Conn
var err error
var line string
var obj Data2
for {
line, err = buff.Pop()
if err != nil {
log.Critical("Error Poping:", err.Error())
continue
}
if len(line) == 0 {
time.Sleep(1 * time.Second)
continue
}
if err := json.Unmarshal([]byte(line), &obj); err != nil {
log.Critical("Error Unmarshaling:", err.Error())
continue
}
for {
log.Info("Trying to connect with Server...")
conn, err = net.Dial(PROTO, address)
// If err try to connect again
if err != nil {
log.Error("Error connecting:", err.Error())
time.Sleep(1 * time.Second)
continue
}
// If connected break the loop
break
}
log.Debug("Sending ", obj, " to:", address)
encoder := gob.NewEncoder(conn)
err := encoder.Encode(obj)
if err != nil {
log.Critical("Error Encoding Gob:", err.Error())
}
// Timer between every sending, ie. Reading from buffer
time.Sleep(300 * time.Millisecond)
conn.Close()
}
Add a parameter that allocates a new value of the type to receive and send:
func SenderData1(address string, buff *filebuffer.Buffer) {
SenderData(address, buff, func() interface{} { return new(Data1) })
}
func SenderData2(address string, buff *filebuffer.Buffer) {
SenderData(address, buff, func() interface{} { return new(Data2) })
}
func SenderData(address string, buff *filebuffer.Buffer, newfn func() interface{}) {
var conn net.Conn
var err error
var line string
for {
line, err = buff.Pop()
if err != nil {
log.Critical("Error Poping:", err.Error())
continue
}
if len(line) == 0 {
time.Sleep(1 * time.Second)
continue
}
obj := newfn()
if err := json.Unmarshal([]byte(line), obj); err != nil {
log.Critical("Error Unmarshaling:", err.Error())
continue
}
for {
log.Info("Trying to connect with Server...")
conn, err = net.Dial(PROTO, address)
// If err try to connect again
if err != nil {
log.Error("Error connecting:", err.Error())
time.Sleep(1 * time.Second)
continue
}
// If connected break the loop
break
}
log.Debug("Sending ", obj, " to:", address)
encoder := gob.NewEncoder(conn)
err := encoder.Encode(obj)
if err != nil {
log.Critical("Error Encoding Gob:", err.Error())
}
// Timer between every sending, ie. Reading from buffer
time.Sleep(300 * time.Millisecond)
conn.Close()
}
}
The code in this answer allocates a new value every time through the loop while the code in the question allocates the object once. Allocating each time through the loop prevents crosstalk between received JSON objects.

Resources