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)
})
mux.HandleFunc(StatsPath, sessionStatsHandler)
server := &http.Server{Addr: StatsAddr, Handler: mux}
s.server = server
go func() {
_ = s.server.ListenAndServe()
}()
s.server = &http.Server{Addr: StatsAddr, Handler: mux}
go s.server.ListenAndServe()
return nil
}
@ -118,14 +115,14 @@ func (s *simpleSessionStater) GetSession(key interface{}) *stats.Session {
}
func (s *simpleSessionStater) RemoveSession(key interface{}) {
s.mux.Lock()
defer s.mux.Unlock()
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)))
if len(s.completedSessions) > maxCompletedSessions {
s.completedSessions = s.completedSessions[1:]
}
s.mux.Unlock()
}
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) {
var err error
upCh := make(chan struct{})
cls := func(dir direction, interrupt bool) {
@ -109,6 +108,7 @@ func (h *tcpHandler) relay(lhs, rhs net.Conn, sess *stats.Session) {
// Uplink
go func() {
var err error
if h.sessionStater != nil && sess != nil {
_, err = statsCopy(rhs, lhs, sess, dirUplink)
} else {
@ -123,6 +123,7 @@ func (h *tcpHandler) relay(lhs, rhs net.Conn, sess *stats.Session) {
}()
// Downlink
var err error
if h.sessionStater != nil && sess != nil {
_, err = statsCopy(lhs, rhs, sess, dirDownlink)
} else {

Loading…
Cancel
Save