Added portManager debug logging

This commit is contained in:
Samuel Hawksby-Robinson 2022-10-19 11:01:04 +01:00
parent 8fb069286c
commit 9a7f38fcdf
4 changed files with 41 additions and 8 deletions

View File

@ -3,6 +3,8 @@ package server
import ( import (
"fmt" "fmt"
"sync" "sync"
"go.uber.org/zap"
) )
// portManager is responsible for maintaining segregated access to the port field // portManager is responsible for maintaining segregated access to the port field
@ -10,6 +12,7 @@ import (
// rwLock establishes a standard read write mutex that allows consecutive reads and exclusive writes // rwLock establishes a standard read write mutex that allows consecutive reads and exclusive writes
// mustRead forces MustGetPort to wait until port has a none 0 value // mustRead forces MustGetPort to wait until port has a none 0 value
type portManger struct { type portManger struct {
logger *zap.Logger
port int port int
afterPortChanged func(port int) afterPortChanged func(port int)
rwLock *sync.RWMutex rwLock *sync.RWMutex
@ -17,8 +20,9 @@ type portManger struct {
} }
// newPortManager returns a newly initialised portManager with a pre-Locked portManger.mustRead sync.Mutex // newPortManager returns a newly initialised portManager with a pre-Locked portManger.mustRead sync.Mutex
func newPortManager(afterPortChanged func(int)) portManger { func newPortManager(logger *zap.Logger, afterPortChanged func(int)) portManger {
pm := portManger{ pm := portManger{
logger: logger.Named("portManger"),
afterPortChanged: afterPortChanged, afterPortChanged: afterPortChanged,
rwLock: new(sync.RWMutex), rwLock: new(sync.RWMutex),
mustRead: new(sync.Mutex), mustRead: new(sync.Mutex),
@ -31,18 +35,26 @@ func newPortManager(afterPortChanged func(int)) portManger {
// next triggers any given portManger.afterPortChanged function // next triggers any given portManger.afterPortChanged function
// additionally portManger.mustRead.Unlock() is called, releasing any calls to MustGetPort // additionally portManger.mustRead.Unlock() is called, releasing any calls to MustGetPort
func (p *portManger) SetPort(port int) error { func (p *portManger) SetPort(port int) error {
l := p.logger.Named("SetPort")
l.Debug("fired", zap.Int("port", port))
p.rwLock.Lock() p.rwLock.Lock()
defer p.rwLock.Unlock() defer p.rwLock.Unlock()
l.Debug("acquired rwLock.Lock")
if port == 0 { if port == 0 {
return fmt.Errorf("port can not be `0`, use ResetPort() instead") errMsg := "port can not be `0`, use ResetPort() instead"
l.Error(errMsg)
return fmt.Errorf(errMsg)
} }
p.port = port p.port = port
if p.afterPortChanged != nil { if p.afterPortChanged != nil {
l.Debug("p.afterPortChanged != nil")
p.afterPortChanged(port) p.afterPortChanged(port)
} }
p.mustRead.Unlock() p.mustRead.Unlock()
l.Debug("p.mustRead.Unlock()")
return nil return nil
} }
@ -52,30 +64,47 @@ func (p *portManger) SetPort(port int) error {
// and calling multiple times must not cause a deadlock or an infinite hang, but the lock needs to be // and calling multiple times must not cause a deadlock or an infinite hang, but the lock needs to be
// reapplied if it is not present. // reapplied if it is not present.
func (p *portManger) ResetPort() { func (p *portManger) ResetPort() {
l := p.logger.Named("ResetPort")
l.Debug("fired")
p.rwLock.Lock() p.rwLock.Lock()
defer p.rwLock.Unlock() defer p.rwLock.Unlock()
l.Debug("acquired rwLock.Lock")
if p.mustRead.TryLock() { if p.mustRead.TryLock() {
l.Debug("able to lock mustRead")
p.port = 0 p.port = 0
return
} }
l.Debug("unable to lock mustRead")
} }
// GetPort gets the current value of portManager.port without any concern for the state of its value // GetPort gets the current value of portManager.port without any concern for the state of its value
// and therefore does not block until portManager.mustRead.Unlock() is called // and therefore does not block until portManager.mustRead.Unlock() is called
func (p *portManger) GetPort() int { func (p *portManger) GetPort() int {
l := p.logger.Named("GetPort")
l.Debug("fired")
p.rwLock.RLock() p.rwLock.RLock()
defer p.rwLock.RUnlock() defer p.rwLock.RUnlock()
l.Debug("acquired rwLock.RLock")
return p.port return p.port
} }
// MustGetPort only returns portManager.port if portManager.mustRead is unlocked. // MustGetPort only returns portManager.port if portManager.mustRead is unlocked.
// This presupposes that portManger.mustRead has a default state of locked and SetPort unlock portManager.mustRead // This presupposes that portManger.mustRead has a default state of locked and SetPort unlock portManager.mustRead
func (p *portManger) MustGetPort() int { func (p *portManger) MustGetPort() int {
l := p.logger.Named("MustGetPort")
l.Debug("fired")
p.mustRead.Lock() p.mustRead.Lock()
defer p.mustRead.Unlock() defer p.mustRead.Unlock()
l.Debug("acquired mustRead.Lock")
p.rwLock.RLock() p.rwLock.RLock()
defer p.rwLock.RUnlock() defer p.rwLock.RUnlock()
l.Debug("acquired rwLock.RLock")
return p.port return p.port
} }

View File

@ -24,11 +24,12 @@ type Server struct {
} }
func NewServer(cert *tls.Certificate, hostname string, afterPortChanged func(int)) Server { func NewServer(cert *tls.Certificate, hostname string, afterPortChanged func(int)) Server {
logger := logutils.ZapLogger()
return Server{ return Server{
logger: logutils.ZapLogger(), logger: logger,
cert: cert, cert: cert,
hostname: hostname, hostname: hostname,
portManger: newPortManager(afterPortChanged), portManger: newPortManager(logger, afterPortChanged),
} }
} }

View File

@ -5,6 +5,8 @@ import (
"time" "time"
"github.com/stretchr/testify/suite" "github.com/stretchr/testify/suite"
"github.com/status-im/status-go/logutils"
) )
const ( const (
@ -26,17 +28,18 @@ type ServerURLSuite struct {
func (s *ServerURLSuite) SetupTest() { func (s *ServerURLSuite) SetupTest() {
s.SetupKeyComponents(s.T()) s.SetupKeyComponents(s.T())
logger := logutils.ZapLogger()
s.server = &MediaServer{Server: Server{ s.server = &MediaServer{Server: Server{
hostname: defaultIP.String(), hostname: defaultIP.String(),
portManger: newPortManager(nil), portManger: newPortManager(logger, nil),
}} }}
err := s.server.SetPort(1337) err := s.server.SetPort(1337)
s.Require().NoError(err) s.Require().NoError(err)
s.serverNoPort = &MediaServer{Server: Server{ s.serverNoPort = &MediaServer{Server: Server{
hostname: defaultIP.String(), hostname: defaultIP.String(),
portManger: newPortManager(nil), portManger: newPortManager(logger, nil),
}} }}
go func() { go func() {
time.Sleep(waitTime) time.Sleep(waitTime)

View File

@ -7,11 +7,11 @@ const (
// EventMesssageDelivered triggered when we got acknowledge from datasync level, that means peer got message // EventMesssageDelivered triggered when we got acknowledge from datasync level, that means peer got message
EventMesssageDelivered = "message.delivered" EventMesssageDelivered = "message.delivered"
// EventCommunityFound triggered when user requested info about some community and messenger successfully // EventCommunityInfoFound triggered when user requested info about some community and messenger successfully
// retrieved it from mailserver // retrieved it from mailserver
EventCommunityInfoFound = "community.found" EventCommunityInfoFound = "community.found"
// Event Automatic Status Updates Timed out // EventStatusUpdatesTimedOut Event Automatic Status Updates Timed out
EventStatusUpdatesTimedOut = "status.updates.timedout" EventStatusUpdatesTimedOut = "status.updates.timedout"
) )