From af2db0a0d9a6ffbcea295c0dfd9c27133ca8856a Mon Sep 17 00:00:00 2001 From: x1nchen Date: Wed, 13 May 2020 22:00:13 +0800 Subject: [PATCH 1/3] fix: update dependency certmagic (#1625) module github.com/mholt/certmagic has been renamed github.com/caddyserver/certmagic, so upgrade on this module will fail. fix: micro/micro#835 caddyserver/certmagic@v0.10.6 is Maximum upgradeable version with go version 1.13 Higher version use *tls.ClientHelloInfo.SupportsCertificate which only supported in go 1.14 --- api/server/acme/certmagic/certmagic.go | 13 +++++++------ api/server/acme/certmagic/storage.go | 2 +- go.mod | 4 ++-- go.sum | 14 +++++++++----- 4 files changed, 19 insertions(+), 14 deletions(-) diff --git a/api/server/acme/certmagic/certmagic.go b/api/server/acme/certmagic/certmagic.go index e9daf62e..678add5e 100644 --- a/api/server/acme/certmagic/certmagic.go +++ b/api/server/acme/certmagic/certmagic.go @@ -1,4 +1,4 @@ -// Package certmagic is the ACME provider from github.com/mholt/certmagic +// Package certmagic is the ACME provider from github.com/caddyserver/certmagic package certmagic import ( @@ -7,7 +7,7 @@ import ( "net" "time" - "github.com/mholt/certmagic" + "github.com/caddyserver/certmagic" "github.com/micro/go-micro/v2/api/server/acme" "github.com/micro/go-micro/v2/logger" ) @@ -18,10 +18,10 @@ type certmagicProvider struct { // TODO: set self-contained options func (c *certmagicProvider) setup() { - certmagic.Default.CA = c.opts.CA + certmagic.DefaultACME.CA = c.opts.CA if c.opts.ChallengeProvider != nil { // Enabling DNS Challenge disables the other challenges - certmagic.Default.DNSProvider = c.opts.ChallengeProvider + certmagic.DefaultACME.DNSProvider = c.opts.ChallengeProvider } if c.opts.OnDemand { certmagic.Default.OnDemand = new(certmagic.OnDemandConfig) @@ -32,9 +32,10 @@ func (c *certmagicProvider) setup() { } // If multiple instances of the provider are running, inject some // randomness so they don't collide + // RenewalWindowRatio [0.33 - 0.50) rand.Seed(time.Now().UnixNano()) - randomDuration := (7 * 24 * time.Hour) + (time.Duration(rand.Intn(504)) * time.Hour) - certmagic.Default.RenewDurationBefore = randomDuration + randomRatio := float64(rand.Intn(17) + 33) * 0.01 + certmagic.Default.RenewalWindowRatio = randomRatio } func (c *certmagicProvider) Listen(hosts ...string) (net.Listener, error) { diff --git a/api/server/acme/certmagic/storage.go b/api/server/acme/certmagic/storage.go index 4e26b888..e9ac1ad1 100644 --- a/api/server/acme/certmagic/storage.go +++ b/api/server/acme/certmagic/storage.go @@ -9,7 +9,7 @@ import ( "strings" "time" - "github.com/mholt/certmagic" + "github.com/caddyserver/certmagic" "github.com/micro/go-micro/v2/store" "github.com/micro/go-micro/v2/sync" ) diff --git a/go.mod b/go.mod index 126d4415..4baf1ac9 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,7 @@ require ( github.com/BurntSushi/toml v0.3.1 github.com/bitly/go-simplejson v0.5.0 github.com/bwmarrin/discordgo v0.20.2 + github.com/caddyserver/certmagic v0.10.6 github.com/coreos/etcd v3.3.18+incompatible github.com/cpuguy83/go-md2man/v2 v2.0.0 // indirect github.com/davecgh/go-spew v1.1.1 @@ -16,7 +17,7 @@ require ( github.com/fsnotify/fsnotify v1.4.7 github.com/fsouza/go-dockerclient v1.6.0 github.com/ghodss/yaml v1.0.0 - github.com/go-acme/lego/v3 v3.3.0 + github.com/go-acme/lego/v3 v3.4.0 github.com/go-git/go-git/v5 v5.0.0 github.com/gobwas/httphead v0.0.0-20180130184737-2c6c146eadee github.com/gobwas/pool v0.2.0 // indirect @@ -33,7 +34,6 @@ require ( github.com/kr/pretty v0.1.0 github.com/lib/pq v1.3.0 github.com/lucas-clemente/quic-go v0.14.1 - github.com/mholt/certmagic v0.9.3 github.com/micro/cli/v2 v2.1.2 github.com/micro/go-micro v1.18.0 github.com/miekg/dns v1.1.27 diff --git a/go.sum b/go.sum index 32596903..345168c5 100644 --- a/go.sum +++ b/go.sum @@ -69,8 +69,12 @@ github.com/bwmarrin/discordgo v0.19.0/go.mod h1:O9S4p+ofTFwB02em7jkpkV8M3R0/PUVO github.com/bwmarrin/discordgo v0.20.1/go.mod h1:O9S4p+ofTFwB02em7jkpkV8M3R0/PUVOwN61zSZ0r4Q= github.com/bwmarrin/discordgo v0.20.2 h1:nA7jiTtqUA9lT93WL2jPjUp8ZTEInRujBdx1C9gkr20= github.com/bwmarrin/discordgo v0.20.2/go.mod h1:O9S4p+ofTFwB02em7jkpkV8M3R0/PUVOwN61zSZ0r4Q= +github.com/caddyserver/certmagic v0.10.6 h1:sCya6FmfaN74oZE46kqfaFOVoROD/mF36rTQfjN7TZc= +github.com/caddyserver/certmagic v0.10.6/go.mod h1:Y8jcUBctgk/IhpAzlHKfimZNyXCkfGgRTC0orl8gROQ= github.com/cenkalti/backoff/v3 v3.0.0 h1:ske+9nBpD9qZsTBoF41nW5L+AIuFBKMeze18XQ3eG1c= github.com/cenkalti/backoff/v3 v3.0.0/go.mod h1:cIeZDE3IrqwwJl6VUwCN6trj1oXrTS4rc0ij+ULvLYs= +github.com/cenkalti/backoff/v4 v4.0.0 h1:6VeaLF9aI+MAUQ95106HwWzYZgJJpZ4stumjj6RFYAU= +github.com/cenkalti/backoff/v4 v4.0.0/go.mod h1:eEew/i+1Q6OrCDZh3WiXYv3+nJwBASZ8Bog/87DQnVg= github.com/census-instrumentation/opencensus-proto v0.2.0/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/cheekybits/genny v1.0.0 h1:uGGa4nei+j20rOSeDeP5Of12XVm7TGUd4dJA9RDitfE= @@ -155,8 +159,8 @@ github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeME github.com/gliderlabs/ssh v0.2.2 h1:6zsha5zo/TWhRhwqCD3+EarCAgZ2yN28ipRnGPnwkI0= github.com/gliderlabs/ssh v0.2.2/go.mod h1:U7qILu1NlMHj9FlMhZLlkCdDnU1DBEAqr0aevW3Awn0= github.com/go-acme/lego/v3 v3.1.0/go.mod h1:074uqt+JS6plx+c9Xaiz6+L+GBb+7itGtzfcDM2AhEE= -github.com/go-acme/lego/v3 v3.3.0 h1:6BePZsOiYA4/w+M7QDytxQtMfCipMPGnWAHs9pWks98= -github.com/go-acme/lego/v3 v3.3.0/go.mod h1:iGSY2vQrvQs3WezicSB/oVbO2eCrD88dpWPwb1qLqu0= +github.com/go-acme/lego/v3 v3.4.0 h1:deB9NkelA+TfjGHVw8J7iKl/rMtffcGMWSMmptvMv0A= +github.com/go-acme/lego/v3 v3.4.0/go.mod h1:xYbLDuxq3Hy4bMUT1t9JIuz6GWIWb3m5X+TeTHYaT7M= github.com/go-cmd/cmd v1.0.5/go.mod h1:y8q8qlK5wQibcw63djSl/ntiHUHXHGdCkPk0j4QeW4s= github.com/go-errors/errors v1.0.1/go.mod h1:f4zRHt4oKfwPJE5k8C9vpYG+aDHdBFUsgrm6/TyX73Q= github.com/go-git/gcfg v1.5.0 h1:Q5ViNfGF8zFgyJWPqYwA7qGFoMTEiBmdlkcfRmpIMa4= @@ -288,6 +292,8 @@ github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvW github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= github.com/klauspost/cpuid v1.2.0 h1:NMpwD2G9JSFOE1/TJjGSo5zG7Yb2bTe7eq1jH+irmeE= github.com/klauspost/cpuid v1.2.0/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek= +github.com/klauspost/cpuid v1.2.3 h1:CCtW0xUnWGVINKvE/WWOYKdsPV6mawAtvQuSl8guwQs= +github.com/klauspost/cpuid v1.2.3/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek= github.com/kolo/xmlrpc v0.0.0-20190717152603-07c4ee3fd181/go.mod h1:o03bZfuBwAXHetKXuInt4S7omeXUu62/A845kiycsSQ= github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/konsorten/go-windows-terminal-sequences v1.0.2 h1:DB17ag19krx9CFsz4o3enTrPXyIXCl+2iCXH/aMAp9s= @@ -328,8 +334,6 @@ github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0j github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= github.com/mholt/certmagic v0.7.5/go.mod h1:91uJzK5K8IWtYQqTi5R2tsxV1pCde+wdGfaRaOZi6aQ= github.com/mholt/certmagic v0.8.3/go.mod h1:91uJzK5K8IWtYQqTi5R2tsxV1pCde+wdGfaRaOZi6aQ= -github.com/mholt/certmagic v0.9.3 h1:RmzuNJ5mpFplDbyS41z+gGgE/py24IX6m0nHZ0yNTQU= -github.com/mholt/certmagic v0.9.3/go.mod h1:nu8jbsbtwK4205EDH/ZUMTKsfYpJA1Q7MKXHfgTihNw= github.com/micro/cli v0.2.0 h1:ut3rV5JWqZjsXIa2MvGF+qMUP8DAUTvHX9Br5gO4afA= github.com/micro/cli v0.2.0/go.mod h1:jRT9gmfVKWSS6pkKcXQ8YhUyj6bzwxK8Fp5b0Y7qNnk= github.com/micro/cli/v2 v2.1.2 h1:43J1lChg/rZCC1rvdqZNFSQDrGT7qfMrtp6/ztpIkEM= @@ -385,7 +389,7 @@ github.com/nlopes/slack v0.6.0/go.mod h1:JzQ9m3PMAqcpeCam7UaHSuBuupz7CmpjehYMayT github.com/nlopes/slack v0.6.1-0.20191106133607-d06c2a2b3249 h1:Pr5gZa2VcmktVwq0lyC39MsN5tz356vC/pQHKvq+QBo= github.com/nlopes/slack v0.6.1-0.20191106133607-d06c2a2b3249/go.mod h1:JzQ9m3PMAqcpeCam7UaHSuBuupz7CmpjehYMayT6YOk= github.com/nrdcg/auroradns v1.0.0/go.mod h1:6JPXKzIRzZzMqtTDgueIhTi6rFf1QvYE/HzqidhOhjw= -github.com/nrdcg/dnspod-go v0.3.0/go.mod h1:vZSoFSFeQVm2gWLMkyX61LZ8HI3BaqtHZWgPTGKr6KQ= +github.com/nrdcg/dnspod-go v0.4.0/go.mod h1:vZSoFSFeQVm2gWLMkyX61LZ8HI3BaqtHZWgPTGKr6KQ= github.com/nrdcg/goinwx v0.6.1/go.mod h1:XPiut7enlbEdntAqalBIqcYcTEVhpv/dKWgDCX2SwKQ= github.com/nrdcg/namesilo v0.2.1/go.mod h1:lwMvfQTyYq+BbjJd30ylEG4GPSS6PII0Tia4rRpRiyw= github.com/olekukonko/tablewriter v0.0.1/go.mod h1:vsDQFd/mU46D+Z4whnwzcISnGGzXWMclvtLoiIKAKIo= From b14d63b4a12dda13bc4a8a2bbfeb53d6e8cdffe5 Mon Sep 17 00:00:00 2001 From: Pieter Voorwinden Date: Wed, 13 May 2020 16:13:23 +0200 Subject: [PATCH 2/3] Check if auth is nil to prevent nilpointer --- util/wrapper/wrapper.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/util/wrapper/wrapper.go b/util/wrapper/wrapper.go index 285a775f..8b501b12 100644 --- a/util/wrapper/wrapper.go +++ b/util/wrapper/wrapper.go @@ -155,7 +155,7 @@ func (a *authWrapper) Call(ctx context.Context, req client.Request, rsp interfac // if auth is nil we won't be able to get an access token, so we execute // the request without one. aa := a.auth() - if a == nil { + if aa == nil { return a.Client.Call(ctx, req, rsp, opts...) } From 290595f88e22af4a80c15b1f960d41ac6aeaded2 Mon Sep 17 00:00:00 2001 From: Asim Aslam Date: Wed, 13 May 2020 16:13:36 +0100 Subject: [PATCH 3/3] Strip down router code (#1627) --- router/default.go | 125 ++++------------------------------------------ 1 file changed, 9 insertions(+), 116 deletions(-) diff --git a/router/default.go b/router/default.go index 9026886b..f3b329a4 100644 --- a/router/default.go +++ b/router/default.go @@ -3,7 +3,6 @@ package router import ( "errors" "fmt" - "math" "sort" "strings" "sync" @@ -19,18 +18,6 @@ var ( AdvertiseEventsTick = 10 * time.Second // DefaultAdvertTTL is default advertisement TTL DefaultAdvertTTL = 2 * time.Minute - // AdvertSuppress is advert suppression threshold - AdvertSuppress = 200.0 - // AdvertRecover is advert recovery threshold - AdvertRecover = 20.0 - // Penalty for routes processed multiple times - Penalty = 100.0 - // PenaltyHalfLife is the time the advert penalty decays to half its value - PenaltyHalfLife = 30.0 - // MaxSuppressTime defines time after which the suppressed advert is deleted - MaxSuppressTime = 90 * time.Second - // PenaltyDecay is a coefficient which controls the speed the advert penalty decays - PenaltyDecay = math.Log(2) / PenaltyHalfLife ) // router implements default router @@ -269,68 +256,8 @@ func (r *router) publishAdvert(advType AdvertType, events []*Event) { r.sub.RUnlock() } -// advert contains a route event to be advertised -type advert struct { - // event received from routing table - event *Event - // lastSeen records the time of the last advert update - lastSeen time.Time - // penalty is current advert penalty - penalty float64 - // isSuppressed flags the advert suppression - isSuppressed bool - // suppressTime records the time interval the advert has been suppressed for - suppressTime time.Time -} - // adverts maintains a map of router adverts -type adverts map[uint64]*advert - -// process processes advert -// It updates advert timestamp, increments its penalty and -// marks upresses or recovers it if it reaches configured thresholds -func (m adverts) process(a *advert) error { - // lookup advert in adverts - hash := a.event.Route.Hash() - a, ok := m[hash] - if !ok { - return fmt.Errorf("advert not found") - } - - // decay the event penalty - delta := time.Since(a.lastSeen).Seconds() - - // decay advert penalty - a.penalty = a.penalty * math.Exp(-delta*PenaltyDecay) - service := a.event.Route.Service - address := a.event.Route.Address - - // suppress/recover the event based on its penalty level - switch { - case a.penalty > AdvertSuppress && !a.isSuppressed: - if logger.V(logger.DebugLevel, logger.DefaultLogger) { - logger.Debugf("Router suppressing advert %d %.2f for route %s %s", hash, a.penalty, service, address) - } - a.isSuppressed = true - a.suppressTime = time.Now() - case a.penalty < AdvertRecover && a.isSuppressed: - if logger.V(logger.DebugLevel, logger.DefaultLogger) { - logger.Debugf("Router recovering advert %d %.2f for route %s %s", hash, a.penalty, service, address) - } - a.isSuppressed = false - } - - // if suppressed, checked how long has it been suppressed for - if a.isSuppressed { - // max suppression time threshold has been reached, delete the advert - if time.Since(a.suppressTime) > MaxSuppressTime { - delete(m, hash) - return nil - } - } - - return nil -} +type adverts map[uint64]*Event // advertiseEvents advertises routing table events // It suppresses unhealthy flapping events and advertises healthy events upstream. @@ -396,21 +323,9 @@ func (r *router) advertiseEvents() error { var events []*Event // collect all events which are not flapping - for key, advert := range adverts { - // process the advert - if err := adverts.process(advert); err != nil { - if logger.V(logger.DebugLevel, logger.DefaultLogger) { - logger.Debugf("Router failed processing advert %d: %v", key, err) - } - continue - } - // if suppressed go to the next advert - if advert.isSuppressed { - continue - } - + for key, event := range adverts { // if we only advertise local routes skip processing anything not link local - if r.options.Advertise == AdvertiseLocal && advert.event.Route.Link != "local" { + if r.options.Advertise == AdvertiseLocal && event.Route.Link != "local" { continue } @@ -418,7 +333,7 @@ func (r *router) advertiseEvents() error { e := new(Event) // this is ok, because router.Event only contains builtin types // and no references so this creates a deep copy of struct Event - *e = *(advert.event) + *e = *event events = append(events, e) // delete the advert from adverts delete(adverts, key) @@ -447,44 +362,22 @@ func (r *router) advertiseEvents() error { continue } - now := time.Now() - if logger.V(logger.DebugLevel, logger.DefaultLogger) { logger.Debugf("Router processing table event %s for service %s %s", e.Type, e.Route.Service, e.Route.Address) } // check if we have already registered the route hash := e.Route.Hash() - a, ok := adverts[hash] + ev, ok := adverts[hash] if !ok { - a = &advert{ - event: e, - penalty: Penalty, - lastSeen: now, - } - adverts[hash] = a + ev = e + adverts[hash] = e continue } // override the route event only if the previous event was different - if a.event.Type != e.Type { - a.event = e - } - - // process the advert - if err := adverts.process(a); err != nil { - if logger.V(logger.DebugLevel, logger.DefaultLogger) { - logger.Debugf("Router error processing advert %d: %v", hash, err) - } - continue - } - - // update event penalty and timestamp - a.lastSeen = now - // increment the penalty - a.penalty += Penalty - if logger.V(logger.DebugLevel, logger.DefaultLogger) { - logger.Debugf("Router advert %d for route %s %s event penalty: %f", hash, a.event.Route.Service, a.event.Route.Address, a.penalty) + if ev.Type != e.Type { + ev = e } case <-r.exit: if w != nil {