diff options
| author | Jesper Jensen <jesper@jnsn.dev> | 2025-07-03 21:37:46 +0200 |
|---|---|---|
| committer | Jesper Jensen <jesper@jnsn.dev> | 2025-07-03 22:18:59 +0200 |
| commit | a46e9a5cbb8fc233a0ccb47708e7686c2b561ac0 (patch) | |
| tree | 7623f70cb31ca38ab63730dda8ed9b4ad039184b /src | |
| parent | 75e3fd649bf4baae66a7415634fe999c726ba149 (diff) | |
Only save config when there are worthy changes
Diffstat (limited to 'src')
| -rw-r--r-- | src/main.c | 5 | ||||
| -rw-r--r-- | src/peers.c | 3 | ||||
| -rw-r--r-- | src/peers.h | 2 | ||||
| -rw-r--r-- | src/proto.c | 8 | ||||
| -rw-r--r-- | src/proto.h | 1 | ||||
| -rw-r--r-- | src/query.c | 3 | ||||
| -rw-r--r-- | src/query.h | 2 |
7 files changed, 17 insertions, 7 deletions
@@ -256,7 +256,10 @@ int main(int argc, char** argv) { } } - save_config(); + if(dht.dirtyconf) { + save_config(); + dht.dirtyconf = false; + } } proto_end(&dht); diff --git a/src/peers.c b/src/peers.c index 8b3e8a3..3236a9a 100644 --- a/src/peers.c +++ b/src/peers.c @@ -165,7 +165,7 @@ void get_peers(struct infohash* infohash, struct addr **peers, size_t *peers_len prom_counter_add(peers_fetched, entry->value_len, NULL); } -void expire_hashes(time_t now) { +void expire_hashes(time_t now, bool *dirty) { assert(peer_table_load < peer_table_size); for(size_t i = 0; i < peer_table_size; i++) { struct peer_entry *entry = &peer_table[i]; @@ -202,6 +202,7 @@ void expire_hashes(time_t now) { } } + *dirty = true; peer_table[current_hole].set = false; peer_table_load--; prom_counter_inc(hash_expired, NULL); diff --git a/src/peers.h b/src/peers.h index 2c7845b..15044a7 100644 --- a/src/peers.h +++ b/src/peers.h @@ -36,5 +36,5 @@ int allocate_hashtable(); int add_peer(struct infohash* infohash, struct addr* peer, time_t now); void get_peers(struct infohash* infohash, struct addr **peers, size_t *peers_len); -void expire_hashes(time_t now); +void expire_hashes(time_t now, bool *dirty); void peer_update_metrics(); diff --git a/src/proto.c b/src/proto.c index d17f331..bd08c23 100644 --- a/src/proto.c +++ b/src/proto.c @@ -479,6 +479,7 @@ PROCESS_TIMEOUT(getclient_timeout) { return 0; routing_remove(&cont->ping.remote_id); + dht->dirtyconf = true; return 0; } @@ -611,6 +612,7 @@ PROCESS_REPONSE(getclient_response) { entry->addr.ip = ipv4->sin_addr.s_addr; entry->addr.port = ipv4->sin_port; entry->expire = now + PROTO_UNCTM; + dht->dirtyconf = true; routing_update_metrics(); } else { dbg("We are no longer interested"); @@ -622,6 +624,7 @@ PROCESS_REPONSE(getclient_response) { // removed while we are waiting for a response? if(entry != NULL) { entry->expire = now + PROTO_UNCTM; + dht->dirtyconf = true; } } @@ -778,7 +781,7 @@ int handle_packet(struct dht* dht, time_t now, enum commandType type, char* tran cursor += rc; char respType = 'r'; - rc = handle_request(&dht->self, &dht->tokens, now, query, (const struct sockaddr*)remote, remote_len, packet, packet_len, &cursor, end-cursor-1); + rc = handle_request(&dht->self, &dht->tokens, &dht->dirtyconf, now, query, (const struct sockaddr*)remote, remote_len, packet, packet_len, &cursor, end-cursor-1); if(rc == QUERY_EUNK) { prom_counter_inc(queries, (const char *[]){query, "unknown"}); // @FRAGILE: @HACK: Static offsets to fiddle with already written @@ -929,6 +932,7 @@ int proto_run(struct dht* dht, char* buff, size_t recv_len, struct sockaddr_in* prom_counter_inc(keepalive_count, NULL); oldest->expire = 0; + dht->dirtyconf = true; routing_oldest(&oldest); } @@ -969,7 +973,7 @@ int proto_run(struct dht* dht, char* buff, size_t recv_len, struct sockaddr_in* // @HACK 0 means unitialized, only happens in tests if(peer_table_size != 0) { - expire_hashes(now); + expire_hashes(now, &dht->dirtyconf); } recalulate_waketime(dht); diff --git a/src/proto.h b/src/proto.h index 638c1ec..95c7d7e 100644 --- a/src/proto.h +++ b/src/proto.h @@ -78,6 +78,7 @@ struct dht { int sfd; bool pause; + bool dirtyconf; bool reqalloc[MAX_INFLIGHT]; struct { diff --git a/src/query.c b/src/query.c index 2276ac1..a8643f1 100644 --- a/src/query.c +++ b/src/query.c @@ -9,7 +9,7 @@ #include <assert.h> #include <arpa/inet.h> -int handle_request(struct nodeid* self, struct tokens *tokens, time_t now, const char* method, const struct sockaddr* src, socklen_t src_len, const char* packet, size_t packet_len, char** response, size_t response_len) { +int handle_request(struct nodeid* self, struct tokens* tokens, bool *dirtyconf, time_t now, const char* method, const struct sockaddr* src, socklen_t src_len, const char* packet, size_t packet_len, char** response, size_t response_len) { if(method == NULL) { return QUERY_EBADQ; } @@ -472,6 +472,7 @@ int handle_request(struct nodeid* self, struct tokens *tokens, time_t now, const src_addr.port = htons(port); } int rc = add_peer(&infohash, &src_addr, now); + *dirtyconf = true; if(rc == PEER_EFULL) { } else if(rc != 0) { fatal("Could not add peer (%d)", rc); diff --git a/src/query.h b/src/query.h index 193df30..c948846 100644 --- a/src/query.h +++ b/src/query.h @@ -14,4 +14,4 @@ struct tokens; void token_create(struct tokens* tokens, time_t now, struct addr* remote, char* token); int token_validate(struct tokens* tokens, time_t now, struct addr* remote, char* token); -int handle_request(struct nodeid* self, struct tokens* tokens, time_t now, const char* method, const struct sockaddr* src, socklen_t src_len, const char* packet, size_t packet_len, char** response, size_t response_len); +int handle_request(struct nodeid* self, struct tokens* tokens, bool *dirtyconf, time_t now, const char* method, const struct sockaddr* src, socklen_t src_len, const char* packet, size_t packet_len, char** response, size_t response_len); |
