Added slog and progress

This commit is contained in:
Matthias Fulz 2020-07-06 01:17:10 +02:00
parent 668123cccb
commit 4198576f52
3 changed files with 31 additions and 8 deletions

View File

@ -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)
)

4
rpc.go
View File

@ -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
}
}

View File

@ -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