Goroutine stops halfway.

Asked 2 years ago, Updated 2 years ago, 78 views

I am writing code in Go language to automate Twitter follow-up.I wrote the following code, but I will follow it normally until the middle, but it will stop in the middle after a while.Exceeded Rate Limit. and Finished are not printed in the code, so it is not stopped at the intended location, but it is stopped due to an abnormality.However, I don't know where the processing has stopped.Sometimes after Add friends and sometimes after Waiting.

code

package autofollow

import(
    "errors"
    "fmt"
    "github.com/ChimeraCoder/anaconda"
    "net/url"
    "os"
    "strconv"
    "time"
)

var(
    accessToken string
    accessTokenSecret string
)

// AutoFollow is object for following.
type AutoFollow structure {
    words [ ] string
    knint
    numuint
}

// NewAutoFollow is constructor of AutoFollow.
func NewAutoFollow(words[]string, nuint) (*AutoFollow, error) {
    if consumerKey: =os.Getenv("CONSUMER_KEY"); consumerKey=="{
        return nil, errors.New("CONSUMER_KEY enviable was not set")
    } else{
        anaconda.SetConsumerKey(consumerKey)
    }

    if consumerSecret: =os.Getenv("CONSUMER_SECRET"); consumerSecret=="{
        return nil, errors.New("CONSUMER_SECRET enviable was not set")
    } else{
        anaconda.SetConsumerSecret(consumerSecret)
    }

    if accessToken=os.Getenv("ACCESS_TOKEN"); accessToken=="{
        return nil, errors.New("ACCESS_TOKEN enviable was not set")
    }

    if accessTokenSecret=os.Getenv("ACCESS_TOKEN_SECRET"); accessTokenSecret=="{
        return nil, errors.New("ACCESS_TOKEN_SECRET enviable was not set")
    }

    US>return&AutoFollow{
        words:words,
        wn —len(words),
        num —n,
    }, nil
}

// Follow starts following.
func(a*AutoFollow)Follow() (chan*anaconda.User, chanbool, chan error, error) {
    c:=make(chan*anaconda.User, a.num)
    fnCh: = make (chan bool, 1)
    errCh: = make (chan error, a.num)
    go a.roop(c,fnCh,errCh)

    return c, fnCh, errCh, nil
}

func(a*AutoFollow)roop(ucchan*anaconda.User, fnChanbool, errChan error){
    fCount: = 0
    ec:=make(chan error, a.wn)
    nc: = make (chan bool, a.wn)
    cn: = make (chan bool, a.wn)

    // create a goroutine for each word
    for_,v: = range a.words {
        fmt.Printf("Starting thread for %s\n", v)
        go a.follow(uc,nc,ec,cn,v)
    }

    // until the desired number of people is reached
L:
    for {
        // move on
        for i —=0;i<a.wn;i++{
            nc<-true
        }
        select{
        caseerr:=<-ec:
            iferr.(anaconda.TwitterError).Code==anaconda.TwitterErrorRateLimitExceeded{
                fmt.Println("Exceeded Rate Limit.")
                // When the rate limit is reached
                time.Sleep(20*time.Minute)
            } else{
                fmt.Printf("error happy%v\n",err)
                errCh<-err
            }
        case<-cn:
            fmt.Println("Add friends")
            fCount++
            if int(a.num)<fCount{
                // be over when one reaches one's intended number
                fmt.Println("Finished")
                break L
            }
        default:
        }
    }
    fnCh<-true
}

func(a*AutoFollow)follow(ucchan*anaconda.User, ncchanbool, ecchan error, cnchanbool, word string){
    p: = 0
    api: = anaconda.NewTwitterApi (accessToken, accessTokenSecret)
    for {
        fmt.Println ("Waiting")
        <-nc
        v: = url.Values{}
        v.Add ("page", strconv.Itoa(p))
        v.Add("count", strconv.Itoa(20))
        v.Add("include_entities", "false")
        users,err: = api.GetUserSearch(word,v)
        if err!=nil{
            // RateLimit
            ec<-err
        } else{
            for_, v: = range users {
                if!v.Following&!v.FollowRequestSent&(v.FollowersCount<v.FriendsCount){
                    // become friends with
                    cn<-true
                    user,err: = api.FollowUserId(v.Id, url.Values{})
                    if err!=nil{
                        ec<-err
                    } else{
                        uc<-&user
                    }
                }
            }
        }
        p++
    }
}

output

Waiting
Waiting
Waiting
Waiting
Waiting
Waiting
Waiting
Waiting
Waiting
Waiting
Waiting
Waiting
Waiting
Waiting
Waiting
Waiting
Waiting
Waiting
Waiting
Waiting
Waiting
Waiting
Waiting
Waiting
Waiting
Add friends
Waiting
Waiting
Waiting
Add friends
Waiting
Waiting
Waiting
Add friends
Waiting
Waiting
Waiting
Add friends
Waiting

Why does the process stop in the middle?What we know is as follows:

  • It works well halfway

  • I don't think the rate limit has been reached because there is no output and I can follow it even if I run it over and over again.

  • The specified number of followers has not been reached.

  • There are no errors.

All the way to the middle

It is thought that the rate limit has not been reached because there is no output and it can be followed even if it is repeated.

The specified number of followers has not been reached.

There are no errors.

go

2022-09-30 18:12

1 Answers

Let's stop squashing errors.


2022-09-30 18:12

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.