Skip to content

Commit

Permalink
optimize sail service logic
Browse files Browse the repository at this point in the history
  • Loading branch information
alimy committed May 24, 2024
1 parent b6f0161 commit a7e0fdd
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 24 deletions.
24 changes: 3 additions & 21 deletions sail/examples/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,22 @@ package main

import (
"fmt"
"net/http"
"os"
"os/signal"
"syscall"

"github.com/alimy/mir/sail/mir-example/v4/service"
sail "github.com/alimy/mir/sail/v4/service"
"github.com/fatih/color"
"github.com/gin-gonic/gin"
"github.com/sourcegraph/conc"
)

func main() {
webEngine, botEngine := service.NewEngine(), service.NewEngine()
p := sail.NewHttpServerPool[*gin.Engine]()

webServerAddr, botServerAddr := ":8080", ":8081"
hsWeb := sail.NewBaseHttpService(p, webEngine, &http.Server{
Addr: webServerAddr,
})
hsBot := sail.NewBaseHttpService(p, botEngine, &http.Server{
Addr: botServerAddr,
})

webSrv := service.NewWebService(hsWeb, webServerAddr)
botSrv := service.NewBotService(hsBot, botServerAddr)

// init service
sail.MustInitService(webSrv, botSrv)
runtime := service.NewRuntime()

// start services
wg := conc.NewWaitGroup()
fmt.Fprintf(color.Output, "\nstarting run service...\n\n")
p.Start(wg)
runtime.Start(wg)

// graceful stop services
wg.Go(func() {
Expand All @@ -50,7 +32,7 @@ func main() {
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
fmt.Fprintf(color.Output, "\nshutting down server...\n\n")
p.Stop()
runtime.Stop()
})
wg.Wait()
}
2 changes: 1 addition & 1 deletion sail/examples/service/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (s *botService) String() string {
return fmt.Sprintf("listen on %s\n", color.GreenString("http://%s", s.addr))
}

func NewBotService(s *service.BaseHttpService[*gin.Engine], addr string) *botService {
func newBotService(s *service.BaseHttpService[*gin.Engine], addr string) *botService {
return &botService{
BaseHttpService: s,
addr: addr,
Expand Down
23 changes: 22 additions & 1 deletion sail/examples/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,31 @@ package service
import (
"net/http"

sail "github.com/alimy/mir/sail/v4/service"
"github.com/gin-gonic/gin"
)

func NewEngine() *gin.Engine {
func NewRuntime() sail.Runtime {
webEngine, botEngine := newEngine(), newEngine()
p := sail.NewHttpServerPool[*gin.Engine]()

webServerAddr, botServerAddr := ":8080", ":8081"
hsWeb := sail.NewBaseHttpService(p, webEngine, &http.Server{
Addr: webServerAddr,
})
hsBot := sail.NewBaseHttpService(p, botEngine, &http.Server{
Addr: botServerAddr,
})

webSrv := newWebService(hsWeb, webServerAddr)
botSrv := newBotService(hsBot, botServerAddr)

// init service
sail.MustInitService(webSrv, botSrv)
return p
}

func newEngine() *gin.Engine {
e := gin.New()
e.HandleMethodNotAllowed = true
e.Use(gin.Logger())
Expand Down
2 changes: 1 addition & 1 deletion sail/examples/service/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (s *webService) String() string {
return fmt.Sprintf("listen on %s\n", color.GreenString("http://%s", s.addr))
}

func NewWebService(s *service.BaseHttpService[*gin.Engine], addr string) *webService {
func newWebService(s *service.BaseHttpService[*gin.Engine], addr string) *webService {
return &webService{
BaseHttpService: s,
addr: addr,
Expand Down
8 changes: 8 additions & 0 deletions sail/service/runtime.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package service

import "github.com/sourcegraph/conc"

type Runtime interface {
Start(wg *conc.WaitGroup)
Stop()
}
4 changes: 4 additions & 0 deletions sail/service/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ import (
"github.com/sourcegraph/conc"
)

var (
_ Runtime = (*ServerPool[*httpServer[*http.ServeMux]])(nil)
)

const (
_actOnStart byte = iota
_actOnStop
Expand Down

0 comments on commit a7e0fdd

Please sign in to comment.