Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

An endless loop caused by an iterator #136

Open
evlic opened this issue Jul 5, 2024 · 0 comments
Open

An endless loop caused by an iterator #136

evlic opened this issue Jul 5, 2024 · 0 comments

Comments

@evlic
Copy link

evlic commented Jul 5, 2024

SDK: github.com/larksuite/oapi-sdk-go/v3 v3.2.7

issue code

func QueryAllChat() (list []*larkim.ListChat, err error) {
	list = make([]*larkim.ListChat, 0, defaultGroupCnt)
	req := larkim.NewListChatReqBuilder().
		SortType(`ByCreateTimeAsc`).
		PageSize(20).
		Build()

	iterator, err := component.LarkCli.Im.Chat.ListByIterator(defCtx, req)
	if err != nil {
		return
	}

	for {
		hasNext, chat, thisErr := iterator.Next()
		if thisErr != nil {
			err = thisErr
			return
		}

		if !hasNext {
			break
		}

		if chat != nil {
			list = append(list, chat)
		}
	}
	return
}

log

2024-07-06T01:27:12.399+0800    DEBUG   warp_feishu/logger.go:12        req:POST,/open-apis/auth/v3/tenant_access_token/internal
2024-07-06T01:27:12.891+0800    DEBUG   warp_feishu/logger.go:12        req:GET,/open-apis/im/v1/chats
2024-07-06T01:27:13.158+0800    DEBUG   warp_feishu/logger.go:12        req:GET,/open-apis/im/v1/chats
2024-07-06T01:27:13.549+0800    DEBUG   warp_feishu/logger.go:12        req:GET,/open-apis/im/v1/chats
2024-07-06T01:27:13.811+0800    DEBUG   warp_feishu/logger.go:12        req:GET,/open-apis/im/v1/chats
2024-07-06T01:27:14.039+0800    DEBUG   warp_feishu/logger.go:12        req:GET,/open-apis/im/v1/chats
2024-07-06T01:27:14.277+0800    DEBUG   warp_feishu/logger.go:12        req:GET,/open-apis/im/v1/chats
2024-07-06T01:27:14.521+0800    DEBUG   warp_feishu/logger.go:12        req:GET,/open-apis/im/v1/chats
2024-07-06T01:27:14.780+0800    DEBUG   warp_feishu/logger.go:12        req:GET,/open-apis/im/v1/chats
2024-07-06T01:27:15.029+0800    DEBUG   warp_feishu/logger.go:12        req:GET,/open-apis/im/v1/chats

conjecture

# github.com/larksuite/oapi-sdk-go/[email protected]/service/im/v1/model.go:12076

...
	// 为0则拉取数据
	if iterator.index == 0 || iterator.index >= len(iterator.items) {
		if iterator.index != 0 && iterator.nextPageToken == nil {
			return false, nil, nil
		}
		if iterator.nextPageToken != nil {
			iterator.req.apiReq.QueryParams.Set("page_token", *iterator.nextPageToken)
		}
		resp, err := iterator.listFunc(iterator.ctx, iterator.req, iterator.options...)
		if err != nil {
			return false, nil, err
		}

		if resp.Code != 0 {
			return false, nil, errors.New(fmt.Sprintf("Code:%d,Msg:%s", resp.Code, resp.Msg))
		}

		if len(resp.Data.Items) == 0 {
			return false, nil, nil
		}

		iterator.nextPageToken = resp.Data.PageToken
		iterator.items = resp.Data.Items
		iterator.index = 0
	}
...

当列表仅有一页数据,此时接口返回 pageToken 空,hasMore false

iterator 并没有保存 hasMore 状态,进入死循环

image
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant