github.com/GeniusesGroup/libgo@v0.0.0-20220929090155-5ff932cb408e/chapar/connections.go (about) 1 /* For license and copyright information please see LEGAL file in repository */ 2 3 package chapar 4 5 import ( 6 "sync" 7 8 er "../error" 9 "../protocol" 10 ) 11 12 // connections store pools of connection to retrieve in many ways! 13 // TODO::: add analytic methods 14 type connections struct { 15 mutex sync.Mutex 16 poolByPath map[string]protocol.NetworkLinkConnection // It is optimize to convert byte slice to string as key in map: https://github.com/golang/go/commit/f5f5a8b6209f84961687d993b93ea0d397f5d5bf 17 poolByThingID map[[32]byte]protocol.NetworkLinkConnection 18 poolByBlackList map[[32]byte]protocol.NetworkLinkConnection 19 } 20 21 func (c *connections) init() { 22 if c.poolByPath == nil { 23 c.poolByPath = make(map[string]protocol.NetworkLinkConnection, 16384) 24 } 25 if c.poolByThingID == nil { 26 c.poolByThingID = make(map[[32]byte]protocol.NetworkLinkConnection, 16384) 27 } 28 if c.poolByBlackList == nil { 29 c.poolByBlackList = make(map[[32]byte]protocol.NetworkLinkConnection) 30 } 31 } 32 33 func (c *connections) newConnection(port *port, frame []byte) (conn protocol.NetworkLinkConnection) { 34 conn = &Connection{ 35 port: port, 36 } 37 conn.init(frame) 38 39 // TODO::: get ThingID from peer or func args?? 40 41 c.registerConnection(conn) 42 return 43 } 44 45 func (c *connections) establishConnectionByPath(path []byte) (conn *Connection, err protocol.Error) { 46 return 47 } 48 49 func (c *connections) establishConnectionByThingID(thingID [32]byte) (conn *Connection, err protocol.Error) { 50 return 51 } 52 53 // GetConnectionByPath get a connection by its path from connections pool!! 54 func (c *connections) getConnectionByPath(path []byte) (conn protocol.NetworkLinkConnection, err protocol.Error) { 55 conn = c.poolByPath[string(path)] 56 if conn == nil { 57 conn = &Connection{ 58 path: path, 59 } 60 err = conn.GetLastByPath() 61 } 62 return 63 } 64 65 func (c *connections) getConnectionsByThingID(thingID [32]byte) (conn protocol.NetworkLinkConnection, err protocol.Error) { 66 conn = c.poolByThingID[thingID] 67 if conn == nil { 68 conn = &Connection{ 69 thingID: thingID, 70 } 71 err = conn.GetLastByThingID() 72 } 73 return 74 } 75 76 func (c *connections) registerConnection(conn protocol.NetworkLinkConnection) { 77 c.mutex.Lock() 78 c.poolByPath[conn.Path.GetAsString()] = conn 79 c.poolByThingID[conn.ThingID] = conn 80 c.mutex.Unlock() 81 } 82 83 func (c *connections) registerNewPathForConnection(conn protocol.NetworkLinkConnection, alternativePath []byte) { 84 conn.setAlternativePath(alternativePath) 85 86 c.mutex.Lock() 87 c.poolByPath[string(alternativePath)] = conn 88 c.mutex.Unlock() 89 } 90 91 func (c *connections) closeConnection(conn protocol.NetworkLinkConnection) { 92 c.mutex.Lock() 93 // TODO::: Is it worth to don't delete connection just reset it and send it to pool of unused connection due to GC! 94 delete(c.poolByPath, conn.Path) 95 delete(c.poolByThingID, conn.ThingID) 96 for path := range conn.conn.AlternativePath { 97 delete(c.poolByPath, path) 98 } 99 c.mutex.Unlock() 100 } 101 102 // Shutdown ready the connection pools to shutdown!! 103 func (c *connections) shutdown() { 104 protocol.App.LogInfo("Chapar - ShutDown - Connections saving proccess begin...") 105 protocol.App.LogInfo("Chapar - ShutDown - Number of active connections:", len(c.poolByThingID)) 106 for _, conn := range c.poolByThingID { 107 conn.saveConn() 108 } 109 protocol.App.LogInfo("Chapar - ShutDown - Connections saving proccess end now!") 110 }