Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
ruanhaishen
redis
Commits
1e715383
Commit
1e715383
authored
Sep 30, 2015
by
antirez
Browse files
Refactoring: unlinkClient() added to lower freeClient() complexity.
parent
fdb3be93
Changes
3
Hide whitespace changes
Inline
Side-by-side
src/networking.c
View file @
1e715383
...
@@ -685,12 +685,50 @@ void disconnectSlaves(void) {
...
@@ -685,12 +685,50 @@ void disconnectSlaves(void) {
}
}
}
}
void
freeClient
(
client
*
c
)
{
/* Remove the specified client from global lists where the client could
* be referenced, not including the Pub/Sub channels.
* This is used by freeClient() and replicationCacheMaster(). */
void
unlinkClient
(
client
*
c
)
{
listNode
*
ln
;
listNode
*
ln
;
/* If this is marked as current client unset it */
/* If this is marked as current client unset it
.
*/
if
(
server
.
current_client
==
c
)
server
.
current_client
=
NULL
;
if
(
server
.
current_client
==
c
)
server
.
current_client
=
NULL
;
/* Certain operations must be done only if the client has an active socket.
* If the client was already unlinked or if it's a "fake client" the
* fd is already set to -1. */
if
(
c
->
fd
!=
-
1
)
{
/* Remove from the list of active clients. */
ln
=
listSearchKey
(
server
.
clients
,
c
);
serverAssert
(
ln
!=
NULL
);
listDelNode
(
server
.
clients
,
ln
);
/* Unregister async I/O handlers and close the socket. */
aeDeleteFileEvent
(
server
.
el
,
c
->
fd
,
AE_READABLE
);
aeDeleteFileEvent
(
server
.
el
,
c
->
fd
,
AE_WRITABLE
);
close
(
c
->
fd
);
c
->
fd
=
-
1
;
}
/* Remove from the list of pending writes if needed. */
if
(
c
->
flags
&
CLIENT_PENDING_WRITE
)
{
ln
=
listSearchKey
(
server
.
clients_pending_write
,
c
);
serverAssert
(
ln
!=
NULL
);
listDelNode
(
server
.
clients_pending_write
,
ln
);
}
/* When client was just unblocked because of a blocking operation,
* remove it from the list of unblocked clients. */
if
(
c
->
flags
&
CLIENT_UNBLOCKED
)
{
ln
=
listSearchKey
(
server
.
unblocked_clients
,
c
);
serverAssert
(
ln
!=
NULL
);
listDelNode
(
server
.
unblocked_clients
,
ln
);
}
}
void
freeClient
(
client
*
c
)
{
listNode
*
ln
;
/* If it is our master that's beging disconnected we should make sure
/* If it is our master that's beging disconnected we should make sure
* to cache the state to try a partial resynchronization later.
* to cache the state to try a partial resynchronization later.
*
*
...
@@ -732,36 +770,14 @@ void freeClient(client *c) {
...
@@ -732,36 +770,14 @@ void freeClient(client *c) {
dictRelease
(
c
->
pubsub_channels
);
dictRelease
(
c
->
pubsub_channels
);
listRelease
(
c
->
pubsub_patterns
);
listRelease
(
c
->
pubsub_patterns
);
/* Close socket, unregister events, and remove list of replies and
/* Free data structures. */
* accumulated arguments. */
if
(
c
->
fd
!=
-
1
)
{
aeDeleteFileEvent
(
server
.
el
,
c
->
fd
,
AE_READABLE
);
aeDeleteFileEvent
(
server
.
el
,
c
->
fd
,
AE_WRITABLE
);
close
(
c
->
fd
);
}
listRelease
(
c
->
reply
);
listRelease
(
c
->
reply
);
freeClientArgv
(
c
);
freeClientArgv
(
c
);
/* Remove from the list of clients */
/* Unlink the client: this will close the socket, remove the I/O
if
(
c
->
fd
!=
-
1
)
{
* handlers, and remove references of the client from different
ln
=
listSearchKey
(
server
.
clients
,
c
);
* places where active clients may be referenced. */
serverAssert
(
ln
!=
NULL
);
unlinkClient
(
c
);
listDelNode
(
server
.
clients
,
ln
);
}
/* Remove from the list of pending writes if needed. */
if
(
c
->
flags
&
CLIENT_PENDING_WRITE
)
{
ln
=
listSearchKey
(
server
.
clients_pending_write
,
c
);
listDelNode
(
server
.
clients_pending_write
,
ln
);
}
/* When client was just unblocked because of a blocking operation,
* remove it from the list of unblocked clients. */
if
(
c
->
flags
&
CLIENT_UNBLOCKED
)
{
ln
=
listSearchKey
(
server
.
unblocked_clients
,
c
);
serverAssert
(
ln
!=
NULL
);
listDelNode
(
server
.
unblocked_clients
,
ln
);
}
/* Master/slave cleanup Case 1:
/* Master/slave cleanup Case 1:
* we lost the connection with a slave. */
* we lost the connection with a slave. */
...
...
src/replication.c
View file @
1e715383
...
@@ -1846,36 +1846,16 @@ void replicationSendAck(void) {
...
@@ -1846,36 +1846,16 @@ void replicationSendAck(void) {
* handshake in order to reactivate the cached master.
* handshake in order to reactivate the cached master.
*/
*/
void
replicationCacheMaster
(
client
*
c
)
{
void
replicationCacheMaster
(
client
*
c
)
{
listNode
*
ln
;
serverAssert
(
server
.
master
!=
NULL
&&
server
.
cached_master
==
NULL
);
serverAssert
(
server
.
master
!=
NULL
&&
server
.
cached_master
==
NULL
);
serverLog
(
LL_NOTICE
,
"Caching the disconnected master state."
);
serverLog
(
LL_NOTICE
,
"Caching the disconnected master state."
);
/* Remove from the list of clients, we don't want this client to be
/* Unlink the client from the server structures. */
* listed by CLIENT LIST or processed in any way by batch operations. */
unlinkClient
(
c
);
ln
=
listSearchKey
(
server
.
clients
,
c
);
serverAssert
(
ln
!=
NULL
);
listDelNode
(
server
.
clients
,
ln
);
/* Remove from the list of clients with pending writes as well. */
if
(
c
->
flags
&
CLIENT_PENDING_WRITE
)
{
ln
=
listSearchKey
(
server
.
clients_pending_write
,
c
);
if
(
ln
)
listDelNode
(
server
.
clients_pending_write
,
ln
);
}
/* Save the master. Server.master will be set to null later by
/* Save the master. Server.master will be set to null later by
* replicationHandleMasterDisconnection(). */
* replicationHandleMasterDisconnection(). */
server
.
cached_master
=
server
.
master
;
server
.
cached_master
=
server
.
master
;
/* Remove the event handlers and close the socket. We'll later reuse
* the socket of the new connection with the master during PSYNC. */
aeDeleteFileEvent
(
server
.
el
,
c
->
fd
,
AE_READABLE
);
aeDeleteFileEvent
(
server
.
el
,
c
->
fd
,
AE_WRITABLE
);
close
(
c
->
fd
);
/* Set fd to -1 so that we can safely call freeClient(c) later. */
c
->
fd
=
-
1
;
/* Invalidate the Peer ID cache. */
/* Invalidate the Peer ID cache. */
if
(
c
->
peerid
)
{
if
(
c
->
peerid
)
{
sdsfree
(
c
->
peerid
);
sdsfree
(
c
->
peerid
);
...
...
src/server.h
View file @
1e715383
...
@@ -1112,6 +1112,7 @@ int clientsArePaused(void);
...
@@ -1112,6 +1112,7 @@ int clientsArePaused(void);
int
processEventsWhileBlocked
(
void
);
int
processEventsWhileBlocked
(
void
);
void
handleClientsWithPendingWrites
(
void
);
void
handleClientsWithPendingWrites
(
void
);
int
clientHasPendingReplies
(
client
*
c
);
int
clientHasPendingReplies
(
client
*
c
);
void
unlinkClient
(
client
*
c
);
#ifdef __GNUC__
#ifdef __GNUC__
void
addReplyErrorFormat
(
client
*
c
,
const
char
*
fmt
,
...)
void
addReplyErrorFormat
(
client
*
c
,
const
char
*
fmt
,
...)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment