Skip to content
This repository has been archived by the owner on Apr 21, 2022. It is now read-only.

hide watchdog behind cgo flag #103

Merged
merged 1 commit into from
Jan 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions connmgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (

logging "github.com/ipfs/go-log/v2"
ma "github.com/multiformats/go-multiaddr"
"github.com/raulk/go-watchdog"
)

var log = logging.Logger("connmgr")
Expand Down Expand Up @@ -44,10 +43,10 @@ type BasicConnMgr struct {
lastTrimMu sync.RWMutex
lastTrim time.Time

refCount sync.WaitGroup
ctx context.Context
cancel func()
unregisterWatchdog func()
refCount sync.WaitGroup
ctx context.Context
cancel func()
unregisterMemoryWatcher func()
}

var (
Expand Down Expand Up @@ -131,9 +130,7 @@ func NewConnManager(low, hi int, opts ...Option) (*BasicConnMgr, error) {

if cfg.emergencyTrim {
// When we're running low on memory, immediately trigger a trim.
cm.unregisterWatchdog = watchdog.RegisterPostGCNotifee(cm.memoryEmergency)
} else {
cm.unregisterWatchdog = func() {}
cm.unregisterMemoryWatcher = registerWatchdog(cm.memoryEmergency)
}

decay, _ := NewDecayer(cfg.decayer, cm)
Expand Down Expand Up @@ -176,7 +173,9 @@ func (cm *BasicConnMgr) memoryEmergency() {

func (cm *BasicConnMgr) Close() error {
cm.cancel()
cm.unregisterWatchdog()
if cm.unregisterMemoryWatcher != nil {
cm.unregisterMemoryWatcher()
}
if err := cm.decayer.Close(); err != nil {
return err
}
Expand Down
8 changes: 0 additions & 8 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,3 @@ func WithSilencePeriod(p time.Duration) Option {
return nil
}
}

// WithEmergencyTrim is an option to enable trimming connections on memory emergency.
func WithEmergencyTrim(enable bool) Option {
return func(cfg *config) error {
cfg.emergencyTrim = enable
return nil
}
}
18 changes: 18 additions & 0 deletions watchdog_cgo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//go:build cgo
// +build cgo

package connmgr

import "github.com/raulk/go-watchdog"

func registerWatchdog(cb func()) (unregister func()) {
return watchdog.RegisterPostGCNotifee(cb)
}

// WithEmergencyTrim is an option to enable trimming connections on memory emergency.
func WithEmergencyTrim(enable bool) Option {
return func(cfg *config) error {
cfg.emergencyTrim = enable
return nil
}
}
16 changes: 16 additions & 0 deletions watchdog_no_cgo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//go:build !cgo
// +build !cgo

package connmgr

func registerWatchdog(func()) (unregister func()) {
return nil
}

// WithEmergencyTrim is an option to enable trimming connections on memory emergency.
func WithEmergencyTrim(enable bool) Option {
return func(cfg *config) error {
log.Warn("platform doesn't support go-watchdog")
return nil
}
}