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
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
48 additions
and
39 deletions
+48
-39
dc_phone10_http_server.go
devproduct/dcphone10/dc_phone10_http_server.go
+3
-3
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
+32
-31
No files found.
devproduct/dcphone10/dc_phone10_http_server.go
View file @
ab1316b4
...
...
@@ -70,8 +70,8 @@ func (dev *DCPhone10) getOnlineList(c *gin.Context) {
//查在线设备总数量
func
(
dev
*
DCPhone10
)
getOnlineTotal
(
c
*
gin
.
Context
)
{
type
DataO
struct
{
Count
int
`json:"count"`
Socket
int
`json:"socket"`
Count
int
`json:"count"`
Socket
int
32
`json:"socket"`
}
var
dataOut
DataO
dev
.
ConnectMap
.
Range
(
func
(
_
,
_
interface
{})
bool
{
...
...
@@ -92,7 +92,7 @@ func (dev *DCPhone10) getOnlineAll(c *gin.Context) {
type
DataO
struct
{
Count
int
`json:"count"`
DevID
[]
uint32
`json:"devId"`
Socket
int
`json:"socket"`
Socket
int
32
`json:"socket"`
}
var
dataOut
DataO
//dataOut.DevID = make([]uint32,0,5000)
...
...
devproduct/dcphone10/dc_phone10_para_json.go
View file @
ab1316b4
...
...
@@ -53,6 +53,7 @@ type devUpRestartLogin struct {
DevID
[]
uint32
`json:"devId"`
Value
struct
{
ResetReason
byte
`json:"reset_reason"`
ResetCode
byte
`json:"reset_code"`
Imei0
string
`json:"imei_0"`
Iccid0
string
`json:"iccid_0"`
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) {
}{
0
,
0
},
}
if
data
.
Data
[
0
]
|
1
!=
0
{
if
data
.
Data
[
0
]
&
1
!=
0
{
jsonStruct
.
Value
.
Call
=
1
}
if
data
.
Data
[
0
]
|
2
!=
0
{
if
data
.
Data
[
0
]
&
2
!=
0
{
jsonStruct
.
Value
.
Net
=
1
}
...
...
@@ -265,7 +265,14 @@ func (dev *DCPhone10) response(data *tcplongserver.HexData) {
DevTime
:
data
.
TimeUtc
,
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
.
Iccid0
=
string
(
data
.
Data
[
16
:
36
])
jsonStruct
.
Value
.
Imei1
=
string
(
data
.
Data
[
36
:
51
])
...
...
jeffutil/setting.go
View file @
ab1316b4
...
...
@@ -15,7 +15,7 @@ var (
HttpClientTimeOut
time
.
Duration
TcpLongPort
string
TcpLongMaxConnections
int
TcpLongMaxConnections
int
32
TcpLongNewConnReadDeadline
time
.
Duration
TcpLongNewConnRightfulTimeout
int64
TcpLongOldConnReadDeadline
time
.
Duration
...
...
@@ -47,7 +47,7 @@ func ConfigInit() {
HttpClientTimeOut
=
time
.
Duration
(
file
.
Section
(
"http_client"
)
.
Key
(
"TimeOut"
)
.
MustInt
(
5
))
*
time
.
Second
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
TcpLongNewConnRightfulTimeout
=
file
.
Section
(
"tcp_long_server"
)
.
Key
(
"NewConnRightfulTimeout"
)
.
MustInt64
(
120
)
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 (
"net"
"strconv"
"sync"
"sync/atomic"
"time"
)
...
...
@@ -18,10 +19,10 @@ type ITCPLongServer interface {
type
receiveCallbackFunc
func
(
*
net
.
TCPConn
,
[]
byte
)
uint32
//定义接收到数据的回调函数的函数类型
type
TCPLongServer
struct
{
TCPReceive
receiveCallbackFunc
ConnSocketCount
int
ConnectMap
sync
.
Map
// 用这个可能性能不行,测试下,不行就用map加锁
rwConnSocketCount
chan
int
TCPReceive
receiveCallbackFunc
ConnSocketCount
int32
ConnectMap
sync
.
Map
// 用这个可能性能不行,测试下,不行就用map加锁
// rwConnSocketCount chan int // 20210815 将对ConnSocketCount的操作修改为原子操作
// ConnectMapRWMutex sync.RWMutex
// ConnectMap map[uint32]*net.TCPConn // Connection
}
...
...
@@ -42,17 +43,17 @@ func (s *TCPLongServer) TCPStart(port string) {
zap
.
Error
(
err
))
}
s
.
rwConnSocketCount
=
make
(
chan
int
)
go
func
()
{
for
{
s
.
ConnSocketCount
+=
<-
s
.
rwConnSocketCount
if
s
.
ConnSocketCount
<
0
{
s
.
ConnSocketCount
=
0
logger
.
Log
.
Error
(
"s.ConnSocketCount <0"
,
zap
.
Int
(
"ConnSocketCount"
,
s
.
ConnSocketCount
))
}
}
}()
//s.rwConnSocketCount = make(chan int) // 20210815 将对ConnSocketCount的操作修改为原子操作
//
go func() {
//
for {
//
s.ConnSocketCount += <-s.rwConnSocketCount
//
if s.ConnSocketCount < 0 {
//
s.ConnSocketCount = 0
//
logger.Log.Error("s.ConnSocketCount <0",
//
zap.Int("ConnSocketCount", s.ConnSocketCount))
//
}
//
}
//
}()
// 启动server
for
{
...
...
@@ -87,18 +88,18 @@ func (s *TCPLongServer) TCPStart(port string) {
func
(
s
*
TCPLongServer
)
connectionHandle
(
conn
*
net
.
TCPConn
,
callBack
receiveCallbackFunc
)
{
var
myDevID
uint32
// golang 默认0
s
.
rwConnSocketCount
<-
1
atomic
.
AddInt32
(
&
s
.
ConnSocketCount
,
1
)
defer
func
()
{
s
.
rwConnSocketCount
<-
-
1
//
if s.ConnSocketCount > 0 {
// s.ConnSocketCount--
//
} else {
//
logger.Log.Error("s.ConnSocketCount error",
//
zap.String("Src", "TCP-SERVER-connectionHandle"),
//
zap.Uint32("DevId", myDevID),
//
zap.String("Conn", conn.RemoteAddr().String()),
// zap.Int
("ConnSocketCount", s.ConnSocketCount))
//
}
//
s.rwConnSocketCount <- -1
if
s
.
ConnSocketCount
>
0
{
atomic
.
AddInt32
(
&
s
.
ConnSocketCount
,
-
1
)
}
else
{
logger
.
Log
.
Error
(
"s.ConnSocketCount error"
,
zap
.
String
(
"Src"
,
"TCP-SERVER-connectionHandle"
),
zap
.
Uint32
(
"DevId"
,
myDevID
),
zap
.
String
(
"Conn"
,
conn
.
RemoteAddr
()
.
String
()),
zap
.
Int32
(
"ConnSocketCount"
,
s
.
ConnSocketCount
))
}
if
myDevID
!=
0
{
if
connMyID
,
ok
:=
s
.
ConnectMap
.
Load
(
myDevID
);
ok
{
//查一下设备旧链接还在的话,关掉
if
co
,
ok
:=
connMyID
.
(
*
net
.
TCPConn
);
ok
{
// 类型断言
...
...
@@ -157,11 +158,11 @@ func (s *TCPLongServer) connectionHandle(conn *net.TCPConn, callBack receiveCall
// }
//}()
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
.
Uint32
(
"DevId"
,
myDevID
),
zap
.
String
(
"Conn"
,
conn
.
RemoteAddr
()
.
String
()),
zap
.
Int
(
"ConnSocketCount"
,
s
.
ConnSocketCount
))
zap
.
Int
32
(
"ConnSocketCount"
,
s
.
ConnSocketCount
))
return
}
...
...
@@ -178,7 +179,7 @@ func (s *TCPLongServer) connectionHandle(conn *net.TCPConn, callBack receiveCall
zap
.
String
(
"Src"
,
"TCP-SERVER-connectionHandle"
),
// zap.Uint32("DevId", myDevID),
zap
.
String
(
"Conn"
,
conn
.
RemoteAddr
()
.
String
()),
zap
.
Int
(
"ConnSocketCount"
,
s
.
ConnSocketCount
))
zap
.
Int
32
(
"ConnSocketCount"
,
s
.
ConnSocketCount
))
return
}
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
zap
.
Uint32
(
"DevId"
,
myDevID
),
zap
.
String
(
"Conn"
,
conn
.
RemoteAddr
()
.
String
()),
zap
.
Error
(
err
),
zap
.
Int
(
"ConnSocketCount"
,
s
.
ConnSocketCount
))
zap
.
Int
32
(
"ConnSocketCount"
,
s
.
ConnSocketCount
))
return
}
}
else
{
...
...
@@ -197,7 +198,7 @@ func (s *TCPLongServer) connectionHandle(conn *net.TCPConn, callBack receiveCall
zap
.
Uint32
(
"DevId"
,
myDevID
),
zap
.
String
(
"Conn"
,
conn
.
RemoteAddr
()
.
String
()),
zap
.
Error
(
err
),
zap
.
Int
(
"ConnSocketCount"
,
s
.
ConnSocketCount
))
zap
.
Int
32
(
"ConnSocketCount"
,
s
.
ConnSocketCount
))
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