Skip to content

Commit

Permalink
Use global acm to improve efficiency
Browse files Browse the repository at this point in the history
  • Loading branch information
xxx7xxxx committed Dec 7, 2023
1 parent b7bbeaf commit 800a208
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
33 changes: 19 additions & 14 deletions pkg/object/autocertmanager/autocertmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"fmt"
"net/http"
"strings"
"sync/atomic"
"time"

"github.com/megaease/easegress/v2/pkg/api"
Expand All @@ -44,11 +45,15 @@ const (
Kind = "AutoCertManager"
)

var aliases = []string{
"autocert",
"autocerts",
"autocertmanagers",
}
var (
aliases = []string{
"autocert",
"autocerts",
"autocertmanagers",
}

globalACM atomic.Value
)

func init() {
supervisor.Register(&AutoCertManager{})
Expand Down Expand Up @@ -257,6 +262,8 @@ func (acm *AutoCertManager) reload() {

go acm.run()
go acm.watchCertificate()

globalACM.Store(acm)
}

// Status returns the status of AutoCertManager.
Expand All @@ -275,6 +282,8 @@ func (acm *AutoCertManager) Status() *supervisor.Status {
// Close closes AutoCertManager.
func (acm *AutoCertManager) Close() {
acm.cancel()

globalACM.CompareAndSwap(acm, (*AutoCertManager)(nil))
}

func (acm *AutoCertManager) renew() bool {
Expand Down Expand Up @@ -423,16 +432,12 @@ func (acm *AutoCertManager) HandleHTTP01Challenge(w http.ResponseWriter, r *http
}

func GetGlobalAutoCertManager() (*AutoCertManager, bool) {
var acm *AutoCertManager

supervisor.GetGlobalSuper().WalkControllers(func(controller *supervisor.ObjectEntity) bool {
if controller.Spec().Kind() == Kind {
acm = controller.Instance().(*AutoCertManager)
return false
}
value := globalACM.Load()
if value == nil {
return nil, false
}

return true
})
acm := value.(*AutoCertManager)

if acm == nil {
return nil, false
Expand Down
2 changes: 1 addition & 1 deletion pkg/object/httpserver/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func (spec *Spec) tlsConfig() (*tls.Config, error) {
if spec.AutoCert {
acm, exists := autocertmanager.GetGlobalAutoCertManager()
if !exists {
return nil, fmt.Errorf("BUG: autocert manager is not initialized")
return nil, fmt.Errorf("there is no AutoCertManager")
}

tlsConf.GetCertificate = func(chi *tls.ClientHelloInfo) (*tls.Certificate, error) {
Expand Down

0 comments on commit 800a208

Please sign in to comment.