Repository: ossrs/srs
File: trunk/src/app/srs_app_edge.cpp (C++)

Code before:
{
    // Make sure the coroutine is stopped before disposing the sdk,
    // for sdk is used by coroutine.
    trd->stop();
    srs_freep(sdk);

    queue->clear();
}

// when error, edge ingester sleep for a while and retry.
#define SRS_EDGE_FORWARDER_CIMS (3 * SRS_UTIME_SECONDS)

srs_error_t SrsEdgeForwarder::cycle()
{
    srs_error_t err = srs_success;
    
    while (true) {
        // We always check status first.
        // @see https://github.com/ossrs/srs/issues/1634#issuecomment-597571561
        if ((err = trd->pull()) != srs_success) {
            return srs_error_wrap(err, "thread pull");
        }

        // If coroutine stopping, we should always set the quit error code.
        err = do_cycle();
        if (send_error_code == 0) {
            send_error_code = srs_error_code(err);
        }

        if (err != srs_success) {
            return srs_error_wrap(err, "do cycle");
        }

        srs_usleep(SRS_EDGE_FORWARDER_CIMS);
    }
    
    return err;
}

#define SYS_MAX_EDGE_SEND_MSGS 128

srs_error_t SrsEdgeForwarder::do_cycle()
{
    srs_error_t err = srs_success;
    
    sdk->set_recv_timeout(SRS_CONSTS_RTMP_PULSE);

    SrsUniquePtr<SrsPithyPrint> pprint(SrsPithyPrint::create_edge());
    SrsMessageArray msgs(SYS_MAX_EDGE_SEND_MSGS);
    
    while (true) {

Review comment (suggestion):
```suggestion
      if ((err = do_cycle()) != srs_success) {
            if (send_error_code == ERROR_SUCCESS) {
                // If coroutine stopping, we should always set the quit error code.
                send_error_code = srs_error_code(err);
            }
            return srs_error_wrap(err, "do cycle");
      }
```

suggest another way.

About `send_error_code`, I believe there is a dead loop:
https://github.com/ossrs/srs/blob/8c034e5a13abf209c7cddfb32ae1d32ce6627f09/trunk/src/app/srs_app_edge.cpp#L864-L884

there are no place to set `send_error_code` = ERROR_SUCCESS in the loop, only set the value when `sdk->recv_message(&msg)`.


Code after:
{
    // Make sure the coroutine is stopped before disposing the sdk,
    // for sdk is used by coroutine.
    trd->stop();
    srs_freep(sdk);

    queue->clear();
}

// when error, edge ingester sleep for a while and retry.
#define SRS_EDGE_FORWARDER_CIMS (3 * SRS_UTIME_SECONDS)

srs_error_t SrsEdgeForwarder::cycle()
{
    srs_error_t err = srs_success;
    
    while (true) {
        // We always check status first.
        // @see https://github.com/ossrs/srs/issues/1634#issuecomment-597571561
        if ((err = trd->pull()) != srs_success) {
            return srs_error_wrap(err, "thread pull");
        }

        if ((err = do_cycle()) != srs_success) {
            // If cycle stopping, we should always set the quit error code.
            if (send_error_code == 0) {
                send_error_code = srs_error_code(err);
            }

            return srs_error_wrap(err, "do cycle");
        }

        srs_usleep(SRS_EDGE_FORWARDER_CIMS);
    }
    
    return err;
}

#define SYS_MAX_EDGE_SEND_MSGS 128

srs_error_t SrsEdgeForwarder::do_cycle()
{
    srs_error_t err = srs_success;
    
    sdk->set_recv_timeout(SRS_CONSTS_RTMP_PULSE);

    SrsUniquePtr<SrsPithyPrint> pprint(SrsPithyPrint::create_edge());
    SrsMessageArray msgs(SYS_MAX_EDGE_SEND_MSGS);
    
    while (true) {
        if ((err = trd->pull()) != srs_success) {