Browse Source

fix data race issues

pull/15/head
Jason 5 years ago
parent
commit
1b342bd0b8
  1. 13
      common/stats/session/session.go
  2. 3
      proxy/socks/tcp.go

13
common/stats/session/session.go

@ -93,11 +93,8 @@ func (s *simpleSessionStater) Start() error {
http.Redirect(w, r, StatsPath, 301) http.Redirect(w, r, StatsPath, 301)
}) })
mux.HandleFunc(StatsPath, sessionStatsHandler) mux.HandleFunc(StatsPath, sessionStatsHandler)
server := &http.Server{Addr: StatsAddr, Handler: mux} s.server = &http.Server{Addr: StatsAddr, Handler: mux}
s.server = server go s.server.ListenAndServe()
go func() {
_ = s.server.ListenAndServe()
}()
return nil return nil
} }
@ -118,14 +115,14 @@ func (s *simpleSessionStater) GetSession(key interface{}) *stats.Session {
} }
func (s *simpleSessionStater) RemoveSession(key interface{}) { func (s *simpleSessionStater) RemoveSession(key interface{}) {
s.mux.Lock()
defer s.mux.Unlock()
if sess, ok := s.sessions.Load(key); ok { if sess, ok := s.sessions.Load(key); ok {
// temporary workaround for slice race condition
s.mux.Lock()
s.completedSessions = append(s.completedSessions, *(sess.(*stats.Session))) s.completedSessions = append(s.completedSessions, *(sess.(*stats.Session)))
if len(s.completedSessions) > maxCompletedSessions { if len(s.completedSessions) > maxCompletedSessions {
s.completedSessions = s.completedSessions[1:] s.completedSessions = s.completedSessions[1:]
} }
s.mux.Unlock()
} }
s.sessions.Delete(key) s.sessions.Delete(key)
} }

3
proxy/socks/tcp.go

@ -84,7 +84,6 @@ type duplexConn interface {
} }
func (h *tcpHandler) relay(lhs, rhs net.Conn, sess *stats.Session) { func (h *tcpHandler) relay(lhs, rhs net.Conn, sess *stats.Session) {
var err error
upCh := make(chan struct{}) upCh := make(chan struct{})
cls := func(dir direction, interrupt bool) { cls := func(dir direction, interrupt bool) {
@ -109,6 +108,7 @@ func (h *tcpHandler) relay(lhs, rhs net.Conn, sess *stats.Session) {
// Uplink // Uplink
go func() { go func() {
var err error
if h.sessionStater != nil && sess != nil { if h.sessionStater != nil && sess != nil {
_, err = statsCopy(rhs, lhs, sess, dirUplink) _, err = statsCopy(rhs, lhs, sess, dirUplink)
} else { } else {
@ -123,6 +123,7 @@ func (h *tcpHandler) relay(lhs, rhs net.Conn, sess *stats.Session) {
}() }()
// Downlink // Downlink
var err error
if h.sessionStater != nil && sess != nil { if h.sessionStater != nil && sess != nil {
_, err = statsCopy(lhs, rhs, sess, dirDownlink) _, err = statsCopy(lhs, rhs, sess, dirDownlink)
} else { } else {

Loading…
Cancel
Save