Users of the fabric-network api are repeating a bad design pattern which is as follows
create and connect gateway
submitTransaction/evaluateTransaction
disconnect gateway
This is probably due to the sample which show a single CLI invocation where this pattern is applicable and you don't invoke the command often. However in real world applications that are constantly making requests, this pattern is bad on performance, will use resources and can result in connectivity problems due to resource starvation. Documentation is required to guide users on the lifecycle of the fabric-network gateway.
Gateway objects have a 1-1 association with an identity, so once you connect a gateway it is bound to the identity provided and cannot be changed. Gateway/Network/Contract/Transaction objects are designed to multiplex which means you can make multiple requests concurrently via these objects to submit or evaluate transactions.
Therefore for all requests made by a specific identity should always be done through the same gateway/network/contract objects. Note that the gateway object will cache the network and contract objects for you so it is safe to call getNetwork/getContract multiple times.
Only disconnect that Gateway when you have no further use for it. An example of a long running application might do the following
Create a Gateway object for each individual identity that makes a request and cache it or use a cached one but only have a single gateway object for that identity
Have a stale policy such that if a gateway has not been used in a while then disconnect that gateway. If then that identity comes in again to make a request then a new gateway object can be created.
Users of the fabric-network api are repeating a bad design pattern which is as follows
Gateway objects have a 1-1 association with an identity, so once you connect a gateway it is bound to the identity provided and cannot be changed. Gateway/Network/Contract/Transaction objects are designed to multiplex which means you can make multiple requests concurrently via these objects to submit or evaluate transactions.
Therefore for all requests made by a specific identity should always be done through the same gateway/network/contract objects. Note that the gateway object will cache the network and contract objects for you so it is safe to call getNetwork/getContract multiple times.
Only disconnect that Gateway when you have no further use for it. An example of a long running application might do the following