1 | |
2 | /* |
3 | * Copyright (c) 2019, Redis Labs |
4 | * All rights reserved. |
5 | * |
6 | * Redistribution and use in source and binary forms, with or without |
7 | * modification, are permitted provided that the following conditions are met: |
8 | * |
9 | * * Redistributions of source code must retain the above copyright notice, |
10 | * this list of conditions and the following disclaimer. |
11 | * * Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
14 | * * Neither the name of Redis nor the names of its contributors may be used |
15 | * to endorse or promote products derived from this software without |
16 | * specific prior written permission. |
17 | * |
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
26 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
28 | * POSSIBILITY OF SUCH DAMAGE. |
29 | */ |
30 | |
31 | #ifndef __REDIS_CONNHELPERS_H |
32 | #define __REDIS_CONNHELPERS_H |
33 | |
34 | #include "connection.h" |
35 | |
36 | /* These are helper functions that are common to different connection |
37 | * implementations (currently sockets in connection.c and TLS in tls.c). |
38 | * |
39 | * Currently helpers implement the mechanisms for invoking connection |
40 | * handlers and tracking connection references, to allow safe destruction |
41 | * of connections from within a handler. |
42 | */ |
43 | |
44 | /* Increment connection references. |
45 | * |
46 | * Inside a connection handler, we guarantee refs >= 1 so it is always |
47 | * safe to connClose(). |
48 | * |
49 | * In other cases where we don't want to prematurely lose the connection, |
50 | * it can go beyond 1 as well; currently it is only done by connAccept(). |
51 | */ |
52 | static inline void connIncrRefs(connection *conn) { |
53 | conn->refs++; |
54 | } |
55 | |
56 | /* Decrement connection references. |
57 | * |
58 | * Note that this is not intended to provide any automatic free logic! |
59 | * callHandler() takes care of that for the common flows, and anywhere an |
60 | * explicit connIncrRefs() is used, the caller is expected to take care of |
61 | * that. |
62 | */ |
63 | |
64 | static inline void connDecrRefs(connection *conn) { |
65 | conn->refs--; |
66 | } |
67 | |
68 | static inline int connHasRefs(connection *conn) { |
69 | return conn->refs; |
70 | } |
71 | |
72 | /* Helper for connection implementations to call handlers: |
73 | * 1. Increment refs to protect the connection. |
74 | * 2. Execute the handler (if set). |
75 | * 3. Decrement refs and perform deferred close, if refs==0. |
76 | */ |
77 | static inline int callHandler(connection *conn, ConnectionCallbackFunc handler) { |
78 | connIncrRefs(conn); |
79 | if (handler) handler(conn); |
80 | connDecrRefs(conn); |
81 | if (conn->flags & CONN_FLAG_CLOSE_SCHEDULED) { |
82 | if (!connHasRefs(conn)) connClose(conn); |
83 | return 0; |
84 | } |
85 | return 1; |
86 | } |
87 | |
88 | #endif /* __REDIS_CONNHELPERS_H */ |
89 | |