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

Concurrent goroutine and WithRetryKey() Usage Leads to Request Discrepancies #435

Open
YukiTsuchida opened this issue Apr 12, 2024 · 3 comments

Comments

@YukiTsuchida
Copy link

YukiTsuchida commented Apr 12, 2024

System Informations

  • Go version: 1.22
  • OS: All

Expected Behavior

When making multiple PushMessage API requests concurrently using goroutines, each request's X-Line-Retry-Key and message body should match and be sent as expected.

Current Behavior

Using goroutines to invoke the PushMessage API multiple times results in occasional mismatches between the X-Line-Retry-Key and the message content in the request body. This leads to the LINE API mistakenly identifying these requests as duplicates, returning a 409 error, and consequently failing to send some messages.

Steps to Reproduce

https://go.dev/play/p/6idORXgHYJ_s

Logs

Here is an example of the actual request logs, where the mismatch between the X-Line-Retry-Key and the text field in the request body can be confirmed:

POST /v2/bot/message/push HTTP/1.1
Host: 127.0.0.1:36111
Accept-Encoding: gzip
Authorization: Bearer channelToken
Content-Length: 52
Content-Type: application/json; charset=UTF-8
User-Agent: LINE-BotSDK-Go/8.4.0
X-Line-Retry-Key: 1

{"to":"to","messages":[{"type":"text","text":"1"}]}

POST /v2/bot/message/push HTTP/1.1
Host: 127.0.0.1:36111
Accept-Encoding: gzip
Authorization: Bearer channelToken
Content-Length: 52
Content-Type: application/json; charset=UTF-8
User-Agent: LINE-BotSDK-Go/8.4.0
X-Line-Retry-Key: 4

{"to":"to","messages":[{"type":"text","text":"4"}]}

POST /v2/bot/message/push HTTP/1.1
Host: 127.0.0.1:36111
Accept-Encoding: gzip
Authorization: Bearer channelToken
Content-Length: 52
Content-Type: application/json; charset=UTF-8
User-Agent: LINE-BotSDK-Go/8.4.0
X-Line-Retry-Key: 0

{"to":"to","messages":[{"type":"text","text":"0"}]}

POST /v2/bot/message/push HTTP/1.1
Host: 127.0.0.1:36111
Accept-Encoding: gzip
Authorization: Bearer channelToken
Content-Length: 52
Content-Type: application/json; charset=UTF-8
User-Agent: LINE-BotSDK-Go/8.4.0
X-Line-Retry-Key: 2

{"to":"to","messages":[{"type":"text","text":"2"}]}

POST /v2/bot/message/push HTTP/1.1
Host: 127.0.0.1:36111
Accept-Encoding: gzip
Authorization: Bearer channelToken
Content-Length: 52
Content-Type: application/json; charset=UTF-8
User-Agent: LINE-BotSDK-Go/8.4.0
X-Line-Retry-Key: 0                                              <-------- 0 (incorrect)

{"to":"to","messages":[{"type":"text","text":"3"}]}

Additional Context

Currently, I am working around this issue by creating a new instance of linebot.Client for each request, but this is merely a temporary workaround. This approach is not suitable, especially under circumstances of handling large-scale traffic. I am looking forward to a resolution for this problem.

@YukiTsuchida YukiTsuchida changed the title Inconsistencies in Requests When Using goroutine and WithRetryKey() Together Concurrent goroutine and WithRetryKey() Usage Leads to Request Discrepancies Apr 12, 2024
@Yang-33
Copy link
Contributor

Yang-33 commented Apr 12, 2024

Hi, thank you for using line-bot-sdk-go and this report, @YukiTsuchida !

The issue is that this is not thread-safe in setRetryKey

func (client *Client) setRetryKey(retryKey string) {
client.retryKeyID = retryKey
}

Therefore, creating a new client is one of the solutions. The new client does not seem to have the same issue. Please give it a try.

line-bot-sdk-go/README.md

Lines 149 to 165 in 291a96f

### Send message ###
With an ID, you can send message using ```PushMessage()```
```go
bot.PushMessage(
&messaging_api.PushMessageRequest{
To: "U.......",
Messages: []messaging_api.MessageInterface{
messaging_api.TextMessage{
Text: replyMessage,
},
},
},
nil, // x-line-retry-key
)
```

@YukiTsuchida
Copy link
Author

@Yang-33 Thanks for the quick reply! I solved it using the method you provided.
https://go.dev/play/p/Xx4Bk3nTzrY

@Yang-33
Copy link
Contributor

Yang-33 commented Apr 12, 2024

Thank you for trying this yourself~ That's good~

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

No branches or pull requests

2 participants