mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-08-31 18:34:02 +03:00
bug fixes
This commit is contained in:
@@ -109,6 +109,7 @@ func newConnection(req *protocol.TelegramRequest, hub *connectionHub) (*connecti
|
||||
conn: conn,
|
||||
hub: hub,
|
||||
id: rand.Int(), // nolint: gosec
|
||||
done: make(chan struct{}),
|
||||
}
|
||||
go rv.run()
|
||||
|
||||
|
||||
@@ -43,11 +43,16 @@ func (c *connectionHub) run() {
|
||||
}
|
||||
|
||||
func (c *connectionHub) runGC() {
|
||||
logger := c.logger.Named("gc")
|
||||
|
||||
for key, conn := range c.sockets {
|
||||
switch {
|
||||
case conn.closed():
|
||||
logger.Debugw("Delete closed socket", "key", key)
|
||||
delete(c.sockets, key)
|
||||
|
||||
case conn.idle():
|
||||
logger.Debugw("Delete idle socket", "key", key)
|
||||
conn.shutdown()
|
||||
delete(c.sockets, key)
|
||||
return
|
||||
@@ -56,9 +61,14 @@ func (c *connectionHub) runGC() {
|
||||
}
|
||||
|
||||
func (c *connectionHub) runConnectionRequest(req *connectionHubRequest) {
|
||||
logger := c.logger.Named("request").With("connection-id", req.request.ConnID)
|
||||
|
||||
for key, conn := range c.sockets {
|
||||
delete(c.sockets, key)
|
||||
if !conn.closed() {
|
||||
logger.Debugw("Choose connection",
|
||||
"id", conn.id,
|
||||
"remote_addr", conn.conn.RemoteAddr())
|
||||
req.response <- conn
|
||||
close(req.response)
|
||||
return
|
||||
@@ -66,16 +76,23 @@ func (c *connectionHub) runConnectionRequest(req *connectionHubRequest) {
|
||||
}
|
||||
|
||||
if conn, err := newConnection(req.request, c); err == nil {
|
||||
logger.Debugw("New connection",
|
||||
"id", conn.id,
|
||||
"remote_addr", conn.conn.RemoteAddr())
|
||||
req.response <- conn
|
||||
}
|
||||
close(req.response)
|
||||
}
|
||||
|
||||
func (c *connectionHub) runBrokenSocket(id int) {
|
||||
c.logger.Named("broken-socket").Debugw("Delete broken socket", "id", id)
|
||||
delete(c.sockets, id)
|
||||
}
|
||||
|
||||
func (c *connectionHub) runReturnConnection(conn *connection) {
|
||||
c.logger.Named("return-connection").Debugw("Return connection",
|
||||
"id", conn.id,
|
||||
"remote_addr", conn.conn.RemoteAddr())
|
||||
c.sockets[conn.id] = conn
|
||||
}
|
||||
|
||||
|
||||
@@ -32,8 +32,11 @@ func (h *hub) Write(packet conntypes.Packet, req *protocol.TelegramRequest) erro
|
||||
}
|
||||
|
||||
if err := conn.write(packet); err != nil {
|
||||
conn.shutdown()
|
||||
return fmt.Errorf("cannot send packet: %w", err)
|
||||
}
|
||||
sub.channelReturnConnections <- conn
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -42,10 +42,3 @@ func (r *registry) getChannel(id conntypes.ConnID) (*ctxChannel, bool) {
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
|
||||
func InitRegistry(ctx context.Context) {
|
||||
Registry = ®istry{
|
||||
ctx: ctx,
|
||||
conns: map[string]*ctxChannel{},
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user