removes scheduler and some cleanup

This commit is contained in:
Diego 2024-05-16 19:20:30 +02:00
parent ac994a8f15
commit 3a659ffddb
No known key found for this signature in database
GPG Key ID: C9DAC9BF68D1F806

View File

@ -9,14 +9,11 @@
{.push raises: [].} {.push raises: [].}
import std/[deques, sequtils] import std/sequtils
import stew/[byteutils, results, endians2] import stew/[byteutils, results, endians2]
import chronos, chronos/transports/[osnet, ipnet], metrics import chronos, chronos/transports/[osnet, ipnet], chronicles
import ../[multiaddress, multicodec] import ../[multiaddress, multicodec]
import ../switch import ../switch
import ../wire
import ../utils/heartbeat
import ../crypto/crypto
logScope: logScope:
topics = "libp2p wildcardresolverservice" topics = "libp2p wildcardresolverservice"
@ -24,7 +21,7 @@ logScope:
type type
WildcardAddressResolverService* = ref object of Service WildcardAddressResolverService* = ref object of Service
## Service used to resolve wildcard addresses of the type "0.0.0.0" for IPv4 or "::" for IPv6. ## Service used to resolve wildcard addresses of the type "0.0.0.0" for IPv4 or "::" for IPv6.
## When used with a `Switch`, this service will be automatically started and stopped ## When used with a `Switch`, this service will be automatically set up and stopped
## when the switch starts and stops. This is facilitated by adding the service to the switch's ## when the switch starts and stops. This is facilitated by adding the service to the switch's
## list of services using the `.withServices(@[svc])` method in the `SwitchBuilder`. ## list of services using the `.withServices(@[svc])` method in the `SwitchBuilder`.
networkInterfaceProvider: NetworkInterfaceProvider networkInterfaceProvider: NetworkInterfaceProvider
@ -34,10 +31,6 @@ type
## to the respective list of interface addresses. As an example, if the listen address is 0.0.0.0:4001 ## to the respective list of interface addresses. As an example, if the listen address is 0.0.0.0:4001
## and the machine has 2 interfaces with IPs 172.217.11.174 and 64.233.177.113, the address mapper will ## and the machine has 2 interfaces with IPs 172.217.11.174 and 64.233.177.113, the address mapper will
## expand the wildcard address to 172.217.11.174:4001 and 64.233.177.113:4001. ## expand the wildcard address to 172.217.11.174:4001 and 64.233.177.113:4001.
scheduleHandle: Future[void]
## Represents the task that is scheduled to run the service.
scheduleInterval: Opt[Duration]
## The interval at which the service should run.
NetworkInterfaceProvider* = ref object of RootObj NetworkInterfaceProvider* = ref object of RootObj
@ -72,36 +65,18 @@ proc new*(
scheduleInterval: Opt[Duration] = Opt.none(Duration), scheduleInterval: Opt[Duration] = Opt.none(Duration),
networkInterfaceProvider: NetworkInterfaceProvider = new(NetworkInterfaceProvider), networkInterfaceProvider: NetworkInterfaceProvider = new(NetworkInterfaceProvider),
): T = ): T =
## This procedure initializes a new `WildcardAddressResolverService` with the provided ## This procedure initializes a new `WildcardAddressResolverService` with the provided network interface provider.
## schedule interval and network interface provider.
## ##
## Parameters: ## Parameters:
## - `T`: The type descriptor for `WildcardAddressResolverService`. ## - `T`: The type descriptor for `WildcardAddressResolverService`.
## - `scheduleInterval`: An optional interval at which the service should run. Defaults to none.
## - `networkInterfaceProvider`: A provider that offers access to network interfaces. Defaults to a new instance of `NetworkInterfaceProvider`. ## - `networkInterfaceProvider`: A provider that offers access to network interfaces. Defaults to a new instance of `NetworkInterfaceProvider`.
## ##
## Returns: ## Returns:
## - A new instance of `WildcardAddressResolverService`. ## - A new instance of `WildcardAddressResolverService`.
return T( return T(
scheduleInterval: scheduleInterval,
networkInterfaceProvider: networkInterfaceProvider, networkInterfaceProvider: networkInterfaceProvider,
) )
proc schedule(
service: WildcardAddressResolverService, switch: Switch, interval: Duration
) {.async.} =
## Schedules the WildcardAddressResolverService to run at regular intervals.
##
## Sets up a schedule for the WildcardAddressResolverService to execute periodically based
## on the specified interval. It continuously runs the service on the given switch at the defined time intervals.
##
## Parameters:
## - `service`: The instance of the WildcardAddressResolverService that will be scheduled.
## - `switch`: The Switch object that the service will operate on.
## - `interval`: The Duration specifying how often the service should run.
heartbeat "Scheduling WildcardAddressResolverService run", interval:
await service.run(switch)
proc getProtocolArgument*(ma: MultiAddress, codec: MultiCodec): MaResult[seq[byte]] = proc getProtocolArgument*(ma: MultiAddress, codec: MultiCodec): MaResult[seq[byte]] =
var buffer: seq[byte] var buffer: seq[byte]
for item in ma: for item in ma:
@ -201,8 +176,7 @@ method setup*(
): Future[bool] {.async.} = ): Future[bool] {.async.} =
## Sets up the `WildcardAddressResolverService`. ## Sets up the `WildcardAddressResolverService`.
## ##
## This method adds the address mapper to the peer's list of address mappers and schedules the seervice to run ## This method adds the address mapper to the peer's list of address mappers.
## at the specified interval.
## ##
## Parameters: ## Parameters:
## - `self`: The instance of `WildcardAddressResolverService` being set up. ## - `self`: The instance of `WildcardAddressResolverService` being set up.
@ -221,8 +195,6 @@ method setup*(
debug "Setting up WildcardAddressResolverService" debug "Setting up WildcardAddressResolverService"
let hasBeenSetup = await procCall Service(self).setup(switch) let hasBeenSetup = await procCall Service(self).setup(switch)
if hasBeenSetup: if hasBeenSetup:
self.scheduleInterval.withValue(interval):
self.scheduleHandle = schedule(self, switch, interval)
switch.peerInfo.addressMappers.add(self.addressMapper) switch.peerInfo.addressMappers.add(self.addressMapper)
return hasBeenSetup return hasBeenSetup
@ -241,7 +213,7 @@ method stop*(
## Stops the WildcardAddressResolverService. ## Stops the WildcardAddressResolverService.
## ##
## Handles the shutdown process of the WildcardAddressResolverService for a given switch. ## Handles the shutdown process of the WildcardAddressResolverService for a given switch.
## It cancels the scheduled task, if any, and removes the address mapper from the switch's list of address mappers. ## It removes the address mapper from the switch's list of address mappers.
## It then updates the peer information for the provided switch. Any wildcard address wont be resolved anymore. ## It then updates the peer information for the provided switch. Any wildcard address wont be resolved anymore.
## ##
## Parameters: ## Parameters:
@ -253,9 +225,6 @@ method stop*(
debug "Stopping WildcardAddressResolverService" debug "Stopping WildcardAddressResolverService"
let hasBeenStopped = await procCall Service(self).stop(switch) let hasBeenStopped = await procCall Service(self).stop(switch)
if hasBeenStopped: if hasBeenStopped:
if not isNil(self.scheduleHandle):
self.scheduleHandle.cancel()
self.scheduleHandle = nil
switch.peerInfo.addressMappers.keepItIf(it != self.addressMapper) switch.peerInfo.addressMappers.keepItIf(it != self.addressMapper)
await switch.peerInfo.update() await switch.peerInfo.update()
return hasBeenStopped return hasBeenStopped