Skip to content

Commit

Permalink
Upgrade to the new master-go with version 1.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
zhengshuxin committed Feb 28, 2023
1 parent ce560a1 commit 24b0b30
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
40 changes: 37 additions & 3 deletions gin_service.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package master_gin

import (
"fmt"
"github.com/pkg/errors"
"log"
"net"
"net/http"
"sync"

"github.com/acl-dev/master-go"
Expand All @@ -14,6 +16,8 @@ var (
g sync.WaitGroup // Used to wait for service to stop.
Version = "0.9.2"
)
type AcceptFunc func(net.Conn)
type CloseFunc func(net.Conn)

type GinServ struct {
Listener net.Listener
Expand All @@ -23,23 +27,53 @@ type GinServ struct {
type GinService struct {
Alone bool
Servers []*GinServ
AcceptHandler AcceptFunc
CloseHandler CloseFunc
}

// Run begin to start all the listening servers after Init() called.
func (service *GinService) Run() {
g.Add(len(service.Servers))
for _, s := range service.Servers {
startServer(s.Listener, s.Engine)
service.startServer(s.Listener, s.Engine)
}

g.Wait()
}

func startServer(listener net.Listener, engine *gin.Engine) {
func (service *GinService) startServer(listener net.Listener, engine *gin.Engine) {
go func() {
defer g.Done()

_ = engine.RunListener(listener)
//_ = engine.RunListener(listener)
server := &http.Server {
Handler: engine,
ConnState: func(conn net.Conn, state http.ConnState) {
switch state {
case http.StateNew:
master.ConnCountInc()
if service.AcceptHandler != nil {
service.AcceptHandler(conn)
}
break
case http.StateActive:
break
case http.StateIdle:
break
case http.StateClosed:
case http.StateHijacked:
master.ConnCountDec();
if service.CloseHandler != nil {
service.CloseHandler(conn)
}
break
default:
break
}
fmt.Printf("Connection status=%d from %sr\r\n", int(state), conn.RemoteAddr())
},
}
server.Serve(listener)
}()
}

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/acl-dev/master-gin
go 1.16

require (
github.com/acl-dev/master-go v1.0.8
github.com/acl-dev/master-go v1.1.0
github.com/gin-gonic/gin v1.7.7
github.com/pkg/errors v0.9.1
)

0 comments on commit 24b0b30

Please sign in to comment.