Improve forwardMessages catcher

This commit is contained in:
c0re100 2022-08-03 09:52:06 +08:00
parent 52c9a63fab
commit 51b9da9304
No known key found for this signature in database
GPG key ID: 7C3B3004FE745AAF

View file

@ -19,6 +19,7 @@ type Client struct {
listenerStore *listenerStore listenerStore *listenerStore
catchersStore *sync.Map catchersStore *sync.Map
successMsgStore *sync.Map successMsgStore *sync.Map
forwardMsgStore *sync.Map
updatesTimeout time.Duration updatesTimeout time.Duration
catchTimeout time.Duration catchTimeout time.Duration
} }
@ -74,6 +75,7 @@ func NewClient(authorizationStateHandler AuthorizationStateHandler, options ...O
listenerStore: newListenerStore(), listenerStore: newListenerStore(),
catchersStore: &sync.Map{}, catchersStore: &sync.Map{},
successMsgStore: &sync.Map{}, successMsgStore: &sync.Map{},
forwardMsgStore: &sync.Map{},
} }
client.extraGenerator = UuidV4Generator() client.extraGenerator = UuidV4Generator()
@ -110,9 +112,13 @@ func (client *Client) processResponse(response *Response) {
} }
if typ.GetType() == (&UpdateMessageSendSucceeded{}).GetType() { if typ.GetType() == (&UpdateMessageSendSucceeded{}).GetType() {
value, ok := client.successMsgStore.Load(typ.(*UpdateMessageSendSucceeded).OldMessageId) sendVal, sOk := client.successMsgStore.Load(typ.(*UpdateMessageSendSucceeded).OldMessageId)
if ok { if sOk {
value.(chan *Response) <- response sendVal.(chan *Response) <- response
}
forwardVal, fOk := client.forwardMsgStore.Load(typ.(*UpdateMessageSendSucceeded).OldMessageId)
if fOk {
forwardVal.(chan *Response) <- response
} }
} }
@ -220,16 +226,16 @@ func (client *Client) Send(req Request) (*Response, error) {
} }
for _, m := range ms.Messages { for _, m := range ms.Messages {
successCatcher := make(chan *Response, 1) forwardCatcher := make(chan *Response, 1)
client.successMsgStore.Store(m.Id, successCatcher) client.forwardMsgStore.Store(m.Id, forwardCatcher)
defer (func() { defer (func() {
client.successMsgStore.Delete(m.Id) client.forwardMsgStore.Delete(m.Id)
close(successCatcher) close(forwardCatcher)
})() })()
select { select {
case modResponse := <-successCatcher: case modResponse := <-forwardCatcher:
m2, err2 := UnmarshalUpdateMessageSendSucceeded(modResponse.Data) m2, err2 := UnmarshalUpdateMessageSendSucceeded(modResponse.Data)
if err2 != nil { if err2 != nil {
return response, nil return response, nil