From 6b4824ffbadad878076378b0ca052321c0605c01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Tue, 2 Aug 2022 18:47:23 +0800 Subject: [PATCH] Minor fixes --- common/canceler/packet.go | 8 ++++++++ experimental/clashapi/server.go | 13 ++++++++----- experimental/clashapi/trafficontrol/manager.go | 10 ++++++---- 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/common/canceler/packet.go b/common/canceler/packet.go index 1590e7ed..a964896e 100644 --- a/common/canceler/packet.go +++ b/common/canceler/packet.go @@ -4,6 +4,7 @@ import ( "context" "time" + "github.com/sagernet/sing/common" "github.com/sagernet/sing/common/buf" M "github.com/sagernet/sing/common/metadata" N "github.com/sagernet/sing/common/network" @@ -36,6 +37,13 @@ func (c *PacketConn) WritePacket(buffer *buf.Buffer, destination M.Socksaddr) er return err } +func (c *PacketConn) Close() error { + return common.Close( + c.PacketConn, + c.instance, + ) +} + func (c *PacketConn) Upstream() any { return c.PacketConn } diff --git a/experimental/clashapi/server.go b/experimental/clashapi/server.go index 9ec1d2bf..f6255a79 100644 --- a/experimental/clashapi/server.go +++ b/experimental/clashapi/server.go @@ -35,20 +35,21 @@ type Server struct { httpServer *http.Server trafficManager *trafficontrol.Manager urlTestHistory *urltest.HistoryStorage + tcpListener net.Listener } func NewServer(router adapter.Router, logFactory log.ObservableFactory, options option.ClashAPIOptions) *Server { trafficManager := trafficontrol.NewManager() chiRouter := chi.NewRouter() server := &Server{ - router, - logFactory.NewLogger("clash-api"), - &http.Server{ + router: router, + logger: logFactory.NewLogger("clash-api"), + httpServer: &http.Server{ Addr: options.ExternalController, Handler: chiRouter, }, - trafficManager, - urltest.NewHistoryStorage(), + trafficManager: trafficManager, + urlTestHistory: urltest.NewHistoryStorage(), } cors := cors.New(cors.Options{ AllowedOrigins: []string{"*"}, @@ -91,6 +92,7 @@ func (s *Server) Start() error { return E.Cause(err, "external controller listen error") } s.logger.Info("restful api listening at ", listener.Addr()) + s.tcpListener = listener go func() { err = s.httpServer.Serve(listener) if err != nil && !errors.Is(err, http.ErrServerClosed) { @@ -102,6 +104,7 @@ func (s *Server) Start() error { func (s *Server) Close() error { s.httpServer.Close() + s.tcpListener.Close() s.trafficManager.Close() return nil } diff --git a/experimental/clashapi/trafficontrol/manager.go b/experimental/clashapi/trafficontrol/manager.go index 8b0c6e21..69298788 100644 --- a/experimental/clashapi/trafficontrol/manager.go +++ b/experimental/clashapi/trafficontrol/manager.go @@ -81,16 +81,18 @@ func (m *Manager) ResetStatistic() { } func (m *Manager) handle() { + var uploadTemp int64 + var downloadTemp int64 for { select { case <-m.done: return case <-m.ticker.C: } - m.uploadBlip.Store(m.uploadTemp.Load()) - m.uploadTemp.Store(0) - m.downloadBlip.Store(m.downloadTemp.Load()) - m.downloadTemp.Store(0) + uploadTemp = m.uploadTemp.Swap(0) + downloadTemp = m.downloadTemp.Swap(0) + m.uploadBlip.Store(uploadTemp) + m.downloadBlip.Store(downloadTemp) } }