Chore: unexported globals with _

Use uber’s go style.
This commit is contained in:
xjasonlyu
2021-02-11 16:10:40 +08:00
parent 7e6a9b6a09
commit b54d9936f9
4 changed files with 19 additions and 19 deletions

View File

@@ -31,7 +31,7 @@ func getConnections(w http.ResponseWriter, r *http.Request) {
return return
} }
conn, err := upgrader.Upgrade(w, r, nil) conn, err := _upgrader.Upgrade(w, r, nil)
if err != nil { if err != nil {
return return
} }

View File

@@ -19,7 +19,7 @@ import (
) )
var ( var (
upgrader = websocket.Upgrader{ _upgrader = websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool { CheckOrigin: func(r *http.Request) bool {
return true return true
}, },
@@ -114,7 +114,7 @@ func getLogs(w http.ResponseWriter, r *http.Request) {
var wsConn *websocket.Conn var wsConn *websocket.Conn
if websocket.IsWebSocketUpgrade(r) { if websocket.IsWebSocketUpgrade(r) {
wsConn, err = upgrader.Upgrade(w, r, nil) wsConn, err = _upgrader.Upgrade(w, r, nil)
if err != nil { if err != nil {
return return
} }
@@ -163,7 +163,7 @@ func traffic(w http.ResponseWriter, r *http.Request) {
var wsConn *websocket.Conn var wsConn *websocket.Conn
if websocket.IsWebSocketUpgrade(r) { if websocket.IsWebSocketUpgrade(r) {
var err error var err error
wsConn, err = upgrader.Upgrade(w, r, nil) wsConn, err = _upgrader.Upgrade(w, r, nil)
if err != nil { if err != nil {
return return
} }

View File

@@ -15,9 +15,9 @@ const (
) )
var ( var (
tcpQueue = make(chan core.TCPConn) /* unbuffered */ _tcpQueue = make(chan core.TCPConn) /* unbuffered */
udpQueue = make(chan core.UDPPacket, maxUDPQueueSize) _udpQueue = make(chan core.UDPPacket, maxUDPQueueSize)
numUDPWorkers = max(runtime.NumCPU(), 4 /* at least 4 workers */) _numUDPWorkers = max(runtime.NumCPU(), 4 /* at least 4 workers */)
) )
func init() { func init() {
@@ -26,13 +26,13 @@ func init() {
// Add adds tcpConn to tcpQueue. // Add adds tcpConn to tcpQueue.
func Add(conn core.TCPConn) { func Add(conn core.TCPConn) {
tcpQueue <- conn _tcpQueue <- conn
} }
// AddPacket adds udpPacket to udpQueue. // AddPacket adds udpPacket to udpQueue.
func AddPacket(packet core.UDPPacket) { func AddPacket(packet core.UDPPacket) {
select { select {
case udpQueue <- packet: case _udpQueue <- packet:
default: default:
log.Warnf("queue is currently full, packet will be dropped") log.Warnf("queue is currently full, packet will be dropped")
packet.Drop() packet.Drop()
@@ -47,8 +47,8 @@ func max(a, b int) int {
} }
func process() { func process() {
for i := 0; i < numUDPWorkers; i++ { for i := 0; i < _numUDPWorkers; i++ {
queue := udpQueue queue := _udpQueue
go func() { go func() {
for packet := range queue { for packet := range queue {
handleUDP(packet) handleUDP(packet)
@@ -56,7 +56,7 @@ func process() {
}() }()
} }
for conn := range tcpQueue { for conn := range _tcpQueue {
go handleTCP(conn) go handleTCP(conn)
} }
} }

View File

@@ -21,9 +21,9 @@ const (
) )
var ( var (
// natTable uses source udp packet information // _natTable uses source udp packet information
// as key to store destination udp packetConn. // as key to store destination udp packetConn.
natTable = nat.NewTable() _natTable = nat.NewTable()
) )
func newUDPTracker(conn net.PacketConn, metadata *M.Metadata) net.PacketConn { func newUDPTracker(conn net.PacketConn, metadata *M.Metadata) net.PacketConn {
@@ -46,7 +46,7 @@ func handleUDP(packet core.UDPPacket) {
key := generateNATKey(metadata) key := generateNATKey(metadata)
handle := func(drop bool) bool { handle := func(drop bool) bool {
pc := natTable.Get(key) pc := _natTable.Get(key)
if pc != nil { if pc != nil {
handleUDPToRemote(packet, pc, metadata /* as net.Addr */, drop) handleUDPToRemote(packet, pc, metadata /* as net.Addr */, drop)
return true return true
@@ -59,7 +59,7 @@ func handleUDP(packet core.UDPPacket) {
} }
lockKey := key + "-lock" lockKey := key + "-lock"
cond, loaded := natTable.GetOrCreateLock(lockKey) cond, loaded := _natTable.GetOrCreateLock(lockKey)
go func() { go func() {
if loaded { if loaded {
cond.L.Lock() cond.L.Lock()
@@ -70,7 +70,7 @@ func handleUDP(packet core.UDPPacket) {
} }
defer func() { defer func() {
natTable.Delete(lockKey) _natTable.Delete(lockKey)
cond.Broadcast() cond.Broadcast()
}() }()
@@ -95,12 +95,12 @@ func handleUDP(packet core.UDPPacket) {
go func() { go func() {
defer pc.Close() defer pc.Close()
defer packet.Drop() defer packet.Drop()
defer natTable.Delete(key) defer _natTable.Delete(key)
handleUDPToLocal(packet, pc, udpSessionTimeout) handleUDPToLocal(packet, pc, udpSessionTimeout)
}() }()
natTable.Set(key, pc) _natTable.Set(key, pc)
handle(false /* drop */) handle(false /* drop */)
}() }()
} }