percipia / eslgo

A GoLang FreeSWITCH ESL Library
Mozilla Public License 2.0
103 stars 43 forks source link

Automatic reconnection if there is a lost connection #10

Closed egorsmkv closed 3 years ago

egorsmkv commented 3 years ago

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?

winsock commented 3 years ago

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)
    }
}
egorsmkv commented 3 years ago

Thanks, @winsock !