Arman92 / go-tdlib

Golang Telegram TdLib JSON bindings
GNU General Public License v3.0
435 stars 101 forks source link

operator often timeout #84

Closed FunkyYang closed 3 years ago

FunkyYang commented 3 years ago

for example use client.SendMessage(chatID, 0, 0, nil, nil, inputMsgTxt) like this .the code from go-package and I saw the source code

func (client *Client) SendAndCatch(jsonQuery interface{}) (UpdateMsg, error) { var update UpdateData

switch jsonQuery.(type) {
case string:
    // unmarshal JSON into map, we don't have @extra field, if user don't set it
    json.Unmarshal([]byte(jsonQuery.(string)), &update)
case UpdateData:
    update = jsonQuery.(UpdateData)
}

// letters for generating random string
letterBytes := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

// generate random string for @extra field
b := make([]byte, 32)
for i := range b {
    b[i] = letterBytes[rand.Intn(len(letterBytes))]
}
randomString := string(b)

// set @extra field
update["@extra"] = randomString

// create waiter chan and save it in Waiters
waiter := make(chan UpdateMsg, 1)

client.waitersLock.Lock()
client.waiters[randomString] = waiter
client.waitersLock.Unlock()

// send it through already implemented method
client.Send(update)

select {
// wait response from main loop in NewClient()
case response := <-waiter:
    client.waitersLock.Lock()
    delete(client.waiters, randomString)
    client.waitersLock.Unlock()

    return response, nil
    // or timeout
case <-time.After(10 * time.Second):
    client.waitersLock.Lock()
    delete(client.waiters, randomString)
    client.waitersLock.Unlock()

    return UpdateMsg{}, errors.New("timeout")
}

} after 10 second ,then return timeout error

FunkyYang commented 3 years ago

first time I used this method ok.but since first time often return timeout ,why ?

FunkyYang commented 3 years ago

oh my mistake,i don't consume rawUpdate chan