Changeset 267 in code for trunk/user.go


Ignore:
Timestamp:
Apr 23, 2020, 2:36:20 PM (5 years ago)
Author:
contact
Message:

Add network.channels, remove DB.GetChannel

Store the list of configured channels in the network data structure.
This removes the need for a database lookup and will be useful for
detached channels.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/user.go

    r253 r267  
    5757
    5858        conn           *upstreamConn
     59        channels       map[string]*Channel
    5960        history        map[string]*networkHistory // indexed by entity
    6061        offlineClients map[string]struct{}        // indexed by client name
     
    6263}
    6364
    64 func newNetwork(user *user, record *Network) *network {
     65func newNetwork(user *user, record *Network, channels []Channel) *network {
     66        m := make(map[string]*Channel, len(channels))
     67        for _, ch := range channels {
     68                m[ch.Name] = &ch
     69        }
     70
    6571        return &network{
    6672                Network:        *record,
    6773                user:           user,
    6874                stopped:        make(chan struct{}),
     75                channels:       m,
    6976                history:        make(map[string]*networkHistory),
    7077                offlineClients: make(map[string]struct{}),
     
    141148
    142149func (net *network) createUpdateChannel(ch *Channel) error {
    143         if dbCh, err := net.user.srv.db.GetChannel(net.ID, ch.Name); err == nil {
    144                 ch.ID = dbCh.ID
    145         } else if err != ErrNoSuchChannel {
     150        if current, ok := net.channels[ch.Name]; ok {
     151                ch.ID = current.ID // update channel if it already exists
     152        }
     153        if err := net.user.srv.db.StoreChannel(net.ID, ch); err != nil {
    146154                return err
    147155        }
    148         return net.user.srv.db.StoreChannel(net.ID, ch)
     156        net.channels[ch.Name] = ch
     157        return nil
    149158}
    150159
    151160func (net *network) deleteChannel(name string) error {
    152         return net.user.srv.db.DeleteChannel(net.ID, name)
     161        if err := net.user.srv.db.DeleteChannel(net.ID, name); err != nil {
     162                return err
     163        }
     164        delete(net.channels, name)
     165        return nil
    153166}
    154167
     
    222235
    223236        for _, record := range networks {
    224                 network := newNetwork(u, &record)
     237                channels, err := u.srv.db.ListChannels(record.ID)
     238                if err != nil {
     239                        u.srv.Logger.Printf("failed to list channels for user %q, network %q: %v", u.Username, record.GetName(), err)
     240                }
     241
     242                network := newNetwork(u, &record, channels)
    225243                u.networks = append(u.networks, network)
    226244
     
    354372        }
    355373
    356         network := newNetwork(u, net)
     374        network := newNetwork(u, net, nil)
    357375        err := u.srv.db.StoreNetwork(u.Username, &network.Network)
    358376        if err != nil {
Note: See TracChangeset for help on using the changeset viewer.