percipia / eslgo

A GoLang FreeSWITCH ESL Library

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Automatic reconnection if there is a lost connection

egorsmkv opened this issue · comments

Hello!

I would like to see this feature in the library :)

I think, it is possible to implement with the OnDisconnect callback and global variables. Am I right?

Hi @egorsmkv!

I originally thought of this as out of scope for this library since you can implement it on the user side with the OnDisconnect callback. What would your use case of this feature be? I tend to prefer to leave things to the user to implement since it gives them more power to make the library do what they want.

Example on how to do it today:

var conn *eslgo.Conn // Global instance, could be part of a struct and then startConnection is a method on that struct.
// You will need to protect access to conn behind a mutex or another synchronization method, left out of this example for brevity

func startConnection() {
	var err error
	for { // Add a max retry counter here if desired
		conn, err = eslgo.Dial("127.0.0.1:8021", "ClueCon", func() {
			// We disconnected, start it again
			go startConnection()
		})
		if err == nil {
			break
		}
		time.Sleep(time.Minute)
	}
}