From 4198576f526793b8e4eec5403ebf602b518d1d44 Mon Sep 17 00:00:00 2001 From: Matthias Fulz Date: Mon, 6 Jul 2020 01:17:10 +0200 Subject: [PATCH] Added slog and progress --- configuration.go | 6 +++--- rpc.go | 4 ++-- server.go | 29 ++++++++++++++++++++++++++--- 3 files changed, 31 insertions(+), 8 deletions(-) diff --git a/configuration.go b/configuration.go index 7322d36..692cd03 100644 --- a/configuration.go +++ b/configuration.go @@ -1,13 +1,13 @@ package srpc import ( - "log" + "olznet.de/slog" "time" ) type LogErrorFunc func(format string, args ...interface{}) -var logError = LogErrorFunc(log.Printf) +var logError = LogErrorFunc(slog.LOG_ERROR) func SetLogError(f LogErrorFunc) { logError = f @@ -23,5 +23,5 @@ const ( const ( DefaultMaxClientRequests = int(128) DefaultRequestTimeout = 30 * time.Second - DefaultSimultanousClientRequests = int(5) + DefaultSimultanousClientRequests = int(15) ) diff --git a/rpc.go b/rpc.go index b1007ec..06aa570 100644 --- a/rpc.go +++ b/rpc.go @@ -3,7 +3,7 @@ package srpc import ( "bytes" "errors" - "fmt" + "olznet.de/slog" "reflect" "sync" ) @@ -82,7 +82,7 @@ func (r *RPC) RegisterName(name string, rcvr interface{}) (err error) { } } else { if err = encoder.Encode(v.Interface().(error).Error()); err != nil { - fmt.Println(err) + slog.LOG_ERROR(err.Error()) return nil, err } } diff --git a/server.go b/server.go index d86eded..7d4939a 100644 --- a/server.go +++ b/server.go @@ -5,6 +5,7 @@ import ( "fmt" "io" "net" + "olznet.de/slog" "sync" "sync/atomic" "time" @@ -169,7 +170,7 @@ func serverHandler(s *RPCServer) { func serverHandleConnection(s *RPCServer, conn io.ReadWriteCloser, clientAddr string) { defer func() { - fmt.Println("Client disconnected") + slog.LOG_INFO("Client disconnected\n") s.stopWg.Done() }() @@ -188,8 +189,21 @@ func serverHandleConnection(s *RPCServer, conn io.ReadWriteCloser, clientAddr st dec := s.Ed.NewDecoder(conn) enc := s.Ed.NewEncoder(conn) + clientConns := 0 + clientConnsLock := sync.Mutex{} + for { - //TODO: check for multiple requests per client + if clientConns == s.SimultanousClientRequests { + if !clientDisconnect(err) && !serverStop(s.stopChan) { + s.LogError("srpc - '%s'=>'%s': Client reached max requests\n", clientAddr, s.Addr) + } + return + } + + clientConnsLock.Lock() + clientConns++ + clientConnsLock.Unlock() + var msg clientMessage msg.Data = s.RequestDataHandler() @@ -198,11 +212,20 @@ func serverHandleConnection(s *RPCServer, conn io.ReadWriteCloser, clientAddr st s.LogError("srpc - '%s'=>'%s': Cannot decode request: '%s'\n", clientAddr, s.Addr, err) } conn.Close() + clientConnsLock.Lock() + clientConns-- + clientConnsLock.Unlock() return } - fmt.Println(msg.Data) + slog.LOG_DEBUG(fmt.Sprintln(msg.Data)) go func() { + defer func() { + clientConnsLock.Lock() + clientConns-- + clientConnsLock.Unlock() + }() + var response serverMessage response.ID = msg.ID response.ClientAddr = msg.ClientAddr