Changeset 267 in code for trunk


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.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/db.go

    r263 r267  
    4848        Key  string
    4949}
    50 
    51 var ErrNoSuchChannel = fmt.Errorf("soju: no such channel")
    5250
    5351const schema = `
     
    372370}
    373371
    374 func (db *DB) GetChannel(networkID int64, name string) (*Channel, error) {
    375         db.lock.RLock()
    376         defer db.lock.RUnlock()
    377 
    378         ch := &Channel{Name: name}
    379 
    380         var key *string
    381         row := db.db.QueryRow("SELECT id, key FROM Channel WHERE network = ? AND name = ?", networkID, name)
    382         if err := row.Scan(&ch.ID, &key); err == sql.ErrNoRows {
    383                 return nil, ErrNoSuchChannel
    384         } else if err != nil {
    385                 return nil, err
    386         }
    387         ch.Key = fromStringPtr(key)
    388         return ch, nil
    389 }
    390 
    391372func (db *DB) StoreChannel(networkID int64, ch *Channel) error {
    392373        db.lock.Lock()
  • trunk/upstream.go

    r263 r267  
    422422                uc.logger.Printf("connection registered")
    423423
    424                 channels, err := uc.srv.db.ListChannels(uc.network.ID)
    425                 if err != nil {
    426                         uc.logger.Printf("failed to list channels from database: %v", err)
    427                         break
    428                 }
    429 
    430                 for _, ch := range channels {
     424                for _, ch := range uc.network.channels {
    431425                        params := []string{ch.Name}
    432426                        if ch.Key != "" {
  • 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.