Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
G
golangphone10
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
zhangjie
golangphone10
Commits
ab1316b4
Commit
ab1316b4
authored
Aug 16, 2021
by
zhangjiec
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
统计socket连接数量的变量改为原子操作
parent
5f865c50
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
45 additions
and
36 deletions
+45
-36
dc_phone10_http_server.go
devproduct/dcphone10/dc_phone10_http_server.go
+2
-2
dc_phone10_para_json.go
devproduct/dcphone10/dc_phone10_para_json.go
+1
-0
dc_phone10_tcp.go
devproduct/dcphone10/dc_phone10_tcp.go
+10
-3
setting.go
jeffutil/setting.go
+2
-2
jeff_hex_server.go
tcplongserver/jeff_hex_server.go
+30
-29
No files found.
devproduct/dcphone10/dc_phone10_http_server.go
View file @
ab1316b4
...
@@ -71,7 +71,7 @@ func (dev *DCPhone10) getOnlineList(c *gin.Context) {
...
@@ -71,7 +71,7 @@ func (dev *DCPhone10) getOnlineList(c *gin.Context) {
func
(
dev
*
DCPhone10
)
getOnlineTotal
(
c
*
gin
.
Context
)
{
func
(
dev
*
DCPhone10
)
getOnlineTotal
(
c
*
gin
.
Context
)
{
type
DataO
struct
{
type
DataO
struct
{
Count
int
`json:"count"`
Count
int
`json:"count"`
Socket
int
`json:"socket"`
Socket
int
32
`json:"socket"`
}
}
var
dataOut
DataO
var
dataOut
DataO
dev
.
ConnectMap
.
Range
(
func
(
_
,
_
interface
{})
bool
{
dev
.
ConnectMap
.
Range
(
func
(
_
,
_
interface
{})
bool
{
...
@@ -92,7 +92,7 @@ func (dev *DCPhone10) getOnlineAll(c *gin.Context) {
...
@@ -92,7 +92,7 @@ func (dev *DCPhone10) getOnlineAll(c *gin.Context) {
type
DataO
struct
{
type
DataO
struct
{
Count
int
`json:"count"`
Count
int
`json:"count"`
DevID
[]
uint32
`json:"devId"`
DevID
[]
uint32
`json:"devId"`
Socket
int
`json:"socket"`
Socket
int
32
`json:"socket"`
}
}
var
dataOut
DataO
var
dataOut
DataO
//dataOut.DevID = make([]uint32,0,5000)
//dataOut.DevID = make([]uint32,0,5000)
...
...
devproduct/dcphone10/dc_phone10_para_json.go
View file @
ab1316b4
...
@@ -53,6 +53,7 @@ type devUpRestartLogin struct {
...
@@ -53,6 +53,7 @@ type devUpRestartLogin struct {
DevID
[]
uint32
`json:"devId"`
DevID
[]
uint32
`json:"devId"`
Value
struct
{
Value
struct
{
ResetReason
byte
`json:"reset_reason"`
ResetReason
byte
`json:"reset_reason"`
ResetCode
byte
`json:"reset_code"`
Imei0
string
`json:"imei_0"`
Imei0
string
`json:"imei_0"`
Iccid0
string
`json:"iccid_0"`
Iccid0
string
`json:"iccid_0"`
Imei1
string
`json:"imei_1"`
Imei1
string
`json:"imei_1"`
...
...
devproduct/dcphone10/dc_phone10_tcp.go
View file @
ab1316b4
...
@@ -102,10 +102,10 @@ func (dev *DCPhone10) response(data *tcplongserver.HexData) {
...
@@ -102,10 +102,10 @@ func (dev *DCPhone10) response(data *tcplongserver.HexData) {
}{
0
,
0
},
}{
0
,
0
},
}
}
if
data
.
Data
[
0
]
|
1
!=
0
{
if
data
.
Data
[
0
]
&
1
!=
0
{
jsonStruct
.
Value
.
Call
=
1
jsonStruct
.
Value
.
Call
=
1
}
}
if
data
.
Data
[
0
]
|
2
!=
0
{
if
data
.
Data
[
0
]
&
2
!=
0
{
jsonStruct
.
Value
.
Net
=
1
jsonStruct
.
Value
.
Net
=
1
}
}
...
@@ -265,7 +265,14 @@ func (dev *DCPhone10) response(data *tcplongserver.HexData) {
...
@@ -265,7 +265,14 @@ func (dev *DCPhone10) response(data *tcplongserver.HexData) {
DevTime
:
data
.
TimeUtc
,
DevTime
:
data
.
TimeUtc
,
DevID
:
[]
uint32
{
data
.
DevID
},
DevID
:
[]
uint32
{
data
.
DevID
},
}
}
jsonStruct
.
Value
.
ResetReason
=
data
.
Data
[
0
]
// test
if
data
.
Data
[
0
]
&
0x03
==
0x03
{
jsonStruct
.
Value
.
ResetReason
=
0
// 正常上电
}
else
if
data
.
Data
[
0
]
==
0x40
{
jsonStruct
.
Value
.
ResetReason
=
1
// 软件复位
}
else
{
jsonStruct
.
Value
.
ResetReason
=
2
// 异常复位
}
jsonStruct
.
Value
.
ResetCode
=
data
.
Data
[
0
]
// test
jsonStruct
.
Value
.
Imei0
=
string
(
data
.
Data
[
1
:
16
])
//从索引1到索引16(不包含)
jsonStruct
.
Value
.
Imei0
=
string
(
data
.
Data
[
1
:
16
])
//从索引1到索引16(不包含)
jsonStruct
.
Value
.
Iccid0
=
string
(
data
.
Data
[
16
:
36
])
jsonStruct
.
Value
.
Iccid0
=
string
(
data
.
Data
[
16
:
36
])
jsonStruct
.
Value
.
Imei1
=
string
(
data
.
Data
[
36
:
51
])
jsonStruct
.
Value
.
Imei1
=
string
(
data
.
Data
[
36
:
51
])
...
...
jeffutil/setting.go
View file @
ab1316b4
...
@@ -15,7 +15,7 @@ var (
...
@@ -15,7 +15,7 @@ var (
HttpClientTimeOut
time
.
Duration
HttpClientTimeOut
time
.
Duration
TcpLongPort
string
TcpLongPort
string
TcpLongMaxConnections
int
TcpLongMaxConnections
int
32
TcpLongNewConnReadDeadline
time
.
Duration
TcpLongNewConnReadDeadline
time
.
Duration
TcpLongNewConnRightfulTimeout
int64
TcpLongNewConnRightfulTimeout
int64
TcpLongOldConnReadDeadline
time
.
Duration
TcpLongOldConnReadDeadline
time
.
Duration
...
@@ -47,7 +47,7 @@ func ConfigInit() {
...
@@ -47,7 +47,7 @@ func ConfigInit() {
HttpClientTimeOut
=
time
.
Duration
(
file
.
Section
(
"http_client"
)
.
Key
(
"TimeOut"
)
.
MustInt
(
5
))
*
time
.
Second
HttpClientTimeOut
=
time
.
Duration
(
file
.
Section
(
"http_client"
)
.
Key
(
"TimeOut"
)
.
MustInt
(
5
))
*
time
.
Second
TcpLongPort
=
file
.
Section
(
"tcp_long_server"
)
.
Key
(
"TcpPort"
)
.
MustString
(
"60000"
)
TcpLongPort
=
file
.
Section
(
"tcp_long_server"
)
.
Key
(
"TcpPort"
)
.
MustString
(
"60000"
)
TcpLongMaxConnections
=
int
(
file
.
Section
(
"tcp_long_server"
)
.
Key
(
"MaxConnections"
)
.
MustUint
(
20000
))
TcpLongMaxConnections
=
int
32
(
file
.
Section
(
"tcp_long_server"
)
.
Key
(
"MaxConnections"
)
.
MustUint
(
20000
))
TcpLongNewConnReadDeadline
=
time
.
Duration
(
file
.
Section
(
"tcp_long_server"
)
.
Key
(
"NewConnReadDeadline"
)
.
MustInt
(
60
))
*
time
.
Second
TcpLongNewConnReadDeadline
=
time
.
Duration
(
file
.
Section
(
"tcp_long_server"
)
.
Key
(
"NewConnReadDeadline"
)
.
MustInt
(
60
))
*
time
.
Second
TcpLongNewConnRightfulTimeout
=
file
.
Section
(
"tcp_long_server"
)
.
Key
(
"NewConnRightfulTimeout"
)
.
MustInt64
(
120
)
TcpLongNewConnRightfulTimeout
=
file
.
Section
(
"tcp_long_server"
)
.
Key
(
"NewConnRightfulTimeout"
)
.
MustInt64
(
120
)
TcpLongOldConnReadDeadline
=
time
.
Duration
(
file
.
Section
(
"tcp_long_server"
)
.
Key
(
"OldConnReadDeadline"
)
.
MustInt
(
180
))
*
time
.
Second
TcpLongOldConnReadDeadline
=
time
.
Duration
(
file
.
Section
(
"tcp_long_server"
)
.
Key
(
"OldConnReadDeadline"
)
.
MustInt
(
180
))
*
time
.
Second
...
...
tcplongserver/jeff_hex_server.go
View file @
ab1316b4
...
@@ -8,6 +8,7 @@ import (
...
@@ -8,6 +8,7 @@ import (
"net"
"net"
"strconv"
"strconv"
"sync"
"sync"
"sync/atomic"
"time"
"time"
)
)
...
@@ -19,9 +20,9 @@ type receiveCallbackFunc func(*net.TCPConn, []byte) uint32 //定义接收到数
...
@@ -19,9 +20,9 @@ type receiveCallbackFunc func(*net.TCPConn, []byte) uint32 //定义接收到数
type
TCPLongServer
struct
{
type
TCPLongServer
struct
{
TCPReceive
receiveCallbackFunc
TCPReceive
receiveCallbackFunc
ConnSocketCount
int
ConnSocketCount
int32
ConnectMap
sync
.
Map
// 用这个可能性能不行,测试下,不行就用map加锁
ConnectMap
sync
.
Map
// 用这个可能性能不行,测试下,不行就用map加锁
rwConnSocketCount
chan
int
// rwConnSocketCount chan int // 20210815 将对ConnSocketCount的操作修改为原子操作
// ConnectMapRWMutex sync.RWMutex
// ConnectMapRWMutex sync.RWMutex
// ConnectMap map[uint32]*net.TCPConn // Connection
// ConnectMap map[uint32]*net.TCPConn // Connection
}
}
...
@@ -42,17 +43,17 @@ func (s *TCPLongServer) TCPStart(port string) {
...
@@ -42,17 +43,17 @@ func (s *TCPLongServer) TCPStart(port string) {
zap
.
Error
(
err
))
zap
.
Error
(
err
))
}
}
s
.
rwConnSocketCount
=
make
(
chan
int
)
//s.rwConnSocketCount = make(chan int) // 20210815 将对ConnSocketCount的操作修改为原子操作
go
func
()
{
//
go func() {
for
{
//
for {
s
.
ConnSocketCount
+=
<-
s
.
rwConnSocketCount
//
s.ConnSocketCount += <-s.rwConnSocketCount
if
s
.
ConnSocketCount
<
0
{
//
if s.ConnSocketCount < 0 {
s
.
ConnSocketCount
=
0
//
s.ConnSocketCount = 0
logger
.
Log
.
Error
(
"s.ConnSocketCount <0"
,
//
logger.Log.Error("s.ConnSocketCount <0",
zap
.
Int
(
"ConnSocketCount"
,
s
.
ConnSocketCount
))
//
zap.Int("ConnSocketCount", s.ConnSocketCount))
}
//
}
}
//
}
}()
//
}()
// 启动server
// 启动server
for
{
for
{
...
@@ -87,18 +88,18 @@ func (s *TCPLongServer) TCPStart(port string) {
...
@@ -87,18 +88,18 @@ func (s *TCPLongServer) TCPStart(port string) {
func
(
s
*
TCPLongServer
)
connectionHandle
(
conn
*
net
.
TCPConn
,
callBack
receiveCallbackFunc
)
{
func
(
s
*
TCPLongServer
)
connectionHandle
(
conn
*
net
.
TCPConn
,
callBack
receiveCallbackFunc
)
{
var
myDevID
uint32
// golang 默认0
var
myDevID
uint32
// golang 默认0
s
.
rwConnSocketCount
<-
1
atomic
.
AddInt32
(
&
s
.
ConnSocketCount
,
1
)
defer
func
()
{
defer
func
()
{
s
.
rwConnSocketCount
<-
-
1
//
s.rwConnSocketCount <- -1
//
if s.ConnSocketCount > 0 {
if
s
.
ConnSocketCount
>
0
{
// s.ConnSocketCount--
atomic
.
AddInt32
(
&
s
.
ConnSocketCount
,
-
1
)
//
} else {
}
else
{
//
logger.Log.Error("s.ConnSocketCount error",
logger
.
Log
.
Error
(
"s.ConnSocketCount error"
,
//
zap.String("Src", "TCP-SERVER-connectionHandle"),
zap
.
String
(
"Src"
,
"TCP-SERVER-connectionHandle"
),
//
zap.Uint32("DevId", myDevID),
zap
.
Uint32
(
"DevId"
,
myDevID
),
//
zap.String("Conn", conn.RemoteAddr().String()),
zap
.
String
(
"Conn"
,
conn
.
RemoteAddr
()
.
String
()),
// zap.Int
("ConnSocketCount", s.ConnSocketCount))
zap
.
Int32
(
"ConnSocketCount"
,
s
.
ConnSocketCount
))
//
}
}
if
myDevID
!=
0
{
if
myDevID
!=
0
{
if
connMyID
,
ok
:=
s
.
ConnectMap
.
Load
(
myDevID
);
ok
{
//查一下设备旧链接还在的话,关掉
if
connMyID
,
ok
:=
s
.
ConnectMap
.
Load
(
myDevID
);
ok
{
//查一下设备旧链接还在的话,关掉
if
co
,
ok
:=
connMyID
.
(
*
net
.
TCPConn
);
ok
{
// 类型断言
if
co
,
ok
:=
connMyID
.
(
*
net
.
TCPConn
);
ok
{
// 类型断言
...
@@ -157,11 +158,11 @@ func (s *TCPLongServer) connectionHandle(conn *net.TCPConn, callBack receiveCall
...
@@ -157,11 +158,11 @@ func (s *TCPLongServer) connectionHandle(conn *net.TCPConn, callBack receiveCall
// }
// }
//}()
//}()
if
s
.
ConnSocketCount
>
jeffutil
.
TcpLongMaxConnections
{
//限制最大连接数2万个配置文件 最大连接限制数添加到配置文件
if
s
.
ConnSocketCount
>
jeffutil
.
TcpLongMaxConnections
{
//限制最大连接数2万个配置文件 最大连接限制数添加到配置文件
logger
.
Log
.
Warn
(
"ConnSocketCount overrun:"
+
strconv
.
Itoa
(
jeffutil
.
TcpLongMaxConnections
),
logger
.
Log
.
Warn
(
"ConnSocketCount overrun:"
+
strconv
.
Itoa
(
int
(
jeffutil
.
TcpLongMaxConnections
)
),
zap
.
String
(
"Src"
,
"TCP-SERVER-connectionHandle"
),
zap
.
String
(
"Src"
,
"TCP-SERVER-connectionHandle"
),
zap
.
Uint32
(
"DevId"
,
myDevID
),
zap
.
Uint32
(
"DevId"
,
myDevID
),
zap
.
String
(
"Conn"
,
conn
.
RemoteAddr
()
.
String
()),
zap
.
String
(
"Conn"
,
conn
.
RemoteAddr
()
.
String
()),
zap
.
Int
(
"ConnSocketCount"
,
s
.
ConnSocketCount
))
zap
.
Int
32
(
"ConnSocketCount"
,
s
.
ConnSocketCount
))
return
return
}
}
...
@@ -178,7 +179,7 @@ func (s *TCPLongServer) connectionHandle(conn *net.TCPConn, callBack receiveCall
...
@@ -178,7 +179,7 @@ func (s *TCPLongServer) connectionHandle(conn *net.TCPConn, callBack receiveCall
zap
.
String
(
"Src"
,
"TCP-SERVER-connectionHandle"
),
zap
.
String
(
"Src"
,
"TCP-SERVER-connectionHandle"
),
// zap.Uint32("DevId", myDevID),
// zap.Uint32("DevId", myDevID),
zap
.
String
(
"Conn"
,
conn
.
RemoteAddr
()
.
String
()),
zap
.
String
(
"Conn"
,
conn
.
RemoteAddr
()
.
String
()),
zap
.
Int
(
"ConnSocketCount"
,
s
.
ConnSocketCount
))
zap
.
Int
32
(
"ConnSocketCount"
,
s
.
ConnSocketCount
))
return
return
}
}
if
err
:=
conn
.
SetReadDeadline
(
time
.
Now
()
.
Add
(
jeffutil
.
TcpLongNewConnReadDeadline
));
err
!=
nil
{
// 配置文件 新连接1分钟超时未收到数据(配置文件用秒)
if
err
:=
conn
.
SetReadDeadline
(
time
.
Now
()
.
Add
(
jeffutil
.
TcpLongNewConnReadDeadline
));
err
!=
nil
{
// 配置文件 新连接1分钟超时未收到数据(配置文件用秒)
...
@@ -187,7 +188,7 @@ func (s *TCPLongServer) connectionHandle(conn *net.TCPConn, callBack receiveCall
...
@@ -187,7 +188,7 @@ func (s *TCPLongServer) connectionHandle(conn *net.TCPConn, callBack receiveCall
zap
.
Uint32
(
"DevId"
,
myDevID
),
zap
.
Uint32
(
"DevId"
,
myDevID
),
zap
.
String
(
"Conn"
,
conn
.
RemoteAddr
()
.
String
()),
zap
.
String
(
"Conn"
,
conn
.
RemoteAddr
()
.
String
()),
zap
.
Error
(
err
),
zap
.
Error
(
err
),
zap
.
Int
(
"ConnSocketCount"
,
s
.
ConnSocketCount
))
zap
.
Int
32
(
"ConnSocketCount"
,
s
.
ConnSocketCount
))
return
return
}
}
}
else
{
}
else
{
...
@@ -197,7 +198,7 @@ func (s *TCPLongServer) connectionHandle(conn *net.TCPConn, callBack receiveCall
...
@@ -197,7 +198,7 @@ func (s *TCPLongServer) connectionHandle(conn *net.TCPConn, callBack receiveCall
zap
.
Uint32
(
"DevId"
,
myDevID
),
zap
.
Uint32
(
"DevId"
,
myDevID
),
zap
.
String
(
"Conn"
,
conn
.
RemoteAddr
()
.
String
()),
zap
.
String
(
"Conn"
,
conn
.
RemoteAddr
()
.
String
()),
zap
.
Error
(
err
),
zap
.
Error
(
err
),
zap
.
Int
(
"ConnSocketCount"
,
s
.
ConnSocketCount
))
zap
.
Int
32
(
"ConnSocketCount"
,
s
.
ConnSocketCount
))
return
return
}
}
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment