mirror of
https://github.com/v2fly/v2ray-core.git
synced 2024-11-09 19:58:45 -05:00
fix: use sync.Map in request to packet conn server
This commit is contained in:
parent
ae4b7733ee
commit
3e7dc35562
@ -5,6 +5,7 @@ import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"io"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/golang-collections/go-datastructures/queue"
|
||||
@ -176,7 +177,7 @@ func (r *requestToPacketConnClientSession) Close() error {
|
||||
|
||||
func newRequestToPacketConnServer(ctx context.Context, config *ServerConfig) *requestToPacketConnServer {
|
||||
return &requestToPacketConnServer{
|
||||
sessionMap: make(map[string]*requestToPacketConnServerSession),
|
||||
sessionMap: sync.Map{},
|
||||
ctx: ctx,
|
||||
config: config,
|
||||
}
|
||||
@ -185,7 +186,7 @@ func newRequestToPacketConnServer(ctx context.Context, config *ServerConfig) *re
|
||||
type requestToPacketConnServer struct {
|
||||
packetSessionReceiver request.SessionReceiver
|
||||
|
||||
sessionMap map[string]*requestToPacketConnServerSession
|
||||
sessionMap sync.Map
|
||||
|
||||
ctx context.Context
|
||||
config *ServerConfig
|
||||
@ -203,7 +204,15 @@ func (r *requestToPacketConnServer) OnRoundTrip(ctx context.Context, req request
|
||||
return request.Response{}, newError("nil session id")
|
||||
}
|
||||
sessionID := string(SessionID)
|
||||
session, found := r.sessionMap[sessionID]
|
||||
var session *requestToPacketConnServerSession
|
||||
sessionAny, found := r.sessionMap.Load(sessionID)
|
||||
if found {
|
||||
var ok bool
|
||||
session, ok = sessionAny.(*requestToPacketConnServerSession)
|
||||
if !ok {
|
||||
return request.Response{}, newError("failed to cast session")
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
ctxWithFinish, finish := context.WithCancel(ctx)
|
||||
session = &requestToPacketConnServerSession{
|
||||
@ -218,9 +227,11 @@ func (r *requestToPacketConnServer) OnRoundTrip(ctx context.Context, req request
|
||||
maxWriteDuration: int(r.config.MaxWriteDurationMs),
|
||||
maxSimultaneousWriteConnection: int(r.config.MaxSimultaneousWriteConnection),
|
||||
}
|
||||
r.sessionMap[sessionID] = session
|
||||
_, loaded := r.sessionMap.LoadOrStore(sessionID, session)
|
||||
if !loaded {
|
||||
err = r.packetSessionReceiver.OnNewSession(ctx, session)
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
return request.Response{}, err
|
||||
}
|
||||
@ -228,7 +239,7 @@ func (r *requestToPacketConnServer) OnRoundTrip(ctx context.Context, req request
|
||||
}
|
||||
|
||||
func (r *requestToPacketConnServer) removeSessionID(sessionID []byte) {
|
||||
delete(r.sessionMap, string(sessionID))
|
||||
r.sessionMap.Delete(string(sessionID))
|
||||
}
|
||||
|
||||
type requestToPacketConnServerSession struct {
|
||||
|
Loading…
Reference in New Issue
Block a user