Changeset 202 in code for trunk/user.go


Ignore:
Timestamp:
Apr 1, 2020, 1:40:20 PM (5 years ago)
Author:
contact
Message:

Add "network delete" service command

And add all the infrastructure required to stop and delete networks.

References: https://todo.sr.ht/~emersion/soju/17

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/user.go

    r201 r202  
    3838type network struct {
    3939        Network
    40         user *user
    41         ring *Ring
     40        user    *user
     41        ring    *Ring
     42        stopped chan struct{}
    4243
    4344        lock    sync.Mutex
     
    5152                user:    user,
    5253                ring:    NewRing(user.srv.RingCap),
     54                stopped: make(chan struct{}),
    5355                history: make(map[string]uint64),
    5456        }
     
    5860        var lastTry time.Time
    5961        for {
     62                select {
     63                case <-net.stopped:
     64                        return
     65                default:
     66                        // This space is intentionally left blank
     67                }
     68
    6069                if dur := time.Now().Sub(lastTry); dur < retryConnectMinDelay {
    6170                        delay := retryConnectMinDelay - dur
     
    91100        defer net.lock.Unlock()
    92101        return net.conn
     102}
     103
     104func (net *network) Stop() {
     105        select {
     106        case <-net.stopped:
     107                return
     108        default:
     109                close(net.stopped)
     110        }
     111
     112        if uc := net.upstream(); uc != nil {
     113                uc.Close()
     114        }
    93115}
    94116
     
    266288        return network, nil
    267289}
     290
     291func (u *user) deleteNetwork(id int64) error {
     292        for i, net := range u.networks {
     293                if net.ID != id {
     294                        continue
     295                }
     296
     297                if err := u.srv.db.DeleteNetwork(net.ID); err != nil {
     298                        return err
     299                }
     300
     301                u.forEachDownstream(func(dc *downstreamConn) {
     302                        if dc.network != nil && dc.network == net {
     303                                dc.Close()
     304                        }
     305                })
     306
     307                net.Stop()
     308                u.networks = append(u.networks[:i], u.networks[i+1:]...)
     309                return nil
     310        }
     311
     312        panic("tried deleting a non-existing network")
     313}
Note: See TracChangeset for help on using the changeset viewer.