From 7ea305f249d772acae76b15595f751ee88f05b8d Mon Sep 17 00:00:00 2001 From: DanB Date: Sun, 3 Mar 2019 20:40:48 +0100 Subject: [PATCH] Adding SessionS.BiRPCv1SetPassiveSession RPC caching --- config/config_defaults.go | 2 +- config/config_json_test.go | 2 +- config/config_test.go | 2 +- sessions/sessions.go | 18 ++++++++++++++++-- utils/coreutils.go | 6 ++++++ 5 files changed, 25 insertions(+), 5 deletions(-) diff --git a/config/config_defaults.go b/config/config_defaults.go index 73d80b5e6..2c4a69e42 100755 --- a/config/config_defaults.go +++ b/config/config_defaults.go @@ -148,7 +148,7 @@ const CGRATES_CFG_JSON = ` "dispatcher_filter_indexes" : {"limit": -1, "ttl": "", "static_ttl": false}, // control dispatcher filter indexes caching "dispatcher_routes": {"limit": -1, "ttl": "", "static_ttl": false}, // control dispatcher routes caching "diameter_messages": {"limit": -1, "ttl": "3h", "static_ttl": false}, // diameter messages caching - "rpc_responses": {"limit": -1, "ttl": "2s", "static_ttl": false}, // RPC responses caching + "rpc_responses": {"limit": 0, "ttl": "2s", "static_ttl": false}, // RPC responses caching }, diff --git a/config/config_json_test.go b/config/config_json_test.go index 353e1d1ad..68a40a494 100755 --- a/config/config_json_test.go +++ b/config/config_json_test.go @@ -154,7 +154,7 @@ func TestCacheJsonCfg(t *testing.T) { Ttl: utils.StringPointer(""), Static_ttl: utils.BoolPointer(false)}, utils.CacheDiameterMessages: &CacheParamJsonCfg{Limit: utils.IntPointer(-1), Ttl: utils.StringPointer("3h"), Static_ttl: utils.BoolPointer(false)}, - utils.CacheRPCResponses: &CacheParamJsonCfg{Limit: utils.IntPointer(-1), + utils.CacheRPCResponses: &CacheParamJsonCfg{Limit: utils.IntPointer(0), Ttl: utils.StringPointer("2s"), Static_ttl: utils.BoolPointer(false)}, } diff --git a/config/config_test.go b/config/config_test.go index 2a5397c68..f0e8dea2f 100755 --- a/config/config_test.go +++ b/config/config_test.go @@ -705,7 +705,7 @@ func TestCgrCfgJSONDefaultsCacheCFG(t *testing.T) { TTL: time.Duration(0), StaticTTL: false, Precache: false}, utils.CacheDiameterMessages: &CacheParamCfg{Limit: -1, TTL: time.Duration(3 * time.Hour), StaticTTL: false}, - utils.CacheRPCResponses: &CacheParamCfg{Limit: -1, + utils.CacheRPCResponses: &CacheParamCfg{Limit: 0, TTL: time.Duration(2 * time.Second), StaticTTL: false}, } diff --git a/sessions/sessions.go b/sessions/sessions.go index dbd0c652e..29815ebba 100644 --- a/sessions/sessions.go +++ b/sessions/sessions.go @@ -1383,7 +1383,7 @@ func (sS *SessionS) CallBiRPC(clnt rpcclient.RpcClientConnection, // BiRPCv1GetActiveSessions returns the list of active sessions based on filter func (sS *SessionS) BiRPCv1GetActiveSessions(clnt rpcclient.RpcClientConnection, - fltr map[string]string, reply *[]*ActiveSession) error { + fltr map[string]string, reply *[]*ActiveSession) (err error) { for fldName, fldVal := range fltr { if fldVal == "" { fltr[fldName] = utils.META_NONE @@ -1455,9 +1455,23 @@ func (sS *SessionS) BiRPCv1SetPassiveSession(clnt rpcclient.RpcClientConnection, if s.CGRID == "" { return utils.NewErrMandatoryIeMissing(utils.CGRID) } + // handle RPC caching + cacheKey := utils.ConcatenatedKey("BiRPCv1SetPassiveSession", s.CGRID) + if itm, has := engine.Cache.Get(utils.CacheRPCResponses, cacheKey); has { + cachedResp := itm.(*utils.CachedRPCResponse) + if cachedResp.Error == nil { + *reply = *cachedResp.Result.(*string) + } + return cachedResp.Error + } + defer engine.Cache.Set(utils.CacheRPCResponses, cacheKey, + &utils.CachedRPCResponse{Result: reply, Error: err}, + nil, true, utils.NonTransactional) + // end of RPC caching if s.EventStart == nil { // remove instead of if removed := sS.unregisterSession(s.CGRID, true); !removed { - return utils.ErrServerError + err = utils.ErrServerError + return } } else { //if we have an active session with the same CGRID diff --git a/utils/coreutils.go b/utils/coreutils.go index d0a036145..3b2fe6dac 100644 --- a/utils/coreutils.go +++ b/utils/coreutils.go @@ -918,3 +918,9 @@ func (ffn *FallbackFileName) AsString() string { } return fmt.Sprintf("%s%s%s%s%s%s%s%s", ffn.Module, HandlerArgSep, ffn.Transport, HandlerArgSep, url.QueryEscape(ffn.Address), HandlerArgSep, ffn.RequestID, ffn.FileSuffix) } + +// CachedRPCResponse is used to cache a RPC response +type CachedRPCResponse struct { + Result interface{} + Error error +}