Close closes the pipeline and returns the connection to normal mode.
()
| 2902 | |
| 2903 | // Close closes the pipeline and returns the connection to normal mode. |
| 2904 | func (p *Pipeline) Close() error { |
| 2905 | if p.closed { |
| 2906 | return p.err |
| 2907 | } |
| 2908 | |
| 2909 | p.closed = true |
| 2910 | |
| 2911 | if p.state.PendingSync() { |
| 2912 | p.conn.asyncClose() |
| 2913 | p.err = errors.New("pipeline has unsynced requests") |
| 2914 | p.conn.contextWatcher.Unwatch() |
| 2915 | p.conn.unlock() |
| 2916 | |
| 2917 | return p.err |
| 2918 | } |
| 2919 | |
| 2920 | for p.state.ExpectedReadyForQuery() > 0 { |
| 2921 | results, err := p.getResults() |
| 2922 | if err != nil { |
| 2923 | p.err = err |
| 2924 | var pgErr *PgError |
| 2925 | if !errors.As(err, &pgErr) { |
| 2926 | p.conn.asyncClose() |
| 2927 | break |
| 2928 | } |
| 2929 | } else if results == nil { |
| 2930 | // getResults returns (nil, nil) when the request queue is exhausted but |
| 2931 | // ExpectedReadyForQuery is still > 0. This can happen when FATAL errors consume |
| 2932 | // queued request slots without the server ever sending ReadyForQuery. |
| 2933 | p.conn.asyncClose() |
| 2934 | if p.err == nil { |
| 2935 | p.err = errors.New("pipeline: no more results but expected ReadyForQuery") |
| 2936 | } |
| 2937 | break |
| 2938 | } |
| 2939 | } |
| 2940 | |
| 2941 | p.conn.contextWatcher.Unwatch() |
| 2942 | p.conn.unlock() |
| 2943 | |
| 2944 | return p.err |
| 2945 | } |
| 2946 | |
| 2947 | // DeadlineContextWatcherHandler handles canceled contexts by setting a deadline on a net.Conn. |
| 2948 | type DeadlineContextWatcherHandler struct { |
nothing calls this directly
no test coverage detected