在订阅完成之前,AWS MQTT连接丢失

I'm having an intermittent issue with go trying to connect to AWS MQTT. Sometimes it works fine, other times not so much. When I connect I get this error: Connection lost before Subscribe completed

I'm really not sure why it does this, at first everything seemed to work fine but now it throws this error more often than not. Here's the code that I'm using to connect:

type ServerConnection struct {
    Cert     tls.Certificate
    Host     string
    Port     int
    Path     string
    Topic    string
    Qos      int
    Messages chan MQTT.Message
    Control  chan os.Signal
 }

func (server *ServerConnection) Start() error {
    cid := uuid.New().String()

    connOpts := &MQTT.ClientOptions{
            ClientID:             cid,
            CleanSession:         true,
            AutoReconnect:        true,
            MaxReconnectInterval: 1 * time.Second,
            KeepAlive:            30000,
            TLSConfig:            tls.Config{Certificates: []tls.Certificate{server.Cert}},
    }
    connOpts.SetConnectTimeout(30 * time.Second)
    connOpts.SetDefaultPublishHandler(func(client MQTT.Client, msg MQTT.Message) {
            server.Messages <- msg
    })
    brokerURL := fmt.Sprintf("tcps://%s:%d%s", server.Host, server.Port, server.Path)
    connOpts.AddBroker(brokerURL)
    mqttClient := MQTT.NewClient(connOpts)
    if token := mqttClient.Connect(); token.Wait() && token.Error() != nil {
            return token.Error()
    }
    // Subscribe
    go func() {
            log.Printf("subscribing")
            if token := mqttClient.Subscribe(server.Topic, byte(server.Qos), nil); token.Wait() && token.Error() != nil {
                    fmt.Println(token.Error())
                    os.Exit(1)
            }
            log.Printf("subscribed")

    }()
    server.ProcessMessages()
    log.Println("[MQTT] Connected")
    quit := make(chan struct{})
    signal.Notify(server.Control, os.Interrupt)
    go func() {
            <-server.Control
            mqttClient.Disconnect(250)
            fmt.Println("[MQTT] Disconnect")
            quit <- struct{}{}
    }()
    <-quit

    return nil

   }

Basically you need to put the subscribe methid call inside the onConnectHandler, this will make sure that the subscribe is being called upon successful connection (and reconnect) The handler is defined in the client options.