Started simultanous client requests

This commit is contained in:
Matthias Fulz 2020-03-26 02:12:46 +01:00
parent 231f347976
commit e418f50f2e
2 changed files with 21 additions and 12 deletions

View File

@ -21,6 +21,7 @@ const (
) )
const ( const (
DefaultMaxClientRequests = int(128) DefaultMaxClientRequests = int(128)
DefaultRequestTimeout = 30 * time.Second DefaultRequestTimeout = 30 * time.Second
DefaultSimultanousClientRequests = int(5)
) )

View File

@ -61,15 +61,16 @@ func (nl *netListener) Addr() net.Addr {
} }
type RPCServer struct { type RPCServer struct {
Addr string Addr string
Listener IListener Listener IListener
LogError LogErrorFunc LogError LogErrorFunc
ConnectHandler ConnectHandlerFunc ConnectHandler ConnectHandlerFunc
RequestDataHandler RequestDataHandlerFunc RequestDataHandler RequestDataHandlerFunc
RequestHandler RequestHandlerFunc RequestHandler RequestHandlerFunc
Ed IEncoderDecoder SimultanousClientRequests int
stopChan chan struct{} Ed IEncoderDecoder
stopWg sync.WaitGroup stopChan chan struct{}
stopWg sync.WaitGroup
} }
func (s *RPCServer) Start() (err error) { func (s *RPCServer) Start() (err error) {
@ -90,6 +91,10 @@ func (s *RPCServer) Start() (err error) {
return errors.New("srpc - Server needs a RequestHandler") return errors.New("srpc - Server needs a RequestHandler")
} }
if s.SimultanousClientRequests == 0 {
s.SimultanousClientRequests = DefaultSimultanousClientRequests
}
if s.Listener == nil { if s.Listener == nil {
s.Listener = &netListener{} s.Listener = &netListener{}
} }
@ -163,7 +168,10 @@ func serverHandler(s *RPCServer) {
} }
func serverHandleConnection(s *RPCServer, conn io.ReadWriteCloser, clientAddr string) { func serverHandleConnection(s *RPCServer, conn io.ReadWriteCloser, clientAddr string) {
defer s.stopWg.Done() defer func() {
fmt.Println("Client disconnected")
s.stopWg.Done()
}()
if s.ConnectHandler != nil { if s.ConnectHandler != nil {
if newConn, err := s.ConnectHandler(clientAddr, conn); err != nil { if newConn, err := s.ConnectHandler(clientAddr, conn); err != nil {