func (b *BridgeDB) Start(converter Converter) error { if err := b.mqttClient.Connect(); err != nil { log.Error(err) return err } defer b.Stop() var st time.Time var counterLimit int64 = 100000 sem := make(chan int, b.setting.Concurrency) for { err := b.mqttClient.Subscribe( b.setting.Topic, mqtt.QOS_ZERO, func(msg mqtt.Message) error { atomic.AddInt64(&b.counter, 1) if b.counter == 1 { log.Infof( "Benchmark mqtt-subscriber pg-bridge start...", ) st = time.Now() } log.Infof("counter=%v", b.counter) sem <- 1 b.wg.Add(1) go func(topic string, msgData []byte) { log.Info(string(msgData)) r := converter(topic) json.Unmarshal(msgData, r) err := b.dbm.Save(r) if err != nil { log.Error(err) } b.wg.Done() <-sem }(msg.Topic(), msg.Payload()) return nil }, ) if err != nil { log.Error(err) break } if b.counter >= counterLimit { break } time.Sleep(time.Millisecond) } b.wg.Wait() log.Infof("counter=%v", b.counter) log.Infof( "Benchmark mqtt-subscriber pg-bridge end.[elapsedtime=%f sec]", time.Now().Sub(st).Seconds(), ) return nil }
func (c *MqttClient) ForceDisconnect() { if c.internalClient == nil || !c.internalClient.IsConnected() { log.Warn("Already closed mqtt-server") return } c.internalClient.ForceDisconnect() log.Infof("ForceDisconnect mqtt-server: %v", c.setting.URL) }
func Create(setting *Setting) *MqttClient { uri, err := url.Parse(setting.URL) if err != nil { log.Errorf("Faild to parse mqtt-url: %v", err) return nil } opts := mqtt.NewClientOptions() // tcp://iot.eclipse.org:1883 - connect to iot.eclipse.org on port 1883 using plain TCP // ws://iot.eclipse.org:1883 - connect to iot.eclipse.org on port 1883 using WebSockets // tls://iot.eclipse.org:8883 - connect to iot.eclipse.org on port 8883 using TLS (ssl:// and tcps:// are synonyms for tls://) //opts.AddBroker(fmt.Sprintf("tcps://%s", uri.Host)) //opts.AddBroker(fmt.Sprintf("tls://%s", uri.Host)) opts.AddBroker(fmt.Sprintf("tls://%s", uri.Host)) if uri.User != nil { username := uri.User.Username() password, _ := uri.User.Password() opts.SetUsername(username) opts.SetPassword(password) } opts.SetClientID(setting.ClientId) opts.AutoReconnect = true opts.ProtocolVersion = 4 opts.CleanSession = true if false { certPool, err := GetCertPool("/vagrant/goapp/src/github.com/fltmtoda/golibs-examples/mosquitto.org.crt") if err != nil { panic(err) } opts.SetTLSConfig( &tls.Config{ RootCAs: certPool, }, ) } opts.OnConnectionLost = func(c *mqtt.Client, err error) { if err != nil { log.Errorf("Failed to disconnect mqtt-server: %v [%v]", opts.Servers, err) return } else { log.Infof("Disonnect mqtt-server: %v", opts.Servers) } } return &MqttClient{ internalClient: mqtt.NewClient(opts), setting: setting, } }
func (db *gormDB) AutoMigrate(values ...interface{}) error { if len(values) > 0 { for _, v := range values { if err := db.Dbm.AutoMigrate(v).Error; err != nil { return err } log.Infof( "Create table: %s", db.Dbm.NewScope(v).TableName(), ) } } return nil }
func (c *MqttClient) Connect() error { if c.internalClient == nil { return fmt.Errorf("Already closed mqtt-server") } if c.internalClient.IsConnected() { log.Warn("Already connected mqtt-server") return nil } token := c.internalClient.Connect() if token.Wait() && token.Error() != nil { return fmt.Errorf("Failed to connect mqtt-server: %v", token.Error()) } time.Sleep(2 * time.Second) log.Infof("Connect mqtt-server: %v", c.setting.URL) return nil }
func (db *gormDB) DropTableWithCascade(values ...interface{}) error { if len(values) > 0 { for _, v := range values { tableName := db.Dbm.NewScope(v).TableName() sql := fmt.Sprintf( "DROP TABLE IF EXISTS %s CASCADE", tableName, ) if err := db.Dbm.Exec(sql).Error; err != nil { return err } log.Infof( "Drop table: %s", tableName, ) } } return nil }