Willy Tarreau | 67b5a16 | 2019-08-11 16:38:56 +0200 | [diff] [blame] | 1 | /* |
| 2 | * include/proto/sink.h |
| 3 | * This file provides declarations for event sinks management |
| 4 | * |
| 5 | * Copyright (C) 2000-2019 Willy Tarreau - w@1wt.eu |
| 6 | * |
| 7 | * This library is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU Lesser General Public |
| 9 | * License as published by the Free Software Foundation, version 2.1 |
| 10 | * exclusively. |
| 11 | * |
| 12 | * This library is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * Lesser General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Lesser General Public |
| 18 | * License along with this library; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | */ |
| 21 | |
| 22 | #ifndef _PROTO_SINK_H |
| 23 | #define _PROTO_SINK_H |
| 24 | |
| 25 | #include <common/mini-clist.h> |
| 26 | #include <types/sink.h> |
| 27 | |
| 28 | extern struct list sink_list; |
| 29 | |
| 30 | struct sink *sink_find(const char *name); |
Willy Tarreau | 973e662 | 2019-08-20 11:57:52 +0200 | [diff] [blame] | 31 | struct sink *sink_new_fd(const char *name, const char *desc, enum sink_fmt fmt, int fd); |
Willy Tarreau | 8f24023 | 2019-08-27 16:41:06 +0200 | [diff] [blame] | 32 | ssize_t __sink_write(struct sink *sink, const struct ist msg[], size_t nmsg); |
| 33 | int sink_announce_dropped(struct sink *sink); |
| 34 | |
| 35 | |
| 36 | /* tries to send <nmsg> message parts (up to 8, ignored above) from message |
| 37 | * array <msg> to sink <sink>. Formating according to the sink's preference is |
| 38 | * done here. Lost messages are accounted for in the sink's counter. If there |
| 39 | * were lost messages, an attempt is first made to indicate it. |
| 40 | */ |
| 41 | static inline void sink_write(struct sink *sink, const struct ist msg[], size_t nmsg) |
| 42 | { |
| 43 | ssize_t sent; |
| 44 | |
| 45 | if (unlikely(sink->ctx.dropped > 0)) { |
| 46 | /* We need to take an exclusive lock so that other producers |
| 47 | * don't do the same thing at the same time and above all we |
| 48 | * want to be sure others have finished sending their messages |
| 49 | * so that the dropped event arrives exactly at the right |
| 50 | * position. |
| 51 | */ |
| 52 | HA_RWLOCK_WRLOCK(LOGSRV_LOCK, &sink->ctx.lock); |
| 53 | sent = sink_announce_dropped(sink); |
| 54 | HA_RWLOCK_WRUNLOCK(LOGSRV_LOCK, &sink->ctx.lock); |
| 55 | |
| 56 | if (!sent) { |
| 57 | /* we failed, we don't try to send our log as if it |
| 58 | * would pass by chance, we'd get disordered events. |
| 59 | */ |
| 60 | goto fail; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | HA_RWLOCK_RDLOCK(LOGSRV_LOCK, &sink->ctx.lock); |
| 65 | sent = __sink_write(sink, msg, nmsg); |
| 66 | HA_RWLOCK_RDUNLOCK(LOGSRV_LOCK, &sink->ctx.lock); |
| 67 | |
| 68 | fail: |
| 69 | if (unlikely(sent <= 0)) |
| 70 | HA_ATOMIC_ADD(&sink->ctx.dropped, 1); |
| 71 | } |
Willy Tarreau | 67b5a16 | 2019-08-11 16:38:56 +0200 | [diff] [blame] | 72 | |
| 73 | #endif /* _PROTO_SINK_H */ |
| 74 | |
| 75 | /* |
| 76 | * Local variables: |
| 77 | * c-indent-level: 8 |
| 78 | * c-basic-offset: 8 |
| 79 | * End: |
| 80 | */ |