georgehao commented on code in PR #803:
URL: https://github.com/apache/rocketmq-client-go/pull/803#discussion_r851024542
##########
internal/request_response_future.go:
##########
@@ -0,0 +1,135 @@
+package internal
+
+import (
+ "context"
+ "fmt"
+ "sync"
+ "time"
+
+ "github.com/patrickmn/go-cache"
+ "github.com/pkg/errors"
+
+ "github.com/apache/rocketmq-client-go/v2/primitive"
+ "github.com/apache/rocketmq-client-go/v2/rlog"
+)
+
+var RequestResponseFutureMap = NewRequestResponseFutureMap()
+
+type requestResponseFutureCache struct {
+ cache *cache.Cache
+}
+
+func NewRequestResponseFutureMap() *requestResponseFutureCache {
+ tmpRrfCache := requestResponseFutureCache{
+ cache: cache.New(5*time.Minute, 10*time.Minute),
+ }
+
+ // OnEvicted delete the timeout RequestResponseFuture, trigger set the
failure cause.
+ tmpRrfCache.cache.OnEvicted(func(s string, i interface{}) {
+ rrf, ok := i.(*RequestResponseFuture)
+ if !ok {
+ rlog.Error("convert i to RequestResponseFuture err",
map[string]interface{}{
+ "correlationId": s,
+ })
+ return
+ }
+ if !rrf.IsTimeout() {
+ return
+ }
+
+ err := fmt.Errorf("correlationId:%s request timeout, no reply
message", s)
+ rrf.CauseErr = err
+ rrf.ExecuteRequestCallback()
+ })
+ return &tmpRrfCache
+}
+
+// SetRequestResponseFuture set rrf to map
+func (fm *requestResponseFutureCache) SetRequestResponseFuture(rrf
*RequestResponseFuture) {
+ fm.cache.Set(rrf.CorrelationId, rrf, rrf.Timeout)
+}
+
+// SetResponseToRequestResponseFuture set reply to rrf
+func (fm *requestResponseFutureCache)
SetResponseToRequestResponseFuture(correlationId string, reply
*primitive.Message) error {
+ rrf, exist := fm.RequestResponseFuture(correlationId)
+ if !exist {
+ return errors.Wrapf(nil, "correlationId:%s not exist in map",
correlationId)
+ }
+ rrf.PutResponseMessage(reply)
+ if rrf.RequestCallback != nil {
+ rrf.ExecuteRequestCallback()
+ }
+ return nil
+}
+
+// RequestResponseFuture get rrf from map by the CorrelationId
+func (fm *requestResponseFutureCache) RequestResponseFuture(correlationId
string) (*RequestResponseFuture, bool) {
+ res, exists := fm.cache.Get(correlationId)
+ if exists {
+ return res.(*RequestResponseFuture), exists
+ }
+ return nil, exists
+}
+
+// RemoveRequestResponseFuture remove the rrf from map
+func (fm *requestResponseFutureCache)
RemoveRequestResponseFuture(correlationId string) {
+ fm.cache.Delete(correlationId)
Review Comment:
request_response_future.go:L28有处理这个事情
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]