feat: max-aps implementation

fix: switching from deferred state
master
Dave S. 2 years ago
parent 706f5d8c90
commit 16d7d9352c
  1. 13
      endpoint.go
  2. 2
      service.go
  3. 10
      task.go
  4. 4
      thread.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() {

@ -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
}

@ -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)

@ -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())
}
}

Loading…
Cancel
Save