diff --git a/contrib/development.conf b/contrib/development.conf index 596c58c..31e69de 100644 --- a/contrib/development.conf +++ b/contrib/development.conf @@ -15,3 +15,4 @@ log "stdout" { }; threads "4"; max-connections "32"; +max-cache "1k"; diff --git a/contrib/production.conf b/contrib/production.conf index 0589d52..6eadb61 100644 --- a/contrib/production.conf +++ b/contrib/production.conf @@ -1,5 +1,5 @@ # -# Telodendria production configuration file. +# Telodendria configuration file. # # The following man pages document the configuration: # @@ -26,5 +26,7 @@ log "file" { level "warning"; timestampFormat "default"; }; + threads "4"; max-connections "32"; +max-cache "512M"; diff --git a/man/man5/telodendria.conf.5 b/man/man5/telodendria.conf.5 index 7444ea7..5087034 100644 --- a/man/man5/telodendria.conf.5 +++ b/man/man5/telodendria.conf.5 @@ -1,4 +1,4 @@ -.Dd $Mdocdate: October 15 2022 $ +.Dd $Mdocdate: October 24 2022 $ .Dt TELODENDRIA.CONF 5 .Os Telodendria Project .Sh NAME @@ -172,6 +172,17 @@ less than the total CPU core count, to prevent overloading the system. The most efficient number of threads ultimately depends on the configuration of the machine running Telodendria, so you may just have to play around with different values here to see which gives the best performance. +.It Ic max-connections Ar count +The maximum number of simultanious connections to allow to the daemon. This option +prevents the daemon from allocating large amounts of memory in the even that it +undergoes a denial of service attack. It typically does not need to be adjusted. +.It Ic max-cache Ar bytes +The maximum size of the cache. Telodendria relies heavily on caching to speed +things up. The cache grows as data is loaded from the data directory. All cache +is stored in memory. This option limits the size of the memory cache. If you have +a system that has a lot of memory, you'll get better performance if this option +is set higher. Otherwise, this value should be lowered on systems that have +minimal memory available. .El .Sh FILES .Bl -tag -width Ds diff --git a/src/Telodendria.c b/src/Telodendria.c index 1b87648..8b3c38e 100644 --- a/src/Telodendria.c +++ b/src/Telodendria.c @@ -334,6 +334,8 @@ main(int argc, char **argv) Log(lc, LOG_DEBUG, "Run As: %s:%s", tConfig->uid, tConfig->gid); Log(lc, LOG_DEBUG, "Data Directory: %s", tConfig->dataDir); Log(lc, LOG_DEBUG, "Threads: %d", tConfig->threads); + Log(lc, LOG_DEBUG, "Max Connections: %d", tConfig->maxConnections); + Log(lc, LOG_DEBUG, "Max Cache: %ld", tConfig->maxCache); Log(lc, LOG_DEBUG, "Flags: %x", tConfig->flags); LogConfigUnindent(lc); diff --git a/src/TelodendriaConfig.c b/src/TelodendriaConfig.c index dff1a1c..a7fd9e5 100644 --- a/src/TelodendriaConfig.c +++ b/src/TelodendriaConfig.c @@ -230,7 +230,10 @@ TelodendriaConfigParse(HashMap * config, LogConfig * lc) } } - + GET_DIRECTIVE("max-cache"); + ASSERT_NO_CHILDREN("max-cache"); + ASSERT_VALUES("max-cache", 1); + tConfig->maxCache = UtilStringToBytes(ArrayGet(value, 0)); GET_DIRECTIVE("federation"); ASSERT_NO_CHILDREN("federation"); diff --git a/src/Util.c b/src/Util.c index 8470f65..b51ac85 100644 --- a/src/Util.c +++ b/src/Util.c @@ -27,14 +27,16 @@ #include #include +#include +#include #include #include -long +unsigned long UtilServerTs(void) { struct timeval tv; - long ts; + unsigned long ts; gettimeofday(&tv, NULL); ts = (tv.tv_sec * 1000) + (tv.tv_usec / 1000); @@ -122,3 +124,53 @@ UtilSleepMillis(long ms) return res; } + +size_t +UtilStringToBytes(char *str) +{ + size_t bytes = 0; + + while (*str) + { + if (isdigit(*str)) + { + bytes *= 10; + bytes += *str - '0'; + } + else + { + switch (*str) + { + case 'K': + bytes *= 1024; + break; + case 'M': + bytes *= pow(1024, 2); + break; + case 'G': + bytes *= pow(1024, 3); + break; + case 'k': + bytes *= 1000; + break; + case 'm': + bytes *= pow(1000, 2); + break; + case 'g': + bytes *= pow(1000, 3); + break; + default: + return 0; + } + + if (*(str + 1)) + { + return 0; + } + } + + str++; + } + + return bytes; +} diff --git a/src/include/TelodendriaConfig.h b/src/include/TelodendriaConfig.h index df364c6..80bb063 100644 --- a/src/include/TelodendriaConfig.h +++ b/src/include/TelodendriaConfig.h @@ -68,6 +68,8 @@ typedef struct TelodendriaConfig unsigned int threads; unsigned int maxConnections; + size_t maxCache; + char *logTimestamp; int logLevel; } TelodendriaConfig; diff --git a/src/include/Util.h b/src/include/Util.h index 8023e6b..a789e90 100644 --- a/src/include/Util.h +++ b/src/include/Util.h @@ -31,6 +31,8 @@ #ifndef TELODENDRIA_UTIL_H #define TELODENDRIA_UTIL_H +#include + /* * Get the current type in milliseconds since the Unix epoch. This uses * POSIX gettimeofday(2) and time_t, and converts it to a single number, @@ -50,7 +52,7 @@ * Return: A long representing the current time in milliseconds since * the beginning of the Unix epoch, just as the Matrix spec requires. */ -extern long +extern unsigned long UtilServerTs(void); /* @@ -96,4 +98,7 @@ extern char * extern int UtilSleepMillis(long); +extern size_t + UtilStringToBytes(char *); + #endif /* TELODENDRIA_UTIL_H */