You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by GitBox <gi...@apache.org> on 2021/06/27 07:29:34 UTC

[GitHub] [apisix-dashboard] tokers commented on a change in pull request #1956: refactor: server manager

tokers commented on a change in pull request #1956:
URL: https://github.com/apache/apisix-dashboard/pull/1956#discussion_r659277256



##########
File path: api/internal/core/server/server.go
##########
@@ -0,0 +1,152 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package server
+
+import (
+	"context"
+	"fmt"
+	"net/http"
+	"os"
+	"time"
+
+	"github.com/apisix/manager-api/internal/conf"
+	"github.com/apisix/manager-api/internal/log"
+	"github.com/apisix/manager-api/internal/utils"
+)
+
+// server is the manager of Manager API, which is responsible for managing the life cycle of Manager API, including initialization, start, stop and so on
+type server struct {
+	server    *http.Server
+	serverSSL *http.Server
+	options   *Options
+}
+
+type Options struct {
+	ForceStart bool // force start new instance
+}
+
+// NewServer Create a server manager
+func NewServer(options *Options) (*server, error) {
+	return &server{options: options}, nil
+}
+
+func (s *server) Start() error {
+	// initialize server
+	err := s.init()
+	if err != nil {
+		return err
+	}
+
+	// write daemon pid file
+	err = s.writePID()
+	if err != nil {
+		return err
+	}
+
+	// print server info to stdout
+	s.printInfo()
+
+	// For internal error handling across multiple goroutines.
+	errSig := make(chan error, 1)
+
+	// start HTTP server
+	log.Infof("The Manager API is listening on %s", s.server.Addr)
+	go func() {
+		if err := s.server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
+			log.Errorf("listen and serv fail: %s", err)
+			errSig <- err
+		}
+	}()
+
+	// start HTTPs server
+	if conf.SSLCert != "" && conf.SSLKey != "" {
+		go func() {
+			err := s.serverSSL.ListenAndServeTLS(conf.SSLCert, conf.SSLKey)
+			if err != nil && err != http.ErrServerClosed {
+				log.Errorf("listen and serve for HTTPS failed: %s", err)
+				errSig <- err
+			}
+		}()
+	}
+
+	// handle HTTP(s) server error
+	select {
+	case err := <-errSig:
+		return err
+	default:
+		return nil
+	}
+}
+
+func (s *server) Stop() {
+	utils.CloseAll()
+
+	s.shutdownServer(s.server)
+	s.shutdownServer(s.serverSSL)
+}
+
+func (s *server) init() error {
+	log.Info("Initialize Manager API store")
+	err := s.setupStore()
+	if err != nil {
+		return err
+	}
+
+	log.Info("Initialize Manager API server")
+	s.setupHTTPServer()

Review comment:
       Only setup the HTTP server is enough?

##########
File path: api/internal/core/server/server.go
##########
@@ -0,0 +1,152 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package server
+
+import (
+	"context"
+	"fmt"
+	"net/http"
+	"os"
+	"time"
+
+	"github.com/apisix/manager-api/internal/conf"
+	"github.com/apisix/manager-api/internal/log"
+	"github.com/apisix/manager-api/internal/utils"
+)
+
+// server is the manager of Manager API, which is responsible for managing the life cycle of Manager API, including initialization, start, stop and so on
+type server struct {
+	server    *http.Server
+	serverSSL *http.Server
+	options   *Options
+}
+
+type Options struct {
+	ForceStart bool // force start new instance
+}
+
+// NewServer Create a server manager
+func NewServer(options *Options) (*server, error) {
+	return &server{options: options}, nil
+}
+
+func (s *server) Start() error {
+	// initialize server
+	err := s.init()
+	if err != nil {
+		return err
+	}
+
+	// write daemon pid file
+	err = s.writePID()
+	if err != nil {
+		return err
+	}
+
+	// print server info to stdout
+	s.printInfo()
+
+	// For internal error handling across multiple goroutines.
+	errSig := make(chan error, 1)
+
+	// start HTTP server
+	log.Infof("The Manager API is listening on %s", s.server.Addr)
+	go func() {
+		if err := s.server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
+			log.Errorf("listen and serv fail: %s", err)
+			errSig <- err
+		}
+	}()
+
+	// start HTTPs server
+	if conf.SSLCert != "" && conf.SSLKey != "" {
+		go func() {
+			err := s.serverSSL.ListenAndServeTLS(conf.SSLCert, conf.SSLKey)
+			if err != nil && err != http.ErrServerClosed {
+				log.Errorf("listen and serve for HTTPS failed: %s", err)
+				errSig <- err
+			}
+		}()
+	}
+
+	// handle HTTP(s) server error
+	select {
+	case err := <-errSig:

Review comment:
       Frankly, the way to detect whether these servers were started is not reliable, as it depends on the behavior the go runtime, like the scheduler. These two goroutines might not get the change to run when the `select` statement is run.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org