From f081e9b8c1f864274f97a1ff264117663ec99a4b Mon Sep 17 00:00:00 2001 From: Eryu Guan Date: Wed, 8 Jan 2025 23:50:04 +0800 Subject: [PATCH 1/2] Export Coalescer interface Prepared for user defined coalescer. Signed-off-by: Eryu Guan --- serf/coalesce.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/serf/coalesce.go b/serf/coalesce.go index 9310e53e6..518444dd2 100644 --- a/serf/coalesce.go +++ b/serf/coalesce.go @@ -7,10 +7,10 @@ import ( "time" ) -// coalescer is a simple interface that must be implemented to be +// Coalescer is a simple interface that must be implemented to be // used inside of a coalesceLoop -type coalescer interface { - // Can the coalescer handle this event, if not it is +type Coalescer interface { + // Can the Coalescer handle this event, if not it is // directly passed through to the destination channel Handle(Event) bool @@ -22,9 +22,9 @@ type coalescer interface { } // coalescedEventCh returns an event channel where the events are coalesced -// using the given coalescer. +// using the given Coalescer. func coalescedEventCh(outCh chan<- Event, shutdownCh <-chan struct{}, - cPeriod time.Duration, qPeriod time.Duration, c coalescer) chan<- Event { + cPeriod time.Duration, qPeriod time.Duration, c Coalescer) chan<- Event { inCh := make(chan Event, 1024) go coalesceLoop(inCh, outCh, shutdownCh, cPeriod, qPeriod, c) return inCh @@ -33,7 +33,7 @@ func coalescedEventCh(outCh chan<- Event, shutdownCh <-chan struct{}, // coalesceLoop is a simple long-running routine that manages the high-level // flow of coalescing based on quiescence and a maximum quantum period. func coalesceLoop(inCh <-chan Event, outCh chan<- Event, shutdownCh <-chan struct{}, - coalescePeriod time.Duration, quiescentPeriod time.Duration, c coalescer) { + coalescePeriod time.Duration, quiescentPeriod time.Duration, c Coalescer) { var quiescent <-chan time.Time var quantum <-chan time.Time shutdown := false From 28c2e8bcec7916a014960e45ed0c4436fc3d6e71 Mon Sep 17 00:00:00 2001 From: Eryu Guan Date: Thu, 9 Jan 2025 11:59:22 +0800 Subject: [PATCH 2/2] Support user defined user event coalescer Signed-off-by: Eryu Guan --- serf/config.go | 3 +++ serf/serf.go | 7 +++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/serf/config.go b/serf/config.go index c44aa7561..0bb3a3c6f 100644 --- a/serf/config.go +++ b/serf/config.go @@ -95,6 +95,9 @@ type Config struct { UserCoalescePeriod time.Duration UserQuiescentPeriod time.Duration + // Optional user defined Coalescer for user events. + UserEventCoalescer Coalescer + // The settings below relate to Serf keeping track of recently // failed/left nodes and attempting reconnects. // diff --git a/serf/serf.go b/serf/serf.go index a260c8738..ba6871705 100644 --- a/serf/serf.go +++ b/serf/serf.go @@ -303,8 +303,11 @@ func Create(conf *Config) (*Serf, error) { // Check if user event coalescing is enabled if conf.UserCoalescePeriod > 0 && conf.UserQuiescentPeriod > 0 && conf.EventCh != nil { - c := &userEventCoalescer{ - events: make(map[string]*latestUserEvents), + c := conf.UserEventCoalescer + if c == nil { + c = &userEventCoalescer{ + events: make(map[string]*latestUserEvents), + } } conf.EventCh = coalescedEventCh(conf.EventCh, serf.shutdownCh,