 
 : check-node ( mdb node --  )
    [ <mdb-connection> &dispose ] dip
-   [ open-connection ] keep swap
-   [ get-ismaster eval-ismaster-result ] with-connection ;
+   [ [ open-connection ] [ 3drop f ] recover ] keep swap
+   [ [ get-ismaster eval-ismaster-result ] with-connection ] [ drop ] if* ;
 
 : nodelist>table ( seq -- assoc )
    [ [ master?>> ] keep 2array ] map >hashtable ;

        mdb node1 remote>>
        [ [ check-node ] keep ]
        [ drop f ] if*  :> node2

        node1 [ acc push ] when*
        node2 [ acc push ] when*
        mdb acc nodelist>table >>nodes drop 
    ] with-destructors ; 
              
: mdb-open ( mdb -- mdb-connection )
    clone [ <mdb-connection> ] keep
    master-node open-connection
    [ authenticate-connection ] keep ; 

: mdb-close ( mdb-connection -- )
     [ dispose f ] change-handle drop ;

M: mdb-connection dispose
     mdb-close ;

USING: accessors arrays assocs byte-vectors checksums
checksums.md5 constructors continuations destructors fry
hashtables io.encodings.binary io.encodings.string
io.encodings.utf8 io.sockets io.streams.duplex kernel locals
math math.parser mongodb.cmd mongodb.msg mongodb.operations
namespaces sequences splitting ;
IN: mongodb.connection

: md5-checksum ( string -- digest )
