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

Commitcc3bda3

Browse files
committed
Tweak TCP-keepalive code so that an invalid setting doesn't cause us
to drop connections unceremoniously. Also some other marginal cleanups:don't query getsockopt() repeatedly if it fails, and avoid having theapparent definition of struct Port depend on which system headers youmight have included or not. Oliver Jowett and Tom Lane.
1 parentf7a5f90 commitcc3bda3

File tree

4 files changed

+98
-78
lines changed

4 files changed

+98
-78
lines changed

‎src/backend/libpq/pqcomm.c

Lines changed: 60 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
3131
* Portions Copyright (c) 1994, Regents of the University of California
3232
*
33-
*$PostgreSQL: pgsql/src/backend/libpq/pqcomm.c,v 1.178 2005/07/30 20:28:20 tgl Exp $
33+
*$PostgreSQL: pgsql/src/backend/libpq/pqcomm.c,v 1.179 2005/09/12 02:26:31 tgl Exp $
3434
*
3535
*-------------------------------------------------------------------------
3636
*/
@@ -595,18 +595,16 @@ StreamConnection(int server_fd, Port *port)
595595
returnSTATUS_ERROR;
596596
}
597597

598-
/* Set default keepalive parameters. This should also catch
599-
* misconfigurations (non-zero values when socket options aren't
600-
* supported)
598+
/*
599+
* Also apply the current keepalive parameters. If we fail to set
600+
* a parameter, don't error out, because these aren't universally
601+
* supported. (Note: you might think we need to reset the GUC
602+
* variables to 0 in such a case, but it's not necessary because
603+
* the show hooks for these variables report the truth anyway.)
601604
*/
602-
if (pq_setkeepalivesidle(tcp_keepalives_idle,port)!=STATUS_OK)
603-
returnSTATUS_ERROR;
604-
605-
if (pq_setkeepalivesinterval(tcp_keepalives_interval,port)!=STATUS_OK)
606-
returnSTATUS_ERROR;
607-
608-
if (pq_setkeepalivescount(tcp_keepalives_count,port)!=STATUS_OK)
609-
returnSTATUS_ERROR;
605+
(void)pq_setkeepalivesidle(tcp_keepalives_idle,port);
606+
(void)pq_setkeepalivesinterval(tcp_keepalives_interval,port);
607+
(void)pq_setkeepalivescount(tcp_keepalives_count,port);
610608
}
611609

612610
returnSTATUS_OK;
@@ -1172,11 +1170,16 @@ pq_endcopyout(bool errorAbort)
11721170
DoingCopyOut= false;
11731171
}
11741172

1173+
1174+
/*
1175+
* Support for TCP Keepalive parameters
1176+
*/
1177+
11751178
int
11761179
pq_getkeepalivesidle(Port*port)
11771180
{
11781181
#ifdefTCP_KEEPIDLE
1179-
if (IS_AF_UNIX(port->laddr.addr.ss_family))
1182+
if (port==NULL||IS_AF_UNIX(port->laddr.addr.ss_family))
11801183
return0;
11811184

11821185
if (port->keepalives_idle!=0)
@@ -1185,12 +1188,13 @@ pq_getkeepalivesidle(Port *port)
11851188
if (port->default_keepalives_idle==0)
11861189
{
11871190
socklen_tsize=sizeof(port->default_keepalives_idle);
1191+
11881192
if (getsockopt(port->sock,IPPROTO_TCP,TCP_KEEPIDLE,
1189-
(char*)&port->default_keepalives_idle,
1193+
(char*)&port->default_keepalives_idle,
11901194
&size)<0)
11911195
{
11921196
elog(LOG,"getsockopt(TCP_KEEPIDLE) failed: %m");
1193-
return-1;
1197+
port->default_keepalives_idle=-1;/* don't know */
11941198
}
11951199
}
11961200

@@ -1199,23 +1203,28 @@ pq_getkeepalivesidle(Port *port)
11991203
return0;
12001204
#endif
12011205
}
1202-
1206+
12031207
int
12041208
pq_setkeepalivesidle(intidle,Port*port)
12051209
{
1206-
if (IS_AF_UNIX(port->laddr.addr.ss_family))
1210+
if (port==NULL||IS_AF_UNIX(port->laddr.addr.ss_family))
12071211
returnSTATUS_OK;
12081212

12091213
#ifdefTCP_KEEPIDLE
12101214
if (idle==port->keepalives_idle)
12111215
returnSTATUS_OK;
12121216

1213-
if (port->default_keepalives_idle==0)
1217+
if (port->default_keepalives_idle<=0)
12141218
{
12151219
if (pq_getkeepalivesidle(port)<0)
1216-
returnSTATUS_ERROR;
1220+
{
1221+
if (idle==0)
1222+
returnSTATUS_OK;/* default is set but unknown */
1223+
else
1224+
returnSTATUS_ERROR;
1225+
}
12171226
}
1218-
1227+
12191228
if (idle==0)
12201229
idle=port->default_keepalives_idle;
12211230

@@ -1242,7 +1251,7 @@ int
12421251
pq_getkeepalivesinterval(Port*port)
12431252
{
12441253
#ifdefTCP_KEEPINTVL
1245-
if (IS_AF_UNIX(port->laddr.addr.ss_family))
1254+
if (port==NULL||IS_AF_UNIX(port->laddr.addr.ss_family))
12461255
return0;
12471256

12481257
if (port->keepalives_interval!=0)
@@ -1251,12 +1260,13 @@ pq_getkeepalivesinterval(Port *port)
12511260
if (port->default_keepalives_interval==0)
12521261
{
12531262
socklen_tsize=sizeof(port->default_keepalives_interval);
1263+
12541264
if (getsockopt(port->sock,IPPROTO_TCP,TCP_KEEPINTVL,
1255-
(char*)&port->default_keepalives_interval,
1265+
(char*)&port->default_keepalives_interval,
12561266
&size)<0)
12571267
{
12581268
elog(LOG,"getsockopt(TCP_KEEPINTVL) failed: %m");
1259-
return-1;
1269+
port->default_keepalives_interval=-1;/* don't know */
12601270
}
12611271
}
12621272

@@ -1265,22 +1275,28 @@ pq_getkeepalivesinterval(Port *port)
12651275
return0;
12661276
#endif
12671277
}
1268-
1278+
12691279
int
12701280
pq_setkeepalivesinterval(intinterval,Port*port)
12711281
{
1272-
if (IS_AF_UNIX(port->laddr.addr.ss_family))
1282+
if (port==NULL||IS_AF_UNIX(port->laddr.addr.ss_family))
12731283
returnSTATUS_OK;
12741284

12751285
#ifdefTCP_KEEPINTVL
12761286
if (interval==port->keepalives_interval)
12771287
returnSTATUS_OK;
12781288

1279-
if (port->default_keepalives_interval==0) {
1289+
if (port->default_keepalives_interval <=0)
1290+
{
12801291
if (pq_getkeepalivesinterval(port)<0)
1281-
returnSTATUS_ERROR;
1292+
{
1293+
if (interval==0)
1294+
returnSTATUS_OK;/* default is set but unknown */
1295+
else
1296+
returnSTATUS_ERROR;
1297+
}
12821298
}
1283-
1299+
12841300
if (interval==0)
12851301
interval=port->default_keepalives_interval;
12861302

@@ -1297,7 +1313,7 @@ pq_setkeepalivesinterval(int interval, Port *port)
12971313
{
12981314
elog(LOG,"setsockopt(TCP_KEEPINTVL) not supported");
12991315
returnSTATUS_ERROR;
1300-
}
1316+
}
13011317
#endif
13021318

13031319
returnSTATUS_OK;
@@ -1307,7 +1323,7 @@ int
13071323
pq_getkeepalivescount(Port*port)
13081324
{
13091325
#ifdefTCP_KEEPCNT
1310-
if (IS_AF_UNIX(port->laddr.addr.ss_family))
1326+
if (port==NULL||IS_AF_UNIX(port->laddr.addr.ss_family))
13111327
return0;
13121328

13131329
if (port->keepalives_count!=0)
@@ -1316,12 +1332,13 @@ pq_getkeepalivescount(Port *port)
13161332
if (port->default_keepalives_count==0)
13171333
{
13181334
socklen_tsize=sizeof(port->default_keepalives_count);
1335+
13191336
if (getsockopt(port->sock,IPPROTO_TCP,TCP_KEEPCNT,
1320-
(char*)&port->default_keepalives_count,
1337+
(char*)&port->default_keepalives_count,
13211338
&size)<0)
13221339
{
13231340
elog(LOG,"getsockopt(TCP_KEEPCNT) failed: %m");
1324-
return-1;
1341+
port->default_keepalives_count=-1;/* don't know */
13251342
}
13261343
}
13271344

@@ -1330,22 +1347,28 @@ pq_getkeepalivescount(Port *port)
13301347
return0;
13311348
#endif
13321349
}
1333-
1350+
13341351
int
13351352
pq_setkeepalivescount(intcount,Port*port)
13361353
{
1337-
if (IS_AF_UNIX(port->laddr.addr.ss_family))
1354+
if (port==NULL||IS_AF_UNIX(port->laddr.addr.ss_family))
13381355
returnSTATUS_OK;
13391356

13401357
#ifdefTCP_KEEPCNT
13411358
if (count==port->keepalives_count)
13421359
returnSTATUS_OK;
13431360

1344-
if (port->default_keepalives_count==0) {
1361+
if (port->default_keepalives_count <=0)
1362+
{
13451363
if (pq_getkeepalivescount(port)<0)
1346-
returnSTATUS_ERROR;
1364+
{
1365+
if (count==0)
1366+
returnSTATUS_OK;/* default is set but unknown */
1367+
else
1368+
returnSTATUS_ERROR;
1369+
}
13471370
}
1348-
1371+
13491372
if (count==0)
13501373
count=port->default_keepalives_count;
13511374

‎src/backend/utils/misc/guc.c

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* Written by Peter Eisentraut <peter_e@gmx.net>.
1111
*
1212
* IDENTIFICATION
13-
* $PostgreSQL: pgsql/src/backend/utils/misc/guc.c,v 1.287 2005/08/29 21:38:18 tgl Exp $
13+
* $PostgreSQL: pgsql/src/backend/utils/misc/guc.c,v 1.288 2005/09/12 02:26:32 tgl Exp $
1414
*
1515
*--------------------------------------------------------------------
1616
*/
@@ -5884,58 +5884,56 @@ assign_canonical_path(const char *newval, bool doit, GucSource source)
58845884
staticbool
58855885
assign_tcp_keepalives_idle(intnewval,booldoit,GucSourcesource)
58865886
{
5887-
if (doit&&MyProcPort!=NULL)
5888-
{
5887+
if (doit)
58895888
return (pq_setkeepalivesidle(newval,MyProcPort)==STATUS_OK);
5890-
}
58915889

58925890
return true;
58935891
}
58945892

58955893
staticconstchar*
58965894
show_tcp_keepalives_idle(void)
58975895
{
5898-
staticcharnbuf[32];
5899-
snprintf(nbuf,sizeof(nbuf),"%d",MyProcPort==NULL ?0 :pq_getkeepalivesidle(MyProcPort));
5896+
staticcharnbuf[16];
5897+
5898+
snprintf(nbuf,sizeof(nbuf),"%d",pq_getkeepalivesidle(MyProcPort));
59005899
returnnbuf;
59015900
}
59025901

59035902
staticbool
59045903
assign_tcp_keepalives_interval(intnewval,booldoit,GucSourcesource)
59055904
{
5906-
if (doit&&MyProcPort!=NULL)
5907-
{
5905+
if (doit)
59085906
return (pq_setkeepalivesinterval(newval,MyProcPort)==STATUS_OK);
5909-
}
59105907

59115908
return true;
59125909
}
59135910

59145911
staticconstchar*
59155912
show_tcp_keepalives_interval(void)
59165913
{
5917-
staticcharnbuf[32];
5918-
snprintf(nbuf,sizeof(nbuf),"%d",MyProcPort==NULL ?0 :pq_getkeepalivesinterval(MyProcPort));
5914+
staticcharnbuf[16];
5915+
5916+
snprintf(nbuf,sizeof(nbuf),"%d",pq_getkeepalivesinterval(MyProcPort));
59195917
returnnbuf;
59205918
}
59215919

59225920
staticbool
59235921
assign_tcp_keepalives_count(intnewval,booldoit,GucSourcesource)
59245922
{
5925-
if (doit&&MyProcPort!=NULL)
5926-
{
5923+
if (doit)
59275924
return (pq_setkeepalivescount(newval,MyProcPort)==STATUS_OK);
5928-
}
59295925

59305926
return true;
59315927
}
59325928

59335929
staticconstchar*
59345930
show_tcp_keepalives_count(void)
59355931
{
5936-
staticcharnbuf[32];
5937-
snprintf(nbuf,sizeof(nbuf),"%d",MyProcPort==NULL ?0 :pq_getkeepalivescount(MyProcPort));
5932+
staticcharnbuf[16];
5933+
5934+
snprintf(nbuf,sizeof(nbuf),"%d",pq_getkeepalivescount(MyProcPort));
59385935
returnnbuf;
59395936
}
59405937

5938+
59415939
#include"guc-file.c"

‎src/backend/utils/misc/postgresql.conf.sample

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,14 @@
7575

7676
# - TCP Keepalives -
7777
# see 'man 7 tcp' for details
78-
#tcp_keepalives_idle = 0# TCP_KEEPIDLE, in secs; 0 uses the
79-
# system default
80-
#tcp_keepalives_interval = 0# TCP_KEEPINTVL, in seconds; 0 uses the
81-
# system default
82-
#tcp_keepalives_count = 0# TCP_KEEPCNT, in seconds; 0 uses the
83-
# system default
78+
79+
#tcp_keepalives_idle = 0# TCP_KEEPIDLE, in seconds;
80+
# 0 selects the system default
81+
#tcp_keepalives_interval = 0# TCP_KEEPINTVL, in seconds;
82+
# 0 selects the system default
83+
#tcp_keepalives_count = 0# TCP_KEEPCNT;
84+
# 0 selects the system default
85+
8486

8587
#---------------------------------------------------------------------------
8688
# RESOURCE USAGE (except WAL)

‎src/include/libpq/libpq-be.h

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
1212
* Portions Copyright (c) 1994, Regents of the University of California
1313
*
14-
* $PostgreSQL: pgsql/src/include/libpq/libpq-be.h,v 1.50 2005/07/30 15:17:25 momjian Exp $
14+
* $PostgreSQL: pgsql/src/include/libpq/libpq-be.h,v 1.51 2005/09/12 02:26:33 tgl Exp $
1515
*
1616
*-------------------------------------------------------------------------
1717
*/
@@ -85,6 +85,21 @@ typedef struct Port
8585
constchar*commandTag;/* current command tag */
8686
structtimevalsession_start;/* for session duration logging */
8787

88+
/*
89+
* TCP keepalive settings.
90+
*
91+
*default values are 0 if AF_UNIX or not yet known;
92+
*current values are 0 if AF_UNIX or using the default.
93+
*Also, -1 in a default value means we were unable to find out the
94+
*default (getsockopt failed).
95+
*/
96+
intdefault_keepalives_idle;
97+
intdefault_keepalives_interval;
98+
intdefault_keepalives_count;
99+
intkeepalives_idle;
100+
intkeepalives_interval;
101+
intkeepalives_count;
102+
88103
/*
89104
* SSL structures
90105
*/
@@ -95,24 +110,6 @@ typedef struct Port
95110
charpeer_cn[SM_USER+1];
96111
unsigned longcount;
97112
#endif
98-
99-
/*
100-
* TCP keepalive settings;
101-
* default values are 0 if AF_UNIX or not yet known;
102-
* current values are 0 if AF_UNIX or using the default.
103-
*/
104-
#ifdefTCP_KEEPIDLE
105-
intdefault_keepalives_idle;
106-
intkeepalives_idle;
107-
#endif
108-
#ifdefTCP_KEEPINTVL
109-
intdefault_keepalives_interval;
110-
intkeepalives_interval;
111-
#endif
112-
#ifdefTCP_KEEPCNT
113-
intdefault_keepalives_count;
114-
intkeepalives_count;
115-
#endif
116113
}Port;
117114

118115

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp