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 package srpc
import ( import (
"log" "olznet.de/slog"
"time" "time"
) )
type LogErrorFunc func(format string, args ...interface{}) type LogErrorFunc func(format string, args ...interface{})
var logError = LogErrorFunc(log.Printf) var logError = LogErrorFunc(slog.LOG_ERROR)
func SetLogError(f LogErrorFunc) { func SetLogError(f LogErrorFunc) {
logError = f logError = f
@ -23,5 +23,5 @@ const (
const ( const (
DefaultMaxClientRequests = int(128) DefaultMaxClientRequests = int(128)
DefaultRequestTimeout = 30 * time.Second DefaultRequestTimeout = 30 * time.Second
DefaultSimultanousClientRequests = int(5) DefaultSimultanousClientRequests = int(15)
) )

4
rpc.go
View File

@ -3,7 +3,7 @@ package srpc
import ( import (
"bytes" "bytes"
"errors" "errors"
"fmt" "olznet.de/slog"
"reflect" "reflect"
"sync" "sync"
) )
@ -82,7 +82,7 @@ func (r *RPC) RegisterName(name string, rcvr interface{}) (err error) {
} }
} else { } else {
if err = encoder.Encode(v.Interface().(error).Error()); err != nil { if err = encoder.Encode(v.Interface().(error).Error()); err != nil {
fmt.Println(err) slog.LOG_ERROR(err.Error())
return nil, err return nil, err
} }
} }

View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"io" "io"
"net" "net"
"olznet.de/slog"
"sync" "sync"
"sync/atomic" "sync/atomic"
"time" "time"
@ -169,7 +170,7 @@ func serverHandler(s *RPCServer) {
func serverHandleConnection(s *RPCServer, conn io.ReadWriteCloser, clientAddr string) { func serverHandleConnection(s *RPCServer, conn io.ReadWriteCloser, clientAddr string) {
defer func() { defer func() {
fmt.Println("Client disconnected") slog.LOG_INFO("Client disconnected\n")
s.stopWg.Done() s.stopWg.Done()
}() }()
@ -188,8 +189,21 @@ func serverHandleConnection(s *RPCServer, conn io.ReadWriteCloser, clientAddr st
dec := s.Ed.NewDecoder(conn) dec := s.Ed.NewDecoder(conn)
enc := s.Ed.NewEncoder(conn) enc := s.Ed.NewEncoder(conn)
clientConns := 0
clientConnsLock := sync.Mutex{}
for { 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 var msg clientMessage
msg.Data = s.RequestDataHandler() 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) s.LogError("srpc - '%s'=>'%s': Cannot decode request: '%s'\n", clientAddr, s.Addr, err)
} }
conn.Close() conn.Close()
clientConnsLock.Lock()
clientConns--
clientConnsLock.Unlock()
return return
} }
fmt.Println(msg.Data) slog.LOG_DEBUG(fmt.Sprintln(msg.Data))
go func() { go func() {
defer func() {
clientConnsLock.Lock()
clientConns--
clientConnsLock.Unlock()
}()
var response serverMessage var response serverMessage
response.ID = msg.ID response.ID = msg.ID
response.ClientAddr = msg.ClientAddr response.ClientAddr = msg.ClientAddr