add lock free queue

This commit is contained in:
ffdfgdfg
2019-10-21 11:55:29 +08:00
parent c2f4510a0f
commit 23b023c562
5 changed files with 419 additions and 131 deletions

View File

@@ -34,6 +34,8 @@ type Mux struct {
}
func NewMux(c net.Conn, connType string) *Mux {
//c.(*net.TCPConn).SetReadBuffer(0)
//c.(*net.TCPConn).SetWriteBuffer(0)
m := &Mux{
conn: c,
connMap: NewConnMap(),
@@ -173,10 +175,6 @@ func (s *Mux) ping() {
select {
case <-ticker.C:
}
//Avoid going beyond the scope
if (math.MaxInt32 - s.id) < 10000 {
s.id = 0
}
now, _ := time.Now().UTC().MarshalText()
s.sendInfo(common.MUX_PING_FLAG, common.MUX_PING, now)
if !s.pingTimer.Stop() {
@@ -321,6 +319,10 @@ func (s *Mux) Close() error {
//get new connId as unique flag
func (s *Mux) getId() (id int32) {
//Avoid going beyond the scope
if (math.MaxInt32 - s.id) < 10000 {
atomic.SwapInt32(&s.id, 0)
}
id = atomic.AddInt32(&s.id, 1)
if _, ok := s.connMap.Get(id); ok {
s.getId()