go-smpp
go-smpp copied to clipboard
A small bug in SubmitLongMsg function
Hello everyone. I've found a "bug" when I use SubmitLongMsg function. If t.do(p) function returns ErrMaxWindowSize, then SubmitLongMsg terminates and it becomes impossible to send the rest parts of message. But ErrMaxWindowSize is a minor error and it would be great if SubmitLongMsg entered to sleep mode when this error occured. I've found the way how to do that:
again:
resp, err := t.do(p)
if err == ErrMaxWindowSize {
time.Sleep(100 * time.Millisecond)
goto again
}
if err != nil {
return nil, err
}
But I don't know how can I calculate ideal sleep mode duration.
Oh! God damn. Maybe need I making semaphore struct and rework t.do function to limit sending messages by my goroutines?
Hmmm I don't know what would be the ideal sleep time here either. One thing you could do is to have a backoff timer that starts at 100ms and increases up to a limit. However, at some point it might have to bail out if the limit is reached and kept there for N times or something.
This is exactly what we did in previous project - set minimal sleep duration (like 10ms) and then increased it by some delta (could also be 10ms) until the error vanished. After some time we can start decreasing the sleep interval by delimiter again until we get an error, and so on. This way it stays balanced out all the time.
How to implement this here...need to think about it.
One pattern that I've used for backoff retries is by creating a time.Timer(0) followed by a for + select loop that on failed attempts reset the timer and continues the loop. This makes it easy to bump the delay (normally use math.E or math.Phi for the multiplier) and to support cancellation since it's blocking in a select call.