github.com/ronaksoft/rony@v0.16.26-0.20230807065236-1743dbfe6959/internal/gateway/tcp/pool.go (about)

     1  package tcpGateway
     2  
     3  import (
     4  	"sync"
     5  
     6  	"github.com/gobwas/ws"
     7  	"github.com/panjf2000/ants/v2"
     8  	"github.com/valyala/fasthttp"
     9  )
    10  
    11  /*
    12     Creation Time: 2020 - Apr - 17
    13     Created by:  (ehsan)
    14     Maintainers:
    15        1.  Ehsan N. Moosa (E2)
    16     Auditor: Ehsan N. Moosa (E2)
    17     Copyright Ronak Software Group 2020
    18  */
    19  
    20  var goPoolB, goPoolNB *ants.Pool
    21  
    22  var httpConnPool sync.Pool
    23  
    24  func acquireHttpConn(gw *Gateway, req *fasthttp.RequestCtx) *httpConn {
    25  	c, ok := httpConnPool.Get().(*httpConn)
    26  	if !ok {
    27  		return &httpConn{
    28  			gateway: gw,
    29  			ctx:     req,
    30  			kv:      make(map[string]interface{}, 4),
    31  		}
    32  	}
    33  	c.gateway = gw
    34  	c.ctx = req
    35  
    36  	return c
    37  }
    38  
    39  func releaseHttpConn(c *httpConn) {
    40  	c.clientIP = c.clientIP[:0]
    41  	c.clientType = c.clientType[:0]
    42  	for k := range c.kv {
    43  		delete(c.kv, k)
    44  	}
    45  	httpConnPool.Put(c)
    46  }
    47  
    48  var writeRequestPool sync.Pool
    49  
    50  func acquireWriteRequest(wc *websocketConn, opCode ws.OpCode) *writeRequest {
    51  	wr, ok := writeRequestPool.Get().(*writeRequest)
    52  	if !ok {
    53  		return &writeRequest{
    54  			wc:     wc,
    55  			opCode: opCode,
    56  		}
    57  	}
    58  	wr.wc = wc
    59  	wr.opCode = opCode
    60  
    61  	return wr
    62  }
    63  
    64  func releaseWriteRequest(wr *writeRequest) {
    65  	wr.wc = nil
    66  	wr.payload = wr.payload[:0]
    67  	writeRequestPool.Put(wr)
    68  }