add 32bit supported

This commit is contained in:
sugar 2023-06-11 11:19:01 +08:00
parent b1e60de34a
commit 9c47b20864
6 changed files with 77 additions and 26 deletions

View File

@ -4,8 +4,8 @@ import (
"context" "context"
"embed" "embed"
"flag" "flag"
"git.nspix.com/golang/kos" "git.nspix.com/golang/kos"
"git.nspix.com/golang/kos/pkg/log"
) )
//go:embed web //go:embed web
@ -20,7 +20,6 @@ func (s *subServer) Start(ctx context.Context) (err error) {
} }
func (s *subServer) Stop() (err error) { func (s *subServer) Stop() (err error) {
log.Debugf("stopxxx")
return return
} }

View File

@ -4,15 +4,16 @@ import (
"context" "context"
"errors" "errors"
"fmt" "fmt"
"git.nspix.com/golang/kos/util/env" "math"
"github.com/sourcegraph/conc"
"net" "net"
"path" "path"
"runtime" "runtime"
"strings" "strings"
"sync" "sync"
"sync/atomic"
"time" "time"
"git.nspix.com/golang/kos/util/env"
"github.com/sourcegraph/conc"
) )
var ( var (
@ -20,13 +21,14 @@ var (
) )
type Server struct { type Server struct {
ctx context.Context ctx context.Context
sequence int64 sequenceLocker sync.Mutex
ctxMap sync.Map sequence int64
waitGroup conc.WaitGroup ctxMap sync.Map
middleware []Middleware waitGroup conc.WaitGroup
router *Router middleware []Middleware
l net.Listener router *Router
l net.Listener
} }
func (svr *Server) applyContext() *Context { func (svr *Server) applyContext() *Context {
@ -75,6 +77,16 @@ func (svr *Server) handle(ctx *Context, frame *Frame) {
} }
} }
func (svr *Server) nextSequence() int64 {
svr.sequenceLocker.Lock()
defer svr.sequenceLocker.Unlock()
if svr.sequence >= math.MaxInt64 {
svr.sequence = 1
}
svr.sequence++
return svr.sequence
}
func (svr *Server) process(conn net.Conn) { func (svr *Server) process(conn net.Conn) {
var ( var (
err error err error
@ -82,7 +94,7 @@ func (svr *Server) process(conn net.Conn) {
frame *Frame frame *Frame
) )
ctx = svr.applyContext() ctx = svr.applyContext()
ctx.reset(atomic.AddInt64(&svr.sequence, 1), conn) ctx.reset(svr.nextSequence(), conn)
svr.ctxMap.Store(ctx.Id, ctx) svr.ctxMap.Store(ctx.Id, ctx)
defer func() { defer func() {
_ = conn.Close() _ = conn.Close()

View File

@ -23,20 +23,20 @@ func (c *Conn) Read(b []byte) (n int, err error) {
} }
n, err = c.conn.Read(b[m:]) n, err = c.conn.Read(b[m:])
n += m n += m
atomic.AddInt64(&c.state.Traffic.In, int64(n)) c.state.IncTrafficIn(int64(n))
return return
} }
func (c *Conn) Write(b []byte) (n int, err error) { func (c *Conn) Write(b []byte) (n int, err error) {
n, err = c.conn.Write(b) n, err = c.conn.Write(b)
atomic.AddInt64(&c.state.Traffic.Out, int64(n)) c.state.IncTrafficOut(int64(n))
return return
} }
func (c *Conn) Close() error { func (c *Conn) Close() error {
if atomic.CompareAndSwapInt32(&c.exitFlag, 0, 1) { if atomic.CompareAndSwapInt32(&c.exitFlag, 0, 1) {
atomic.AddInt32(&c.state.Concurrency, -1) atomic.AddInt32(&c.state.Concurrency, -1)
atomic.AddInt64(&c.state.Request.Processed, 1) c.state.IncRequestProcessed(1)
return c.conn.Close() return c.conn.Close()
} }
return nil return nil

View File

@ -4,11 +4,12 @@ import (
"bytes" "bytes"
"context" "context"
"errors" "errors"
"github.com/sourcegraph/conc"
"io" "io"
"net" "net"
"sync/atomic" "sync/atomic"
"time" "time"
"github.com/sourcegraph/conc"
) )
const ( const (
@ -53,7 +54,7 @@ func (gw *Gateway) handle(conn net.Conn) {
defer func() { defer func() {
if atomic.LoadInt32(&success) != 1 { if atomic.LoadInt32(&success) != 1 {
atomic.AddInt32(&gw.state.Concurrency, -1) atomic.AddInt32(&gw.state.Concurrency, -1)
atomic.AddInt64(&gw.state.Request.Discarded, 1) gw.state.IncRequestDiscarded(1)
_ = conn.Close() _ = conn.Close()
} }
}() }()
@ -93,7 +94,7 @@ func (gw *Gateway) accept() {
} else { } else {
select { select {
case gw.ch <- conn: case gw.ch <- conn:
atomic.AddInt64(&gw.state.Request.Total, 1) gw.state.IncRequest(1)
case <-gw.ctx.Done(): case <-gw.ctx.Done():
return return
} }

View File

@ -1,6 +1,9 @@
package entry package entry
import "sync"
type State struct { type State struct {
mutex sync.Mutex
Accepting int32 `json:"accepting"` //是否正在接收连接 Accepting int32 `json:"accepting"` //是否正在接收连接
Processing int32 `json:"processing"` //是否正在处理连接 Processing int32 `json:"processing"` //是否正在处理连接
Concurrency int32 `json:"concurrency"` Concurrency int32 `json:"concurrency"`
@ -14,3 +17,33 @@ type State struct {
Out int64 `json:"out"` //出网流量 Out int64 `json:"out"` //出网流量
} `json:"traffic"` } `json:"traffic"`
} }
func (s *State) IncRequest(n int64) {
s.mutex.Lock()
s.Request.Total += n
s.mutex.Unlock()
}
func (s *State) IncRequestProcessed(n int64) {
s.mutex.Lock()
s.Request.Processed += n
s.mutex.Unlock()
}
func (s *State) IncRequestDiscarded(n int64) {
s.mutex.Lock()
s.Request.Discarded += n
s.mutex.Unlock()
}
func (s *State) IncTrafficIn(n int64) {
s.mutex.Lock()
s.Traffic.In += n
s.mutex.Unlock()
}
func (s *State) IncTrafficOut(n int64) {
s.mutex.Lock()
s.Traffic.Out += n
s.mutex.Unlock()
}

View File

@ -5,13 +5,6 @@ import (
"errors" "errors"
"flag" "flag"
"fmt" "fmt"
"git.nspix.com/golang/kos/entry"
"git.nspix.com/golang/kos/entry/cli"
"git.nspix.com/golang/kos/entry/http"
_ "git.nspix.com/golang/kos/pkg/cache"
"git.nspix.com/golang/kos/pkg/log"
"git.nspix.com/golang/kos/util/env"
"github.com/sourcegraph/conc"
"net" "net"
"net/http/pprof" "net/http/pprof"
"os" "os"
@ -22,6 +15,14 @@ import (
"sync/atomic" "sync/atomic"
"syscall" "syscall"
"time" "time"
"git.nspix.com/golang/kos/entry"
"git.nspix.com/golang/kos/entry/cli"
"git.nspix.com/golang/kos/entry/http"
_ "git.nspix.com/golang/kos/pkg/cache"
"git.nspix.com/golang/kos/pkg/log"
"git.nspix.com/golang/kos/util/env"
"github.com/sourcegraph/conc"
) )
var ( var (
@ -273,6 +274,11 @@ func (app *application) preStop() (err error) {
return return
} }
app.Log().Infof("server stopping") app.Log().Infof("server stopping")
if app.opts.server != nil {
if err = app.opts.server.Stop(); err != nil {
app.Log().Warnf("app server stop error: %s", err.Error())
}
}
app.cancelFunc(ErrStopping) app.cancelFunc(ErrStopping)
app.plugins.Range(func(key, value any) bool { app.plugins.Range(func(key, value any) bool {
if plugin, ok := value.(Plugin); ok { if plugin, ok := value.(Plugin); ok {