diff --git a/endpoint.go b/endpoint.go index 0c43277..d2bf69a 100644 --- a/endpoint.go +++ b/endpoint.go @@ -198,6 +198,8 @@ func (e *Endpoint) SetStateEx(newState EndpointState, takeMutex bool) { } else { e.listElement = newList.PushBack(e) } + + e.state = newState } func (e *Endpoint) SetState(newState EndpointState) { @@ -217,6 +219,7 @@ func (e *Endpoint) Delay(addTime time.Duration) { tm := time.Now().Add(addTime) if e.delayUntil.Before(tm) { + log("ep", 5, "extending delay deadline for endpoint \"%v\" from %v to %v", e, e.delayUntil, tm) e.delayUntil = tm } } @@ -360,7 +363,9 @@ func (e *Endpoint) NoSuchLogin(login string) { // Exhausted gets called when an endpoint no longer has any valid logins and passwords, // thus it may be deleted. -func (e *Endpoint) Exhausted() { +func (e *Endpoint) Exhausted() { + e.TakeMutex() + defer e.ReleaseMutex() e.Delete() } @@ -389,7 +394,11 @@ func GetDelayedEndpoint() (e *Endpoint, waitTime time.Duration) { } } - return nil, minWaitTime.Sub(currentTime) + if minWaitTime.Before(currentTime) { + return nil, 0 + } else { + return nil, minWaitTime.Sub(currentTime) + } } func (e *Endpoint) TakeMutex() { diff --git a/service.go b/service.go index 74359e7..2b49e55 100644 --- a/service.go +++ b/service.go @@ -23,6 +23,6 @@ func TryLogin(task *Task, conn *Connection) (res bool, err error) { } }() - res, err = NewWinbox(task, conn).TryLogin() + res, err = NewLegacyWinbox(task, conn).TryLogin() return res, err } diff --git a/task.go b/task.go index d88eb2e..8a33590 100644 --- a/task.go +++ b/task.go @@ -177,7 +177,11 @@ func GetDeferredTask() (task *Task, waitTime time.Duration) { } } - return nil, minWaitTime.Sub(currentTime) + if minWaitTime.Before(currentTime) { + return nil, 0 + } else { + return nil, minWaitTime.Sub(currentTime) + } } // FetchTaskComponents returns all components needed to build a Task. @@ -249,6 +253,10 @@ func CreateTask(threadIdx int) (task *Task, delay time.Duration) { } } + if getParamInt("max-aps") > 0 { + ep.Delay(time.Duration(float64(1000.0) / float64(getParamInt("max-aps")) * float64(time.Millisecond))) + } + t := Task{e: ep, login: login, password: password, thread: threadIdx} log("task", 4, "new task: %v", &t) diff --git a/thread.go b/thread.go index b780bf9..5b41ae9 100644 --- a/thread.go +++ b/thread.go @@ -68,9 +68,11 @@ func threadWork(threadIdx int) bool { log("thread", 3, "no work currently available, sleeping for %v in thread %v", delay, threadIdx) time.Sleep(delay) return true - } else { + } else if delay == 0 { log("thread", 2, "no more work for thread %v", threadIdx) return false + } else { + panic("negative task creation delay: " + delay.String()) } }