Home
Insert
Update snippet 'playing with dns + async composition'
Title
Description
just some snippets ive been using to play around with async + DNS, nothing production, updated: ipsOfDomain and domainOfIp now both return string array
Source code
let maybe f x = try Some (f x) with _ -> None module ns = open System open System.Net let child f = Async.StartChild(f, 25200) let maybeAsync f x = async { return (maybe f x) } let childAsync f = async { try let! x = f in return! x with _ -> return None } let tryAsyncDnsWithChild f = maybeAsync f >> child >> childAsync (* kinda redundant (parallelism wise) because we do 1 host at a time, look into fixing later*) let findHosts f hosts = hosts |> Array.map (tryAsyncDnsWithChild f) |> Async.Parallel |> Async.RunSynchronously |> Array.filter ((<>) None) |> Array.map Option.get let getFirstEntry a = maybe (Array.get a) 0 |> Option.get let getHostEntry (h : string) = Dns.GetHostEntry h let getHostName h = (getHostEntry h).HostName let getHostAddr h = (getHostEntry h).AddressList |> getFirstEntry |> string let g f h = [| findHosts f h |> getFirstEntry |] let ipsOfDomain = g getHostAddr let domainOfIp = g getHostName
Tags
dns
async
dns
async
Author
Link
Reference NuGet packages
If your snippet has external dependencies, enter the names of NuGet packages to reference, separated by a comma (
#r
directives are not required).
Update