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

@ -23,4 +23,5 @@ const (
const ( const (
DefaultMaxClientRequests = int(128) DefaultMaxClientRequests = int(128)
DefaultRequestTimeout = 30 * time.Second DefaultRequestTimeout = 30 * time.Second
DefaultSimultanousClientRequests = int(5)
) )

View File

@ -67,6 +67,7 @@ type RPCServer struct {
ConnectHandler ConnectHandlerFunc ConnectHandler ConnectHandlerFunc
RequestDataHandler RequestDataHandlerFunc RequestDataHandler RequestDataHandlerFunc
RequestHandler RequestHandlerFunc RequestHandler RequestHandlerFunc
SimultanousClientRequests int
Ed IEncoderDecoder Ed IEncoderDecoder
stopChan chan struct{} stopChan chan struct{}
stopWg sync.WaitGroup stopWg sync.WaitGroup
@ -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 {