Disclosure of Invention
Objects of the invention
In order to overcome the above disadvantages, an object of the present invention is to provide a method and a system for connecting packet management clients, so as to solve the technical problems that the existing connection pool has a limited capacity, cannot store a large number of connections, and needs to traverse a large number of unrelated clients in the connection pool when traversing a target client, and consumes more CPU performance, and the connection efficiency is also low.
(II) technical scheme
In order to achieve the above object, one aspect of the present application provides the following technical solutions:
a connection method for a packet management client comprises the following steps:
grouping all clients according to an organization structure;
respectively creating a corresponding connection pool based on each group;
traversing a target client needing to receive the message in a connection pool of a group to which the request client belongs after receiving a request instruction of the request client needing to forward the message;
if the target client is traversed in the connection pool of the group to which the request client belongs, the request client is in communication connection with the target client;
otherwise, traversing the target client in the connection pool of other groups and performing communication connection between the request client and the target client after traversing to the target client;
according to the method, all the clients in the same intranet are grouped according to an organization framework in advance, each group is provided with a connection pool, when a request client needs to forward a message to a target client, the target client can be traversed in the connection pool corresponding to the group where the request client is located preferentially, so that the probability of traversing to the target client is greatly reduced, the traversing efficiency is improved, the message forwarding speed is improved, more unrelated clients do not need to be traversed, the performance consumption of a CPU in the traversing process can be reduced, and the upper limit of the connecting number of the clients capable of processing is also improved by arranging the plurality of connection pools.
Further, the method also comprises the following steps: if the target client is not traversed in all the connection pools, adding the target client into the corresponding group according to the organization structure and traversing the target client in the corresponding connection pool again;
when the target client is not traversed in all the connection pools, the target client is a new user, the new users are required to be grouped according to the organization structure and distributed to the corresponding connection pools, and after the new users are grouped, the target client can be directly traversed in the corresponding connection pools in the next connection process.
Further, grouping all clients according to the organizational structure includes: if all the clients belong to the same organization architecture, grouping all the clients according to communication groups where different clients are located;
when all the clients belong to the same organization architecture, the clients can be grouped according to the communication group where the clients are located, and because the communication group is formed by frequently connecting and communicating the clients, the clients are grouped according to the communication group, so that the probability of traversing to a target client can be improved.
Further, grouping the clients according to the communication group in which the clients are located includes: if the same client is in a plurality of groups at the same time, grouping according to the communication group with the largest number of clients;
grouping is carried out according to the communication group with the largest number of the clients, the number of the clients which need to be connected and are contained in the same connection pool is larger, the situation that the clients need to be frequently switched back and forth in different connection pools to traverse in the traversing process is avoided, and the connecting efficiency is further improved.
Further, grouping the clients according to the communication group in which the clients are located includes: if the client which has not been added into any communication group exists, adding the client into the group to which the client which has communication connection with the client belongs;
the clients which are not added into the communication group are grouped according to the connection records of the clients, the target clients can be distributed into the connection pool containing the clients which are frequently connected with the target clients, and when the connection is performed next time, the probability of traversing the target clients is improved, and the connection efficiency is further improved.
Further, when the server pushes the message, the server traverses the target client in the connection pool where the target client that needs to receive the pushed message is located, and by traversing the target client in the corresponding connection pool according to the groups and pushing the message, the efficiency of pushing the message can be improved.
Another aspect of the present invention provides a packet management client connection system, including:
the grouping module can group all the clients according to an organization structure;
a creation module capable of creating a corresponding connection pool based on each group, respectively;
the traversing module can traverse a target client terminal needing to receive the message in a connection pool of a group to which the request client terminal belongs after receiving a request instruction of the request client terminal needing to forward the message;
the connection module is used for performing communication connection between the request client and the target client if the traversal module traverses the target client in the connection pool of the group to which the request client belongs; or, the traversing module traverses to the target client in the connection pool of other groups, and the request client is in communication connection with the target client.
Further, still include: and adding the module, and if the traversal module does not traverse the target client in all the connection pools, newly adding the target client into the corresponding group according to the organization structure.
Further, the grouping module includes:
the first grouping submodule is used for grouping all the clients according to the communication groups where the different clients are located if all the clients belong to the same organization architecture;
the second grouping submodule is used for grouping according to the communication group with the largest number of the clients if the same client is positioned in a plurality of groups at the same time;
and the third grouping submodule adds the client into the group to which the client which is in communication connection with the third grouping submodule belongs if the client which is not added into any communication group exists.
Further, still include: when the server pushes the message, the traversal module is further used for traversing the target client in the connection pool where the target client needing to receive the pushed message is located.
Detailed Description
In order to make the objects, technical solutions and advantages of the present invention more apparent, the present invention will be described in further detail with reference to the accompanying drawings in conjunction with the following detailed description. It should be understood that the description is intended to be exemplary only, and is not intended to limit the scope of the present invention. Moreover, in the following description, descriptions of well-known structures and techniques are omitted so as to not unnecessarily obscure the concepts of the present invention.
Referring to fig. 1, a method for connecting a packet management client according to the present invention includes the following steps:
grouping all clients according to an organization structure;
the server respectively creates a corresponding connection pool based on each group;
after logging in, a client establishes connection and sends a message to be forwarded and a request instruction to a server, and after receiving the forwarded message and the request instruction, the server traverses a target client needing to receive the message in a connection pool containing a group to which the request client belongs;
if the target client is traversed in the connection pool of the group to which the request client belongs, the request client is in communication connection with the target client;
otherwise, the target client is traversed in the connection pool of other groups, and the request client and the target client are in communication connection after the target client is traversed.
Specifically, the grouping of all the clients according to the organizational structure may be performed by using one of the organizations of departments, companies, and the like as an organizational structure, and the coarse granularity of the division is different for different use scenarios.
Specifically, before all clients are grouped according to the organizational structure, the organizational structure to which each client belongs needs to be acquired.
Specifically, the obtaining of the organization architecture to which each client belongs may be that a network administrator manually classifies the architecture to which each client belongs, or that different identifiers are marked on servers of different architectures, and then the server classifies the clients according to the different identifiers.
Specifically, the pre-grouping process is performed during server initialization, that is, clients currently added to the organization architecture are grouped at the initial time, and some newly added clients may be added at the later time.
Specifically, if the target client is not traversed in all the connection pools, it indicates that the target client is a newly added client, the new target client is newly added to the corresponding group according to the organization architecture, and the target client is traversed in the corresponding connection pool again.
The specific process of creating the connection pool is as follows:
after grouping, respectively creating a Map corresponding to each group to cache grouping information, wherein the Map caches a client uuid (identification code) as a key and caches a client ID as a value;
after each group creates a corresponding connection pool, a message queue channel and a coroutine are respectively created in a matched manner, and the coroutine is used for processing messages in the message queue channel one by one, namely traversing corresponding target clients in the connection pool according to requests of the requesting clients and connecting the target clients.
Specifically, a large number of connections are shared by the method and the device for processing the messages, the multiple coroutines respectively process the connection pools, the CPU simultaneously manages the multiple coroutines, the multi-core performance of the CPU is fully utilized, the message pushing efficiency is remarkably improved, and the upper limit of the number of the connections capable of being processed is also improved due to the fact that the multiple connection pools exist.
Referring to fig. 2, the following is a specific process of connecting the requesting client and the target client:
when a request client wants to send a message to a target client, the request client sends the message to be forwarded and uuid of the target client to a message manager, the message manager acquires grouping information in a Map of the request client, searches whether the target client is in the same group in the group to which the request client belongs according to the uuid of the target client sent by the request client, acquires a target client ID in the Map if the target client is found, stores the message to be forwarded by the request client and the target client ID in a message queue channel of a connection pool together, waits for coroutine to process, processes the messages in the message queue channel one by one, traverses the target client in the connection pool where the request client is located according to the target client ID, connects after traversing the target client, and then sends the message to the target client. Specifically, if the uuid of the target client is not found in the Map of the requesting client, it indicates that the target client and the requesting client are not in the same group, and the message manager will find maps of other groups again.
Specifically, if the Map of all the groups does not find uuid of the target client, it indicates that the target client is newly added, the newly added target client is newly added to the corresponding group according to the organization structure, the cache information in the Map is updated, and the target client is traversed in the corresponding connection pool again.
Specifically, if all the clients belong to the same organization architecture (i.e., the same department, the same company, etc.), all the clients are grouped according to the communication group where different clients are located. Specifically, for example, the client a, the client B, the client C, and the client D are all in the same communication group, and the client a, the client B, the client C, and the client D are uniformly divided into one group and jointly create a connection pool. Specifically, the communication group may be a WeChat communication group, a QQ communication group, or a nailed communication group, among others.
Specifically, if the same client is in a plurality of groups at the same time, for example, the client a is in the communication group (1) and is also in the communication group (2), wherein the number of people in the communication group (1) is 100 people, and the number of people in the communication group (2) is 300 people, the client a is classified into the group established according to the communication group (2), and the client is removed from the group established by the communication group (1).
Specifically, some clients are newly added and do not join any communication group, and if there are clients that have not joined the communication group, the clients are added to the group to which the clients that have been in communication connection belong. Further, when a client is newly added and has no connection with any client, the client needs to be manually added to the corresponding group.
Specifically, when the server pushes a message to a certain client, the server searches the corresponding uuid of the client in the maps of all the groups, and after the corresponding uuid is found in the Map of the certain group, the server obtains the client ID of the client according to the uuid, and traverses the target client in the connection pool of the group, so that a large number of unrelated clients do not need to be traversed in the traversing process, the base number of traversal can be reduced, and the consumption of the CPU performance in the traversing process is reduced.
The present application refers to the nouns:
websocket-a protocol for full duplex communication over a single TCP connection.
Goroutine, a lightweight thread of Golang, whose scheduling is managed by the Golang runtime.
channel-one channel for data transfer between the goroutines in Golang.
Golang-an open source programming language.
Another aspect of the present invention provides a packet management client connection system, including:
the grouping module can group all the clients according to an organization structure;
a creation module capable of creating a corresponding connection pool based on each group, respectively;
the traversing module can traverse a target client terminal which needs to receive the message in a connection pool of a group to which the requesting client terminal belongs after receiving a request instruction of the requesting client terminal which needs to forward the message;
the connection module is used for performing communication connection between the request client and the target client if the traversal module traverses the target client in the connection pool of the group to which the request client belongs; or, the traversing module traverses to the target client in the connection pool of other groups, and the request client is in communication connection with the target client.
Further, still include: and adding the module, and if the traversal module does not traverse the target client in all the connection pools, newly adding the target client into the corresponding group according to the organization structure.
Further, the grouping module includes:
the first grouping submodule is used for grouping all the clients according to the communication groups where the different clients are located if all the clients belong to the same organization architecture;
the second grouping submodule is used for grouping according to the communication group with the largest number of the clients if the same client is positioned in a plurality of groups at the same time;
and the third grouping submodule adds the client into the group to which the client which is in communication connection with the third grouping submodule belongs if the client which is not added into any communication group exists.
Further, still include: when the server pushes the message, the traversal module is further used for traversing the target client in the connection pool where the target client needing to receive the pushed message is located.
It is to be understood that the above-described embodiments of the present invention are merely illustrative of or explaining the principles of the invention and are not to be construed as limiting the invention. Therefore, any modification, equivalent replacement, improvement and the like made without departing from the spirit and scope of the present invention should be included in the protection scope of the present invention. Further, it is intended that the appended claims cover all such variations and modifications as fall within the scope and boundaries of the appended claims or the equivalents of such scope and boundaries.