Skip to content

Commit 34b1c12

Browse files
Stylistics, async default implication addressed, nph style run
1 parent e1da957 commit 34b1c12

20 files changed

Lines changed: 427 additions & 321 deletions

library/events/json_message_ready_event.nim

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@ type JsonMessageReadyEvent* = ref object of JsonEvent
55
messageId*: SdsMessageID
66
channelId*: SdsChannelID
77

8-
proc new*(T: type JsonMessageReadyEvent, messageId: SdsMessageID, channelId: SdsChannelID): T =
9-
return JsonMessageReadyEvent(eventType: "message_ready", messageId: messageId, channelId: channelId)
8+
proc new*(
9+
T: type JsonMessageReadyEvent, messageId: SdsMessageID, channelId: SdsChannelID
10+
): T =
11+
return JsonMessageReadyEvent(
12+
eventType: "message_ready", messageId: messageId, channelId: channelId
13+
)
1014

1115
method `$`*(jsonMessageReady: JsonMessageReadyEvent): string =
1216
$(%*jsonMessageReady)

library/events/json_message_sent_event.nim

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@ type JsonMessageSentEvent* = ref object of JsonEvent
55
messageId*: SdsMessageID
66
channelId*: SdsChannelID
77

8-
proc new*(T: type JsonMessageSentEvent, messageId: SdsMessageID, channelId: SdsChannelID): T =
9-
return JsonMessageSentEvent(eventType: "message_sent", messageId: messageId, channelId: channelId)
8+
proc new*(
9+
T: type JsonMessageSentEvent, messageId: SdsMessageID, channelId: SdsChannelID
10+
): T =
11+
return JsonMessageSentEvent(
12+
eventType: "message_sent", messageId: messageId, channelId: channelId
13+
)
1014

1115
method `$`*(jsonMessageSent: JsonMessageSentEvent): string =
1216
$(%*jsonMessageSent)

library/events/json_missing_dependencies_event.nim

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ proc new*(
1313
channelId: SdsChannelID,
1414
): T =
1515
return JsonMissingDependenciesEvent(
16-
eventType: "missing_dependencies", messageId: messageId, missingDeps: missingDeps, channelId: channelId
16+
eventType: "missing_dependencies",
17+
messageId: messageId,
18+
missingDeps: missingDeps,
19+
channelId: channelId,
1720
)
1821

1922
method `$`*(jsonMissingDependencies: JsonMissingDependenciesEvent): string =

library/libsds.nim

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ var
6363

6464
proc acquireCtx(callback: SdsCallBack, userData: pointer): ptr SdsContext =
6565
ctxPoolLock.acquire()
66-
defer: ctxPoolLock.release()
66+
defer:
67+
ctxPoolLock.release()
6768
if ctxPool.len > 0:
6869
result = ctxPool.pop()
6970
else:
@@ -74,7 +75,8 @@ proc acquireCtx(callback: SdsCallBack, userData: pointer): ptr SdsContext =
7475

7576
proc releaseCtx(ctx: ptr SdsContext) =
7677
ctxPoolLock.acquire()
77-
defer: ctxPoolLock.release()
78+
defer:
79+
ctxPoolLock.release()
7880
ctx.userData = nil
7981
ctx.eventCallback = nil
8082
ctx.eventUserData = nil
@@ -105,7 +107,9 @@ proc onMessageSent(ctx: ptr SdsContext): MessageSentCallback =
105107
$JsonMessageSentEvent.new(messageId, channelId)
106108

107109
proc onMissingDependencies(ctx: ptr SdsContext): MissingDependenciesCallback =
108-
return proc(messageId: SdsMessageID, missingDeps: seq[HistoryEntry], channelId: SdsChannelID) {.gcsafe.} =
110+
return proc(
111+
messageId: SdsMessageID, missingDeps: seq[HistoryEntry], channelId: SdsChannelID
112+
) {.gcsafe.} =
109113
callEventCallback(ctx, "onMissingDependencies"):
110114
$JsonMissingDependenciesEvent.new(messageId, missingDeps, channelId)
111115

@@ -135,7 +139,7 @@ proc onRetrievalHint(ctx: ptr SdsContext): RetrievalHintProvider =
135139
copyMem(addr hintBytes[0], hint, hintLen)
136140
deallocShared(hint)
137141
return hintBytes
138-
142+
139143
return @[]
140144

141145
### End of not-exported components

library/sds_thread/inter_thread_communication/requests/sds_message_request.nim

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,9 @@ proc process*(
6969
).valueOr:
7070
return err("error processing UNWRAP_MESSAGE request: " & $error)
7171

72-
let res = SdsUnwrapResponse(message: unwrappedMessage, missingDeps: missingDeps, channelId: extractedChannelId)
72+
let res = SdsUnwrapResponse(
73+
message: unwrappedMessage, missingDeps: missingDeps, channelId: extractedChannelId
74+
)
7375

7476
# return the result as a json string
7577
var node = newJObject()

library/sds_thread/sds_thread.nim

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@
44

55
import std/[options, atomics, os, net, locks]
66
import chronicles, chronos, chronos/threadsync, taskpools/channels_spsc_single, results
7-
import
8-
../ffi_types,
9-
./inter_thread_communication/sds_thread_request,
10-
sds/sds_utils
7+
import ../ffi_types, ./inter_thread_communication/sds_thread_request, sds/sds_utils
118

129
type SdsContext* = object
1310
thread: Thread[(ptr SdsContext)]

sds.nim

Lines changed: 36 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ proc isAcknowledged*(
3535

3636
proc reviewAckStatus(
3737
rm: ReliabilityManager, msg: SdsMessage
38-
): Future[void] {.async: (raises: [CatchableError]), gcsafe.} =
38+
) {.async: (raises: [CatchableError]).} =
3939
try:
4040
var rbf: Option[RollingBloomFilter]
4141
if msg.bloomFilter.len > 0:
@@ -46,8 +46,10 @@ proc reviewAckStatus(
4646
RollingBloomFilter.init(
4747
filter = bf,
4848
capacity = bf.capacity,
49-
minCapacity = (bf.capacity.float * (100 - CapacityFlexPercent).float / 100.0).int,
50-
maxCapacity = (bf.capacity.float * (100 + CapacityFlexPercent).float / 100.0).int,
49+
minCapacity =
50+
(bf.capacity.float * (100 - CapacityFlexPercent).float / 100.0).int,
51+
maxCapacity =
52+
(bf.capacity.float * (100 + CapacityFlexPercent).float / 100.0).int,
5153
)
5254
)
5355
else:
@@ -167,7 +169,7 @@ proc wrapOutgoingMessage*(
167169

168170
proc processIncomingBuffer(
169171
rm: ReliabilityManager, channelId: SdsChannelID
170-
): Future[void] {.async: (raises: [CatchableError]), gcsafe.} =
172+
) {.async: (raises: [CatchableError]).} =
171173
try:
172174
await rm.lock.acquire()
173175
try:
@@ -220,10 +222,12 @@ proc processIncomingBuffer(
220222

221223
proc unwrapReceivedMessage*(
222224
rm: ReliabilityManager, message: seq[byte]
223-
): Future[Result[
224-
tuple[message: seq[byte], missingDeps: seq[HistoryEntry], channelId: SdsChannelID],
225-
ReliabilityError,
226-
]] {.async: (raises: [CancelledError]), gcsafe.} =
225+
): Future[
226+
Result[
227+
tuple[message: seq[byte], missingDeps: seq[HistoryEntry], channelId: SdsChannelID],
228+
ReliabilityError,
229+
]
230+
] {.async: (raises: [CancelledError]).} =
227231
## Unwraps a received message and processes its reliability metadata.
228232
try:
229233
let channelId = extractChannelId(message).valueOr:
@@ -258,17 +262,18 @@ proc unwrapReceivedMessage*(
258262
# Remove from our own outgoing repair buffer (someone else is also requesting)
259263
channel.outgoingRepairBuffer.del(repairEntry.messageId)
260264
await rm.persistence.removeOutgoingRepair(channelId, repairEntry.messageId)
261-
if repairEntry.messageId in channel.messageHistory and
262-
rm.participantId.len > 0 and repairEntry.senderId.len > 0:
265+
if repairEntry.messageId in channel.messageHistory and rm.participantId.len > 0 and
266+
repairEntry.senderId.len > 0:
263267
if isInResponseGroup(
264-
rm.participantId, repairEntry.senderId,
265-
repairEntry.messageId, rm.config.numResponseGroups
268+
rm.participantId, repairEntry.senderId, repairEntry.messageId,
269+
rm.config.numResponseGroups,
266270
):
267-
let serialized = serializeMessage(channel.messageHistory[repairEntry.messageId])
271+
let serialized =
272+
serializeMessage(channel.messageHistory[repairEntry.messageId])
268273
if serialized.isOk():
269274
let tResp = computeTResp(
270-
rm.participantId, repairEntry.senderId,
271-
repairEntry.messageId, rm.config.repairTMax
275+
rm.participantId, repairEntry.senderId, repairEntry.messageId,
276+
rm.config.repairTMax,
272277
)
273278
let inEntry = IncomingRepairEntry(
274279
inHistEntry: repairEntry,
@@ -302,15 +307,16 @@ proc unwrapReceivedMessage*(
302307
channel.incomingBuffer[pendingId].missingDeps.excl(msg.messageId)
303308
unblocked.add(pendingId)
304309
for pendingId in unblocked:
305-
await rm.persistence.saveIncoming(channelId, channel.incomingBuffer[pendingId])
310+
await rm.persistence.saveIncoming(
311+
channelId, channel.incomingBuffer[pendingId]
312+
)
306313
await rm.processIncomingBuffer(channelId)
307314
if not rm.onMessageReady.isNil():
308315
{.cast(raises: []).}:
309316
rm.onMessageReady(msg.messageId, channelId)
310317
else:
311318
let entry = IncomingMessage.init(
312-
message = msg,
313-
missingDeps = missingDeps.getMessageIds().toHashSet(),
319+
message = msg, missingDeps = missingDeps.getMessageIds().toHashSet()
314320
)
315321
channel.incomingBuffer[msg.messageId] = entry
316322
await rm.persistence.saveIncoming(channelId, entry)
@@ -323,13 +329,11 @@ proc unwrapReceivedMessage*(
323329
for dep in missingDeps:
324330
if dep.messageId notin channel.outgoingRepairBuffer:
325331
let tReq = computeTReq(
326-
rm.participantId, dep.messageId,
327-
rm.config.repairTMin, rm.config.repairTMax
328-
)
329-
let outEntry = OutgoingRepairEntry(
330-
outHistEntry: dep,
331-
minTimeRepairReq: now + tReq,
332+
rm.participantId, dep.messageId, rm.config.repairTMin,
333+
rm.config.repairTMax,
332334
)
335+
let outEntry =
336+
OutgoingRepairEntry(outHistEntry: dep, minTimeRepairReq: now + tReq)
333337
channel.outgoingRepairBuffer[dep.messageId] = outEntry
334338
await rm.persistence.saveOutgoingRepair(channelId, dep.messageId, outEntry)
335339

@@ -342,7 +346,7 @@ proc unwrapReceivedMessage*(
342346

343347
proc markDependenciesMet*(
344348
rm: ReliabilityManager, messageIds: seq[SdsMessageID], channelId: SdsChannelID
345-
): Future[Result[void, ReliabilityError]] {.async: (raises: [CancelledError]), gcsafe.} =
349+
): Future[Result[void, ReliabilityError]] {.async: (raises: [CancelledError]).} =
346350
## Marks the specified message dependencies as met.
347351
try:
348352
if channelId notin rm.channels:
@@ -385,7 +389,7 @@ proc setCallbacks*(
385389
onPeriodicSync: PeriodicSyncCallback = nil,
386390
onRetrievalHint: RetrievalHintProvider = nil,
387391
onRepairReady: RepairReadyCallback = nil,
388-
): Future[void] {.async: (raises: [CancelledError]), gcsafe.} =
392+
) {.async: (raises: [CancelledError]).} =
389393
## Sets the callback functions for various events in the ReliabilityManager.
390394
try:
391395
await rm.lock.acquire()
@@ -405,7 +409,7 @@ proc setCallbacks*(
405409

406410
proc checkUnacknowledgedMessages(
407411
rm: ReliabilityManager, channelId: SdsChannelID
408-
): Future[void] {.async: (raises: [CancelledError]), gcsafe.} =
412+
) {.async: (raises: [CancelledError]).} =
409413
try:
410414
await rm.lock.acquire()
411415
try:
@@ -443,9 +447,7 @@ proc checkUnacknowledgedMessages(
443447
error "Failed to check unacknowledged messages",
444448
channelId = channelId, msg = getCurrentExceptionMsg()
445449

446-
proc periodicBufferSweep(
447-
rm: ReliabilityManager
448-
) {.async: (raises: [CancelledError]), gcsafe.} =
450+
proc periodicBufferSweep(rm: ReliabilityManager) {.async: (raises: [CancelledError]).} =
449451
while true:
450452
try:
451453
for channelId, channel in rm.channels:
@@ -458,9 +460,7 @@ proc periodicBufferSweep(
458460

459461
await sleepAsync(chronos.milliseconds(rm.config.bufferSweepInterval.inMilliseconds))
460462

461-
proc periodicSyncMessage(
462-
rm: ReliabilityManager
463-
) {.async: (raises: [CancelledError]), gcsafe.} =
463+
proc periodicSyncMessage(rm: ReliabilityManager) {.async: (raises: [CancelledError]).} =
464464
while true:
465465
try:
466466
if not rm.onPeriodicSync.isNil():
@@ -470,9 +470,7 @@ proc periodicSyncMessage(
470470
error "Error in periodic sync", msg = getCurrentExceptionMsg()
471471
await sleepAsync(chronos.seconds(rm.config.syncMessageInterval.inSeconds))
472472

473-
proc runRepairSweep*(
474-
rm: ReliabilityManager
475-
): Future[void] {.async: (raises: [CancelledError]), gcsafe.} =
473+
proc runRepairSweep*(rm: ReliabilityManager) {.async: (raises: [CancelledError]).} =
476474
## SDS-R: Runs a single pass of the repair sweep.
477475
## - Incoming: fires onRepairReady for expired T_resp entries and removes them
478476
## - Outgoing: drops entries past T_max window
@@ -518,9 +516,7 @@ proc runRepairSweep*(
518516
except CatchableError:
519517
error "Error in repair sweep", msg = getCurrentExceptionMsg()
520518

521-
proc periodicRepairSweep(
522-
rm: ReliabilityManager
523-
) {.async: (raises: [CancelledError]), gcsafe.} =
519+
proc periodicRepairSweep(rm: ReliabilityManager) {.async: (raises: [CancelledError]).} =
524520
## SDS-R: Periodically checks repair buffers for expired entries.
525521
while true:
526522
await rm.runRepairSweep()
@@ -537,7 +533,7 @@ proc startPeriodicTasks*(rm: ReliabilityManager) =
537533

538534
proc resetReliabilityManager*(
539535
rm: ReliabilityManager
540-
): Future[Result[void, ReliabilityError]] {.async: (raises: [CancelledError]), gcsafe.} =
536+
): Future[Result[void, ReliabilityError]] {.async: (raises: [CancelledError]).} =
541537
## Resets the ReliabilityManager to its initial state.
542538
try:
543539
await rm.lock.acquire()

sds/bloom.nim

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ proc initializeBloomFilter*(
8080

8181
proc `$`*(bf: BloomFilter): string =
8282
## Prints the configuration of the Bloom filter.
83-
return "Bloom filter with $1 capacity, $2 error rate, $3 hash functions, and requiring $4 bits of memory." %
83+
return
84+
"Bloom filter with $1 capacity, $2 error rate, $3 hash functions, and requiring $4 bits of memory." %
8485
[
8586
$bf.capacity,
8687
formatFloat(bf.errorRate, format = ffScientific, precision = 1),

sds/message.nim

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,5 @@ import ./types/repair_entry
77
import ./types/reliability_config
88

99
export
10-
sds_message_id,
11-
history_entry,
12-
sds_message,
13-
unacknowledged_message,
14-
incoming_message,
15-
repair_entry,
16-
reliability_config
10+
sds_message_id, history_entry, sds_message, unacknowledged_message, incoming_message,
11+
repair_entry, reliability_config

0 commit comments

Comments
 (0)