| 3582 | } |
| 3583 | |
| 3584 | int cdb2_close(cdb2_hndl_tp *hndl) |
| 3585 | { |
| 3586 | cdb2_event *curre; |
| 3587 | void *callbackrc; |
| 3588 | int rc = 0; |
| 3589 | |
| 3590 | if (log_calls) |
| 3591 | fprintf(stderr, "%p> cdb2_close(%p)\n", (void *)pthread_self(), hndl); |
| 3592 | |
| 3593 | if (!hndl) |
| 3594 | return 0; |
| 3595 | |
| 3596 | if (hndl->fdb_hndl) { |
| 3597 | cdb2_close(hndl->fdb_hndl); |
| 3598 | hndl->fdb_hndl = NULL; |
| 3599 | } |
| 3600 | |
| 3601 | if (hndl->ack) |
| 3602 | ack(hndl); |
| 3603 | |
| 3604 | if (hndl->auto_consume_timeout_ms > 0 && hndl->sb && !hndl->in_trans && hndl->firstresponse && |
| 3605 | (!hndl->lastresponse || (hndl->lastresponse->response_type != RESPONSE_TYPE__LAST_ROW))) { |
| 3606 | int nrec = 0; |
| 3607 | sbuf2settimeout(hndl->sb, hndl->auto_consume_timeout_ms, hndl->auto_consume_timeout_ms); |
| 3608 | struct timeval tv; |
| 3609 | gettimeofday(&tv, NULL); |
| 3610 | uint64_t starttimems = ((uint64_t)tv.tv_sec) * 1000 + tv.tv_usec / 1000; |
| 3611 | while (cdb2_next_record_int(hndl, 0) == CDB2_OK) { |
| 3612 | nrec++; |
| 3613 | gettimeofday(&tv, NULL); |
| 3614 | uint64_t curr = ((uint64_t)tv.tv_sec) * 1000 + tv.tv_usec / 1000; |
| 3615 | /* auto consume for up to CDB2_AUTO_CONSUME_TIMEOUT_MS */ |
| 3616 | if (curr - starttimems >= hndl->auto_consume_timeout_ms) |
| 3617 | break; |
| 3618 | } |
| 3619 | if (hndl->debug_trace) { |
| 3620 | gettimeofday(&tv, NULL); |
| 3621 | uint64_t curr = ((uint64_t)tv.tv_sec) * 1000 + tv.tv_usec / 1000; |
| 3622 | fprintf(stderr, "%s: auto consume %d records took %" PRIu64 " ms\n", |
| 3623 | __func__, nrec, curr - starttimems); |
| 3624 | } |
| 3625 | } |
| 3626 | |
| 3627 | if (hndl->sb) |
| 3628 | newsql_disconnect(hndl, hndl->sb, __LINE__); |
| 3629 | |
| 3630 | if (hndl->firstresponse) { |
| 3631 | cdb2__sqlresponse__free_unpacked(hndl->firstresponse, NULL); |
| 3632 | free((void *)hndl->first_buf); |
| 3633 | hndl->first_buf = NULL; |
| 3634 | } |
| 3635 | |
| 3636 | if (hndl->lastresponse) { |
| 3637 | if (hndl->protobuf_used_sysmalloc) |
| 3638 | cdb2__sqlresponse__free_unpacked(hndl->lastresponse, |
| 3639 | &hndl->allocator); |
| 3640 | hndl->protobuf_offset = 0; |
| 3641 | free((void *)hndl->last_buf); |