Skip to content

Commit

Permalink
fix self-monitoring (istio#2140)
Browse files Browse the repository at this point in the history
Automatic merge from submit-queue.

fix self-monitoring

The current self-monitoring is broken, nothing can be fetched
for /metrics nor /version. This is because ListenAndServe() *creates* a new
net.Listener and then serve, while current monitoring.go also creates
a net.Listener instance beforehand, so ListenAndServe() always fails
with "address already in-use" error.

When a net.Listener is given, use "Serve" instead. Also "Close"
can close the listener, the `monitor` does not have to keep the listener
at all. That's all taken care of already.

**What this PR does / why we need it**:

**Which issue this PR fixes** *(optional, in `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close that issue when PR gets merged)*: fixes #

**Special notes for your reviewer**:

**Release note**:

```release-note
none
```
  • Loading branch information
jmuk authored and istio-merge-robot committed Dec 13, 2017
1 parent c679bce commit c81dfbf
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions mixer/pkg/server/monitoring.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
)

type monitor struct {
listener net.Listener
monitoringServer *http.Server
shutdown chan struct{}
}
Expand All @@ -42,8 +41,9 @@ func startMonitor(port uint16) (*monitor, error) {
}

// get the network stuff setup
var listener net.Listener
var err error
if m.listener, err = net.Listen("tcp", fmt.Sprintf(":%d", port)); err != nil {
if listener, err = net.Listen("tcp", fmt.Sprintf(":%d", port)); err != nil {
return nil, fmt.Errorf("unable to listen on socket: %v", err)
}

Expand All @@ -60,19 +60,18 @@ func startMonitor(port uint16) (*monitor, error) {
})

m.monitoringServer = &http.Server{
Addr: fmt.Sprintf(":%d", port),
Handler: mux,
}

go func() {
m.shutdown <- struct{}{}
_ = m.monitoringServer.ListenAndServe()
_ = m.monitoringServer.Serve(listener)
m.shutdown <- struct{}{}
}()

// This is here to work around (mostly) a race condition in the ListenAndServe
// This is here to work around (mostly) a race condition in the Serve
// function. If the Close method is called before or during the execution of
// ListenAndServe, the call may be ignored and ListenAndServe never returns.
// Serve, the call may be ignored and Serve never returns.
<-m.shutdown

return m, nil
Expand All @@ -81,6 +80,5 @@ func startMonitor(port uint16) (*monitor, error) {
func (m *monitor) Close() error {
_ = m.monitoringServer.Close()
<-m.shutdown
_ = m.listener.Close()
return nil
}

0 comments on commit c81dfbf

Please sign in to comment.