Comments (5)
haha I can't believe I just did a pull request of the wrong code.
from libevhtp.
Nathan, that... was epic. LOL!
from libevhtp.
I'm going to be pushing up something more relevant here tonight, specifically I'm starting (and plausibly finishing)... hopefully, an abstracted logger with an interleaved ring buffer for performance and minimal locking...
from libevhtp.
Nice! I've been thinking about doing one of those for a while!
Ages ago, we wrote something to do this on another project:
https://github.com/mandiant/RProxy/blob/feature/ratelimiting/src/logger.c
The idea was to use {xxx} for specific parts of the request. The container that had multiple types of inputs/outputs (even Apple's stupid ASL):
https://github.com/mandiant/RProxy/blob/feature/ratelimiting/src/lzlog.c
Both of these are pretty terrible (I mean, if()s instead switches??), but it's a start.
from libevhtp.
Now back to the task at hand. As I was going through things, there are mixed (builtin so -NDEBUG does not remove it) assertions and normal checks.
I'm a fan of "fail close", but I don't know. I overlooked them since there was a lot of things I would have to figure out to free again. Which is why I wrote this header-only transparent reference counter:
#pragma once
#include <pthread.h>
static void (* REF_free)(void *) = free;
static void * (* REF_realloc)(void *, size_t) = realloc;
static void * (* REF_malloc)(size_t) = malloc;
struct refcount_ {
pthread_mutex_t mux;
unsigned int count;
char data[];
};
#define ref_upcast(DAT) \
(struct refcount_ *)((char *)(DAT - offsetof(struct refcount_, data)))
#define ref_barrier(CODE) \
pthread_mutex_lock(&refc->mux); \
CODE \
pthread_mutex_unlock(&refc->mux)
static inline void
ref_init_functions(void *(*mallocf)(size_t),
void * (*callocf)(size_t, size_t),
void *(*reallocf)(void *, size_t),
void (* freef)(void *))
{
REF_malloc = mallocf;
REF_realloc = reallocf;
REF_free = freef;
}
static unsigned int
ref_inc(void * buf)
{
struct refcount_ * refc = ref_upcast(buf);
unsigned int refs;
ref_barrier({ refs = ++refc->count; });
return refs;
}
static unsigned int
ref_dec(void * buf)
{
struct refcount_ * refc = ref_upcast(buf);
unsigned int refs;
ref_barrier({ refc->count -= 1; refs = refc->count; });
return refs;
}
static inline void *
ref_malloc(size_t size)
{
struct refcount_ * refc;
pthread_mutexattr_t attr;
refc = REF_malloc(sizeof(*refc) + size);
refc->count = 1;
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
pthread_mutex_init(&refc->mux, &attr);
return refc->data;
}
static void
ref_free(void * buf)
{
struct refcount_ * refc = ref_upcast(buf);
ref_barrier({
if (--refc->count == 0)
{
pthread_mutex_unlock(&refc->mux);
return REF_free(refc);
}
});
}
If we use that, it will be easy to know when to free and not by checking the refcount.
from libevhtp.
Related Issues (20)
- Q: how to peek at incoming http requests in pipelined mode
- Q: Is it safe to operate the same request between different threads? HOT 1
- use evbuffer_add_reference send json data have a problem HOT 2
- how to build libevhtp on windows
- Log.c is not cross-platform file.
- Empty pkgconfig libdir
- Inconsistency between EVHTP_BUILD_SHARED and BUILD_SHARED_LIBS CMake options HOT 2
- How to use multithreading HOT 5
- multiple nc request in keepalive fails to increase keepalive count HOT 1
- Does libevhtp support https? If support, what is the tps of http and https with keepalive or non-keepalive?
- Migrating from ev_http: How to get the socket for evhtp_accept_socket()? HOT 1
- Add example server with "1 thread per client connection"
- evhtp.c##htp__create_reply_() missed "buf = request->rc_scratch" after "request->rc_scratch = evbuffer_new()"?
- log-format $status is request status, not response status, why ?
- crash while invoking evhtp_connection_new() HOT 1
- Major slowdown using ssl and add_buffer(req->buffer_out,...) HOT 1
- Invalid content length value results in empty response
- info: libevhtp relies on onigposix.h, which is disabled in onig>=6.9.5_rev1 HOT 1
- Seperate http request thread and worker thread
- Is libevhtp support Server-send Events?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from libevhtp.