Import in v0.10 Concurrent mechanism

This commit is contained in:
TeoV
2020-07-09 16:39:26 +03:00
parent 95b765f8e6
commit 40987e13fd
18 changed files with 972 additions and 125 deletions

69
utils/concureqs.go Normal file
View File

@@ -0,0 +1,69 @@
/*
Real-time Online/Offline Charging System (OCS) for Telecom & ISP environments
Copyright (C) ITsysCOM GmbH
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
*/
package utils
import (
"fmt"
)
var ConReqs *ConcReqs
type ConcReqs struct {
limit int
strategy string
aReqs chan struct{}
}
func NewConReqs(reqs int, strategy string) *ConcReqs {
cR := &ConcReqs{
limit: reqs,
strategy: strategy,
aReqs: make(chan struct{}, reqs),
}
for i := 0; i < reqs; i++ {
cR.aReqs <- struct{}{}
}
return cR
}
var errDeny = fmt.Errorf("denying request due to maximum active requests reached")
func (cR *ConcReqs) Allocate() (err error) {
if cR.limit == 0 {
return
}
switch cR.strategy {
case MetaBusy:
if len(cR.aReqs) == 0 {
return errDeny
}
fallthrough
case MetaQueue:
<-cR.aReqs // get from channel
}
return
}
func (cR *ConcReqs) Deallocate() {
if cR.limit == 0 {
return
}
cR.aReqs <- struct{}{}
return
}

View File

@@ -0,0 +1,96 @@
/*
Real-time Online/Offline Charging System (OCS) for Telecom & ISP environments
Copyright (C) ITsysCOM GmbH
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
*/
// Most of the logic follows standard library implementation in this file
package utils
import (
"bufio"
"encoding/gob"
"io"
"log"
"net/rpc"
)
type concReqsGobServerCodec struct {
rwc io.ReadWriteCloser
dec *gob.Decoder
enc *gob.Encoder
encBuf *bufio.Writer
closed bool
allocated bool // populated if we have allocated a channel for concurrent requests
}
func NewConcReqsGobServerCodec(conn io.ReadWriteCloser) rpc.ServerCodec {
buf := bufio.NewWriter(conn)
return &concReqsGobServerCodec{
rwc: conn,
dec: gob.NewDecoder(conn),
enc: gob.NewEncoder(buf),
encBuf: buf,
}
}
func (c *concReqsGobServerCodec) ReadRequestHeader(r *rpc.Request) error {
return c.dec.Decode(r)
}
func (c *concReqsGobServerCodec) ReadRequestBody(body interface{}) error {
if err := ConReqs.Allocate(); err != nil {
return err
}
c.allocated = true
return c.dec.Decode(body)
}
func (c *concReqsGobServerCodec) WriteResponse(r *rpc.Response, body interface{}) (err error) {
if c.allocated {
defer func() {
ConReqs.Deallocate()
c.allocated = false
}()
}
if err = c.enc.Encode(r); err != nil {
if c.encBuf.Flush() == nil {
// Gob couldn't encode the header. Should not happen, so if it does,
// shut down the connection to signal that the connection is broken.
log.Println("rpc: gob error encoding response:", err)
c.Close()
}
return
}
if err = c.enc.Encode(body); err != nil {
if c.encBuf.Flush() == nil {
// Was a gob problem encoding the body but the header has been written.
// Shut down the connection to signal that the connection is broken.
log.Println("rpc: gob error encoding body:", err)
c.Close()
}
return
}
return c.encBuf.Flush()
}
func (c *concReqsGobServerCodec) Close() error {
if c.closed {
// Only call c.rwc.Close once; otherwise the semantics are undefined.
return nil
}
c.closed = true
return c.rwc.Close()
}

View File

@@ -0,0 +1,133 @@
/*
Real-time Online/Offline Charging System (OCS) for Telecom & ISP environments
Copyright (C) ITsysCOM GmbH
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
*/
// Most of the logic follows standard library implementation in this file
package utils
import (
"encoding/json"
"errors"
"io"
"net/rpc"
"sync"
)
type concReqsServerCodec struct {
dec *json.Decoder // for reading JSON values
enc *json.Encoder // for writing JSON values
c io.Closer
// temporary work space
req serverRequest
// JSON-RPC clients can use arbitrary json values as request IDs.
// Package rpc expects uint64 request IDs.
// We assign uint64 sequence numbers to incoming requests
// but save the original request ID in the pending map.
// When rpc responds, we use the sequence number in
// the response to find the original request ID.
mutex sync.Mutex // protects seq, pending
seq uint64
pending map[uint64]*json.RawMessage
allocated bool // populated if we have allocated a channel for concurrent requests
}
// NewConcReqsServerCodec returns a new rpc.ServerCodec using JSON-RPC on conn.
func NewConcReqsServerCodec(conn io.ReadWriteCloser) rpc.ServerCodec {
return &concReqsServerCodec{
dec: json.NewDecoder(conn),
enc: json.NewEncoder(conn),
c: conn,
pending: make(map[uint64]*json.RawMessage),
}
}
func (c *concReqsServerCodec) ReadRequestHeader(r *rpc.Request) error {
c.req.reset()
if err := c.dec.Decode(&c.req); err != nil {
return err
}
r.ServiceMethod = c.req.Method
// JSON request id can be any JSON value;
// RPC package expects uint64. Translate to
// internal uint64 and save JSON on the side.
c.mutex.Lock()
c.seq++
c.pending[c.seq] = c.req.Id
c.req.Id = nil
r.Seq = c.seq
c.mutex.Unlock()
return nil
}
func (c *concReqsServerCodec) ReadRequestBody(x interface{}) error {
if err := ConReqs.Allocate(); err != nil {
return err
}
c.allocated = true
if x == nil {
return nil
}
if c.req.Params == nil {
return errMissingParams
}
// JSON params is array value.
// RPC params is struct.
// Unmarshal into array containing struct for now.
// Should think about making RPC more general.
var params [1]interface{}
params[0] = x
return json.Unmarshal(*c.req.Params, &params)
}
func (c *concReqsServerCodec) WriteResponse(r *rpc.Response, x interface{}) error {
if c.allocated {
defer func() {
ConReqs.Deallocate()
c.allocated = false
}()
}
c.mutex.Lock()
b, ok := c.pending[r.Seq]
if !ok {
c.mutex.Unlock()
return errors.New("invalid sequence number in response")
}
delete(c.pending, r.Seq)
c.mutex.Unlock()
if b == nil {
// Invalid request so no id. Use JSON null.
b = &null
}
resp := serverResponse{Id: b}
if r.Error == "" {
resp.Result = x
} else {
resp.Error = r.Error
}
return c.enc.Encode(resp)
}
func (c *concReqsServerCodec) Close() error {
return c.c.Close()
}

View File

@@ -659,6 +659,8 @@ const (
MetaGroup = "*group"
InternalRPCSet = "InternalRPCSet"
FileName = "FileName"
MetaBusy = "*busy"
MetaQueue = "*Queue"
)
// Migrator Action
@@ -1202,6 +1204,7 @@ const (
CoreSv1 = "CoreSv1"
CoreSv1Status = "CoreSv1.Status"
CoreSv1Ping = "CoreSv1.Ping"
CoreSv1Sleep = "CoreSv1.Sleep"
)
// SupplierS APIs
@@ -1306,6 +1309,7 @@ const (
SessionSv1ActivateSessions = "SessionSv1.ActivateSessions"
SessionSv1DeactivateSessions = "SessionSv1.DeactivateSessions"
SMGenericV1InitiateSession = "SMGenericV1.InitiateSession"
SessionSv1Sleep = "SessionSv1.Sleep"
)
// Responder APIs
@@ -1562,30 +1566,32 @@ var (
// GeneralCfg
const (
NodeIDCfg = "node_id"
LoggerCfg = "logger"
LogLevelCfg = "log_level"
HttpSkipTlsVerifyCfg = "http_skip_tls_verify"
RoundingDecimalsCfg = "rounding_decimals"
DBDataEncodingCfg = "dbdata_encoding"
TpExportPathCfg = "tpexport_dir"
PosterAttemptsCfg = "poster_attempts"
FailedPostsDirCfg = "failed_posts_dir"
FailedPostsTTLCfg = "failed_posts_ttl"
DefaultReqTypeCfg = "default_request_type"
DefaultCategoryCfg = "default_category"
DefaultTenantCfg = "default_tenant"
DefaultTimezoneCfg = "default_timezone"
DefaultCachingCfg = "default_caching"
ConnectAttemptsCfg = "connect_attempts"
ReconnectsCfg = "reconnects"
ConnectTimeoutCfg = "connect_timeout"
ReplyTimeoutCfg = "reply_timeout"
LockingTimeoutCfg = "locking_timeout"
DigestSeparatorCfg = "digest_separator"
DigestEqualCfg = "digest_equal"
RSRSepCfg = "rsr_separator"
MaxParralelConnsCfg = "max_parralel_conns"
NodeIDCfg = "node_id"
LoggerCfg = "logger"
LogLevelCfg = "log_level"
HttpSkipTlsVerifyCfg = "http_skip_tls_verify"
RoundingDecimalsCfg = "rounding_decimals"
DBDataEncodingCfg = "dbdata_encoding"
TpExportPathCfg = "tpexport_dir"
PosterAttemptsCfg = "poster_attempts"
FailedPostsDirCfg = "failed_posts_dir"
FailedPostsTTLCfg = "failed_posts_ttl"
DefaultReqTypeCfg = "default_request_type"
DefaultCategoryCfg = "default_category"
DefaultTenantCfg = "default_tenant"
DefaultTimezoneCfg = "default_timezone"
DefaultCachingCfg = "default_caching"
ConnectAttemptsCfg = "connect_attempts"
ReconnectsCfg = "reconnects"
ConnectTimeoutCfg = "connect_timeout"
ReplyTimeoutCfg = "reply_timeout"
LockingTimeoutCfg = "locking_timeout"
DigestSeparatorCfg = "digest_separator"
DigestEqualCfg = "digest_equal"
RSRSepCfg = "rsr_separator"
MaxParralelConnsCfg = "max_parralel_conns"
ConcurrentRequestsCfg = "concurrent_requests"
ConcurrentStrategyCfg = "concurrent_strategy"
)
// StorDbCfg

View File

@@ -178,7 +178,7 @@ func (s *Server) ServeJSON(addr string, exitChan chan bool) {
if s.isDispatched {
go rpc.ServeCodec(NewCustomJSONServerCodec(conn))
} else {
go jsonrpc.ServeConn(conn)
go rpc.ServeCodec(NewConcReqsServerCodec(conn))
}
}
@@ -217,8 +217,7 @@ func (s *Server) ServeGOB(addr string, exitChan chan bool) {
continue
}
//utils.Logger.Info(fmt.Sprintf("<CGRServer> New incoming connection: %v", conn.RemoteAddr()))
go rpc.ServeConn(conn)
go rpc.ServeCodec(NewConcReqsGobServerCodec(conn))
}
}
@@ -281,7 +280,7 @@ func (s *Server) ServeHTTP(addr string, jsonRPCURL string, wsRPCURL string,
if s.isDispatched {
rpc.ServeCodec(NewCustomJSONServerCodec(ws))
} else {
jsonrpc.ServeConn(ws)
rpc.ServeCodec(NewConcReqsServerCodec(ws))
}
})
if useBasicAuth {
@@ -378,7 +377,7 @@ func (r *rpcRequest) Close() error {
// Call invokes the RPC request, waits for it to complete, and returns the results.
func (r *rpcRequest) Call() io.Reader {
go jsonrpc.ServeConn(r)
go rpc.ServeCodec(NewConcReqsServerCodec(r))
<-r.done
return r.rw
}