Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit4945e4e

Browse files
committed
Move initialization of the Port struct to the child process
In postmaster, use a more lightweight ClientSocket struct thatencapsulates just the socket itself and the remote endpoint's addressthat you get from accept() call. ClientSocket is passed to the childprocess, which initializes the bigger Port struct. This makes it moreclear what information postmaster initializes, and what is left to thechild process.Rename the StreamServerPort and StreamConnection functions to make itmore clear what they do. Remove StreamClose, replacing it with plainclosesocket() calls.Reviewed-by: Tristan Partin, Andres FreundDiscussion:https://www.postgresql.org/message-id/7a59b073-5b5b-151e-7ed3-8b01ff7ce9ef@iki.fi
1 parentd162c3a commit4945e4e

File tree

6 files changed

+142
-185
lines changed

6 files changed

+142
-185
lines changed

‎src/backend/libpq/pqcomm.c

Lines changed: 26 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,10 @@
2929
* INTERFACE ROUTINES
3030
*
3131
* setup/teardown:
32-
*StreamServerPort- Open postmaster's server port
33-
*StreamConnection- Create new connection with client
34-
*StreamClose- Close a client/backend connection
32+
*ListenServerPort- Open postmaster's server port
33+
*AcceptConnection- Accept new connection with client
3534
*TouchSocketFiles- Protect socket files against /tmp cleaners
36-
*pq_init- initialize libpq at backend startup
35+
*pq_init- initialize libpq at backend startup
3736
*socket_comm_reset- reset libpq during error recovery
3837
*socket_close- shutdown libpq at backend exit
3938
*
@@ -168,13 +167,18 @@ WaitEventSet *FeBeWaitSet;
168167
*pq_init - initialize libpq at backend startup
169168
* --------------------------------
170169
*/
171-
void
172-
pq_init(void)
170+
Port*
171+
pq_init(ClientSocket*client_sock)
173172
{
174-
Port*port=MyProcPort;
173+
Port*port;
175174
intsocket_posPG_USED_FOR_ASSERTS_ONLY;
176175
intlatch_posPG_USED_FOR_ASSERTS_ONLY;
177176

177+
/* allocate the Port struct and copy the ClientSocket contents to it */
178+
port=palloc0(sizeof(Port));
179+
port->sock=client_sock->sock;
180+
port->raddr=client_sock->raddr;
181+
178182
/* fill in the server (local) address */
179183
port->laddr.salen=sizeof(port->laddr.addr);
180184
if (getsockname(port->sock,
@@ -310,6 +314,8 @@ pq_init(void)
310314
*/
311315
Assert(socket_pos==FeBeWaitSetSocketPos);
312316
Assert(latch_pos==FeBeWaitSetLatchPos);
317+
318+
returnport;
313319
}
314320

315321
/* --------------------------------
@@ -384,16 +390,13 @@ socket_close(int code, Datum arg)
384390

385391

386392

387-
/*
388-
* Streams -- wrapper around Unix socket system calls
389-
*
390-
*
391-
*Stream functions are used for vanilla TCP connection protocol.
393+
/* --------------------------------
394+
* Postmaster functions to handle sockets.
395+
* --------------------------------
392396
*/
393397

394-
395398
/*
396-
*StreamServerPort -- open a "listening" port to accept connections.
399+
*ListenServerPort -- open a "listening" port to accept connections.
397400
*
398401
* family should be AF_UNIX or AF_UNSPEC; portNumber is the port number.
399402
* For AF_UNIX ports, hostName should be NULL and unixSocketDir must be
@@ -408,7 +411,7 @@ socket_close(int code, Datum arg)
408411
* RETURNS: STATUS_OK or STATUS_ERROR
409412
*/
410413
int
411-
StreamServerPort(intfamily,constchar*hostName,unsigned shortportNumber,
414+
ListenServerPort(intfamily,constchar*hostName,unsigned shortportNumber,
412415
constchar*unixSocketDir,
413416
pgsocketListenSockets[],int*NumListenSockets,intMaxListen)
414417
{
@@ -774,22 +777,23 @@ Setup_AF_UNIX(const char *sock_path)
774777

775778

776779
/*
777-
* StreamConnection -- create a new connection with client using
778-
*server port. Set port->sock to the FD of the new connection.
780+
* AcceptConnection -- accept a new connection with client using
781+
*server port. Fills *client_sock with the FD and endpoint info
782+
*of the new connection.
779783
*
780784
* ASSUME: that this doesn't need to be non-blocking because
781785
*the Postmaster waits for the socket to be ready to accept().
782786
*
783787
* RETURNS: STATUS_OK or STATUS_ERROR
784788
*/
785789
int
786-
StreamConnection(pgsocketserver_fd,Port*port)
790+
AcceptConnection(pgsocketserver_fd,ClientSocket*client_sock)
787791
{
788792
/* accept connection and fill in the client (remote) address */
789-
port->raddr.salen=sizeof(port->raddr.addr);
790-
if ((port->sock=accept(server_fd,
791-
(structsockaddr*)&port->raddr.addr,
792-
&port->raddr.salen))==PGINVALID_SOCKET)
793+
client_sock->raddr.salen=sizeof(client_sock->raddr.addr);
794+
if ((client_sock->sock=accept(server_fd,
795+
(structsockaddr*)&client_sock->raddr.addr,
796+
&client_sock->raddr.salen))==PGINVALID_SOCKET)
793797
{
794798
ereport(LOG,
795799
(errcode_for_socket_access(),
@@ -809,23 +813,6 @@ StreamConnection(pgsocket server_fd, Port *port)
809813
returnSTATUS_OK;
810814
}
811815

812-
/*
813-
* StreamClose -- close a client/backend connection
814-
*
815-
* NOTE: this is NOT used to terminate a session; it is just used to release
816-
* the file descriptor in a process that should no longer have the socket
817-
* open. (For example, the postmaster calls this after passing ownership
818-
* of the connection to a child process.) It is expected that someone else
819-
* still has the socket open. So, we only want to close the descriptor,
820-
* we do NOT want to send anything to the far end.
821-
*/
822-
void
823-
StreamClose(pgsocketsock)
824-
{
825-
if (closesocket(sock)!=0)
826-
elog(LOG,"could not close client or listen socket: %m");
827-
}
828-
829816
/*
830817
* TouchSocketFiles -- mark socket files as recently accessed
831818
*

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp