From 49141eb3e4c0f335b8668ab7057b753afec5c125 Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Tue, 10 May 2016 18:03:00 -0700 Subject: [PATCH] http: Remove minhttp package and use standard Golang http. (#1587) The functionality provided by minhttp will be implemented cleanly through our own APIs. Since we are not going to send SIGUSR2 and manage configuration in that manner, it doesn't make sense to use minhttp. Fixes #1586 --- pkg/minhttp/LICENSE.Facebook | 30 ---- pkg/minhttp/LICENSE.Minio | 202 -------------------------- pkg/minhttp/http_nix.go | 213 --------------------------- pkg/minhttp/http_windows.go | 213 --------------------------- pkg/minhttp/kill_windows.go | 34 ----- pkg/minhttp/listen.go | 69 --------- pkg/minhttp/net.go | 272 ----------------------------------- server-main.go | 20 ++- 8 files changed, 8 insertions(+), 1045 deletions(-) delete mode 100644 pkg/minhttp/LICENSE.Facebook delete mode 100644 pkg/minhttp/LICENSE.Minio delete mode 100644 pkg/minhttp/http_nix.go delete mode 100644 pkg/minhttp/http_windows.go delete mode 100644 pkg/minhttp/kill_windows.go delete mode 100644 pkg/minhttp/listen.go delete mode 100644 pkg/minhttp/net.go diff --git a/pkg/minhttp/LICENSE.Facebook b/pkg/minhttp/LICENSE.Facebook deleted file mode 100644 index 3aea87532..000000000 --- a/pkg/minhttp/LICENSE.Facebook +++ /dev/null @@ -1,30 +0,0 @@ -BSD License - -For grace software - -Copyright (c) 2015, Facebook, Inc. All rights reserved. - -Redistribution and use in source and binary forms, with or without modification, -are permitted provided that the following conditions are met: - - * Redistributions of source code must retain the above copyright notice, this - list of conditions and the following disclaimer. - - * Redistributions in binary form must reproduce the above copyright notice, - this list of conditions and the following disclaimer in the documentation - and/or other materials provided with the distribution. - - * Neither the name Facebook nor the names of its contributors may be used to - endorse or promote products derived from this software without specific - prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND -ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED -WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR -ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES -(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; -LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS -SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/pkg/minhttp/LICENSE.Minio b/pkg/minhttp/LICENSE.Minio deleted file mode 100644 index d64569567..000000000 --- a/pkg/minhttp/LICENSE.Minio +++ /dev/null @@ -1,202 +0,0 @@ - - Apache License - Version 2.0, January 2004 - http://www.apache.org/licenses/ - - TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION - - 1. Definitions. - - "License" shall mean the terms and conditions for use, reproduction, - and distribution as defined by Sections 1 through 9 of this document. - - "Licensor" shall mean the copyright owner or entity authorized by - the copyright owner that is granting the License. - - "Legal Entity" shall mean the union of the acting entity and all - other entities that control, are controlled by, or are under common - control with that entity. For the purposes of this definition, - "control" means (i) the power, direct or indirect, to cause the - direction or management of such entity, whether by contract or - otherwise, or (ii) ownership of fifty percent (50%) or more of the - outstanding shares, or (iii) beneficial ownership of such entity. - - "You" (or "Your") shall mean an individual or Legal Entity - exercising permissions granted by this License. - - "Source" form shall mean the preferred form for making modifications, - including but not limited to software source code, documentation - source, and configuration files. - - "Object" form shall mean any form resulting from mechanical - transformation or translation of a Source form, including but - not limited to compiled object code, generated documentation, - and conversions to other media types. - - "Work" shall mean the work of authorship, whether in Source or - Object form, made available under the License, as indicated by a - copyright notice that is included in or attached to the work - (an example is provided in the Appendix below). - - "Derivative Works" shall mean any work, whether in Source or Object - form, that is based on (or derived from) the Work and for which the - editorial revisions, annotations, elaborations, or other modifications - represent, as a whole, an original work of authorship. For the purposes - of this License, Derivative Works shall not include works that remain - separable from, or merely link (or bind by name) to the interfaces of, - the Work and Derivative Works thereof. - - "Contribution" shall mean any work of authorship, including - the original version of the Work and any modifications or additions - to that Work or Derivative Works thereof, that is intentionally - submitted to Licensor for inclusion in the Work by the copyright owner - or by an individual or Legal Entity authorized to submit on behalf of - the copyright owner. For the purposes of this definition, "submitted" - means any form of electronic, verbal, or written communication sent - to the Licensor or its representatives, including but not limited to - communication on electronic mailing lists, source code control systems, - and issue tracking systems that are managed by, or on behalf of, the - Licensor for the purpose of discussing and improving the Work, but - excluding communication that is conspicuously marked or otherwise - designated in writing by the copyright owner as "Not a Contribution." - - "Contributor" shall mean Licensor and any individual or Legal Entity - on behalf of whom a Contribution has been received by Licensor and - subsequently incorporated within the Work. - - 2. Grant of Copyright License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - copyright license to reproduce, prepare Derivative Works of, - publicly display, publicly perform, sublicense, and distribute the - Work and such Derivative Works in Source or Object form. - - 3. Grant of Patent License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - (except as stated in this section) patent license to make, have made, - use, offer to sell, sell, import, and otherwise transfer the Work, - where such license applies only to those patent claims licensable - by such Contributor that are necessarily infringed by their - Contribution(s) alone or by combination of their Contribution(s) - with the Work to which such Contribution(s) was submitted. If You - institute patent litigation against any entity (including a - cross-claim or counterclaim in a lawsuit) alleging that the Work - or a Contribution incorporated within the Work constitutes direct - or contributory patent infringement, then any patent licenses - granted to You under this License for that Work shall terminate - as of the date such litigation is filed. - - 4. Redistribution. You may reproduce and distribute copies of the - Work or Derivative Works thereof in any medium, with or without - modifications, and in Source or Object form, provided that You - meet the following conditions: - - (a) You must give any other recipients of the Work or - Derivative Works a copy of this License; and - - (b) You must cause any modified files to carry prominent notices - stating that You changed the files; and - - (c) You must retain, in the Source form of any Derivative Works - that You distribute, all copyright, patent, trademark, and - attribution notices from the Source form of the Work, - excluding those notices that do not pertain to any part of - the Derivative Works; and - - (d) If the Work includes a "NOTICE" text file as part of its - distribution, then any Derivative Works that You distribute must - include a readable copy of the attribution notices contained - within such NOTICE file, excluding those notices that do not - pertain to any part of the Derivative Works, in at least one - of the following places: within a NOTICE text file distributed - as part of the Derivative Works; within the Source form or - documentation, if provided along with the Derivative Works; or, - within a display generated by the Derivative Works, if and - wherever such third-party notices normally appear. The contents - of the NOTICE file are for informational purposes only and - do not modify the License. You may add Your own attribution - notices within Derivative Works that You distribute, alongside - or as an addendum to the NOTICE text from the Work, provided - that such additional attribution notices cannot be construed - as modifying the License. - - You may add Your own copyright statement to Your modifications and - may provide additional or different license terms and conditions - for use, reproduction, or distribution of Your modifications, or - for any such Derivative Works as a whole, provided Your use, - reproduction, and distribution of the Work otherwise complies with - the conditions stated in this License. - - 5. Submission of Contributions. Unless You explicitly state otherwise, - any Contribution intentionally submitted for inclusion in the Work - by You to the Licensor shall be under the terms and conditions of - this License, without any additional terms or conditions. - Notwithstanding the above, nothing herein shall supersede or modify - the terms of any separate license agreement you may have executed - with Licensor regarding such Contributions. - - 6. Trademarks. This License does not grant permission to use the trade - names, trademarks, service marks, or product names of the Licensor, - except as required for reasonable and customary use in describing the - origin of the Work and reproducing the content of the NOTICE file. - - 7. Disclaimer of Warranty. Unless required by applicable law or - agreed to in writing, Licensor provides the Work (and each - Contributor provides its Contributions) on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or - implied, including, without limitation, any warranties or conditions - of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A - PARTICULAR PURPOSE. You are solely responsible for determining the - appropriateness of using or redistributing the Work and assume any - risks associated with Your exercise of permissions under this License. - - 8. Limitation of Liability. In no event and under no legal theory, - whether in tort (including negligence), contract, or otherwise, - unless required by applicable law (such as deliberate and grossly - negligent acts) or agreed to in writing, shall any Contributor be - liable to You for damages, including any direct, indirect, special, - incidental, or consequential damages of any character arising as a - result of this License or out of the use or inability to use the - Work (including but not limited to damages for loss of goodwill, - work stoppage, computer failure or malfunction, or any and all - other commercial damages or losses), even if such Contributor - has been advised of the possibility of such damages. - - 9. Accepting Warranty or Additional Liability. While redistributing - the Work or Derivative Works thereof, You may choose to offer, - and charge a fee for, acceptance of support, warranty, indemnity, - or other liability obligations and/or rights consistent with this - License. However, in accepting such obligations, You may act only - on Your own behalf and on Your sole responsibility, not on behalf - of any other Contributor, and only if You agree to indemnify, - defend, and hold each Contributor harmless for any liability - incurred by, or claims asserted against, such Contributor by reason - of your accepting any such warranty or additional liability. - - END OF TERMS AND CONDITIONS - - APPENDIX: How to apply the Apache License to your work. - - To apply the Apache License to your work, attach the following - boilerplate notice, with the fields enclosed by brackets "[]" - replaced with your own identifying information. (Don't include - the brackets!) The text should be enclosed in the appropriate - comment syntax for the file format. We also recommend that a - file or class name and description of purpose be included on the - same "printed page" as the copyright notice for easier - identification within third-party archives. - - Copyright [yyyy] [name of copyright owner] - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. diff --git a/pkg/minhttp/http_nix.go b/pkg/minhttp/http_nix.go deleted file mode 100644 index 6b35e33d0..000000000 --- a/pkg/minhttp/http_nix.go +++ /dev/null @@ -1,213 +0,0 @@ -// +build linux darwin freebsd openbsd netbsd dragonfly - -/* - * Minio Cloud Storage, (C) 2015 Minio, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -// Package minhttp provides easy to use graceful restart for a set of HTTP services -// -// This package is a fork from https://github.com/facebookgo/grace -// -// Re-licensing with Apache License 2.0, with code modifications -package minhttp - -import ( - "crypto/tls" - "net" - "net/http" - "os" - "os/signal" - "sync" - "syscall" - "time" - - "github.com/facebookgo/httpdown" -) - -// An app contains one or more servers and their associated configuration. -type app struct { - servers []*http.Server - listeners []net.Listener - sds []httpdown.Server - net *minNet - errors chan error -} - -// listen initailize listeners -func (a *app) listen() error { - for _, s := range a.servers { - l, err := a.net.Listen("tcp", s.Addr) - if err != nil { - return err - } - if s.TLSConfig != nil { - l = tls.NewListener(l, s.TLSConfig) - } - a.listeners = append(a.listeners, l) - } - return nil -} - -// serve start serving all listeners -func (a *app) serve() { - h := &httpdown.HTTP{ - StopTimeout: 10 * time.Second, - KillTimeout: 1 * time.Second, - } - for i, s := range a.servers { - a.sds = append(a.sds, h.Serve(s, a.listeners[i])) - } -} - -// wait for http server to signal all requests that have been served -func (a *app) wait() { - var wg sync.WaitGroup - wg.Add(len(a.sds) * 2) // Wait & Stop - go a.trapSignal(&wg) - for _, s := range a.sds { - go func(s httpdown.Server) { - defer wg.Done() - if err := s.Wait(); err != nil { - a.errors <- err - } - }(s) - } - wg.Wait() -} - -// trapSignal wait on listed signals for pre-defined behaviors -func (a *app) trapSignal(wg *sync.WaitGroup) { - ch := make(chan os.Signal, 10) - signal.Notify(ch, syscall.SIGTERM, syscall.SIGHUP) - for { - sig := <-ch - switch sig { - case syscall.SIGTERM: - // this ensures a subsequent TERM will trigger standard go behaviour of terminating - signal.Stop(ch) - // roll through all initialized http servers and stop them - for _, s := range a.sds { - go func(s httpdown.Server) { - defer wg.Done() - if err := s.Stop(); err != nil { - a.errors <- err - } - }(s) - } - return - case syscall.SIGHUP: - // we only return here if there's an error, otherwise the new process - // will send us a TERM when it's ready to trigger the actual shutdown. - if _, err := a.net.StartProcess(); err != nil { - a.errors <- err - } - } - } -} - -// ListenAndServe will serve the given http.Servers and will monitor for signals -// allowing for graceful termination (SIGTERM) or restart (SIGUSR2/SIGHUP). -func ListenAndServe(servers ...*http.Server) error { - // get parent process id - ppid := os.Getppid() - - a := &app{ - servers: servers, - listeners: make([]net.Listener, 0, len(servers)), - sds: make([]httpdown.Server, 0, len(servers)), - net: &minNet{}, - errors: make(chan error, 1+(len(servers)*2)), - } - - // Acquire Listeners - if err := a.listen(); err != nil { - return err - } - - // Start serving. - a.serve() - - // Close the parent if we inherited and it wasn't init that started us. - if os.Getenv("LISTEN_FDS") != "" && ppid != 1 { - if err := syscall.Kill(ppid, syscall.SIGTERM); err != nil { - return err - } - } - - waitdone := make(chan struct{}) - go func() { - defer close(waitdone) - a.wait() - // communicate by sending not by closing a channel - waitdone <- struct{}{} - }() - - select { - case err := <-a.errors: - if err == nil { - panic("unexpected nil error") - } - return err - case <-waitdone: - return nil - } -} - -// ListenAndServeLimited is similar to ListenAndServe but ratelimited with connLimit value -func ListenAndServeLimited(connLimit int, servers ...*http.Server) error { - // get parent process id - ppid := os.Getppid() - - a := &app{ - servers: servers, - listeners: make([]net.Listener, 0, len(servers)), - sds: make([]httpdown.Server, 0, len(servers)), - net: &minNet{connLimit: connLimit}, - errors: make(chan error, 1+(len(servers)*2)), - } - - // Acquire Listeners - if err := a.listen(); err != nil { - return err - } - - // Start serving. - a.serve() - - // Close the parent if we inherited and it wasn't init that started us. - if os.Getenv("LISTEN_FDS") != "" && ppid != 1 { - if err := syscall.Kill(ppid, syscall.SIGTERM); err != nil { - return err - } - } - - waitdone := make(chan struct{}) - go func() { - defer close(waitdone) - a.wait() - // communicate by sending not by closing a channel - waitdone <- struct{}{} - }() - - select { - case err := <-a.errors: - if err == nil { - panic("unexpected nil error") - } - return err - case <-waitdone: - return nil - } -} diff --git a/pkg/minhttp/http_windows.go b/pkg/minhttp/http_windows.go deleted file mode 100644 index 018415e03..000000000 --- a/pkg/minhttp/http_windows.go +++ /dev/null @@ -1,213 +0,0 @@ -// +build windows - -/* - * Minio Cloud Storage, (C) 2015 Minio, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -// Package minhttp provides easy to use graceful restart for a set of HTTP services -// -// This package is a fork from https://github.com/facebookgo/grace -// -// Re-licensing with Apache License 2.0, with code modifications -package minhttp - -import ( - "crypto/tls" - "net" - "net/http" - "os" - "os/signal" - "sync" - "syscall" - "time" - - "github.com/facebookgo/httpdown" -) - -// An app contains one or more servers and their associated configuration. -type app struct { - servers []*http.Server - listeners []net.Listener - sds []httpdown.Server - net *minNet - errors chan error -} - -// listen initailize listeners -func (a *app) listen() error { - for _, s := range a.servers { - l, err := a.net.Listen("tcp", s.Addr) - if err != nil { - return err - } - if s.TLSConfig != nil { - l = tls.NewListener(l, s.TLSConfig) - } - a.listeners = append(a.listeners, l) - } - return nil -} - -// serve start serving all listeners -func (a *app) serve() { - h := &httpdown.HTTP{ - StopTimeout: 10 * time.Second, - KillTimeout: 1 * time.Second, - } - for i, s := range a.servers { - a.sds = append(a.sds, h.Serve(s, a.listeners[i])) - } -} - -// wait for http server to signal all requests that have been served -func (a *app) wait() { - var wg sync.WaitGroup - wg.Add(len(a.sds) * 2) // Wait & Stop - go a.trapSignal(&wg) - for _, s := range a.sds { - go func(s httpdown.Server) { - defer wg.Done() - if err := s.Wait(); err != nil { - a.errors <- err - } - }(s) - } - wg.Wait() -} - -// trapSignal wait on listed signals for pre-defined behaviors -func (a *app) trapSignal(wg *sync.WaitGroup) { - ch := make(chan os.Signal, 10) - signal.Notify(ch, syscall.SIGTERM, syscall.SIGHUP) - for { - sig := <-ch - switch sig { - case syscall.SIGTERM: - // this ensures a subsequent TERM will trigger standard go behaviour of terminating - signal.Stop(ch) - // roll through all initialized http servers and stop them - for _, s := range a.sds { - go func(s httpdown.Server) { - defer wg.Done() - if err := s.Stop(); err != nil { - a.errors <- err - } - }(s) - } - return - case syscall.SIGHUP: - // we only return here if there's an error, otherwise the new process - // will send us a TERM when it's ready to trigger the actual shutdown. - if _, err := a.net.StartProcess(); err != nil { - a.errors <- err - } - } - } -} - -// ListenAndServe will serve the given http.Servers and will monitor for signals -// allowing for graceful termination (SIGTERM) or restart (SIGUSR2/SIGHUP). -func ListenAndServe(servers ...*http.Server) error { - // get parent process id - ppid := os.Getppid() - - a := &app{ - servers: servers, - listeners: make([]net.Listener, 0, len(servers)), - sds: make([]httpdown.Server, 0, len(servers)), - net: &minNet{}, - errors: make(chan error, 1+(len(servers)*2)), - } - - // Acquire Listeners - if err := a.listen(); err != nil { - return err - } - - // Start serving. - a.serve() - - // Close the parent if we inherited and it wasn't init that started us. - if os.Getenv("LISTEN_FDS") != "" && ppid != 1 { - if err := terminateProcess(ppid, 1); err != nil { - return err - } - } - - waitdone := make(chan struct{}) - go func() { - defer close(waitdone) - a.wait() - // communicate by sending not by closing a channel - waitdone <- struct{}{} - }() - - select { - case err := <-a.errors: - if err == nil { - panic("unexpected nil error") - } - return err - case <-waitdone: - return nil - } -} - -// ListenAndServeLimited is similar to ListenAndServe but ratelimited with connLimit value -func ListenAndServeLimited(connLimit int, servers ...*http.Server) error { - // get parent process id - ppid := os.Getppid() - - a := &app{ - servers: servers, - listeners: make([]net.Listener, 0, len(servers)), - sds: make([]httpdown.Server, 0, len(servers)), - net: &minNet{connLimit: connLimit}, - errors: make(chan error, 1+(len(servers)*2)), - } - - // Acquire Listeners - if err := a.listen(); err != nil { - return err - } - - // Start serving. - a.serve() - - // Close the parent if we inherited and it wasn't init that started us. - if os.Getenv("LISTEN_FDS") != "" && ppid != 1 { - if err := terminateProcess(ppid, 1); err != nil { - return err - } - } - - waitdone := make(chan struct{}) - go func() { - defer close(waitdone) - a.wait() - // communicate by sending not by closing a channel - waitdone <- struct{}{} - }() - - select { - case err := <-a.errors: - if err == nil { - panic("unexpected nil error") - } - return err - case <-waitdone: - return nil - } -} diff --git a/pkg/minhttp/kill_windows.go b/pkg/minhttp/kill_windows.go deleted file mode 100644 index 38c26cf1c..000000000 --- a/pkg/minhttp/kill_windows.go +++ /dev/null @@ -1,34 +0,0 @@ -// +build windows - -/* - * Minio Cloud Storage, (C) 2015 Minio, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package minhttp - -import ( - "os" - "syscall" -) - -func terminateProcess(pid, exitcode int) error { - h, e := syscall.OpenProcess(syscall.PROCESS_TERMINATE, false, uint32(pid)) - if e != nil { - return os.NewSyscallError("OpenProcess", e) - } - defer syscall.CloseHandle(h) - e = syscall.TerminateProcess(h, uint32(exitcode)) - return os.NewSyscallError("TerminateProcess", e) -} diff --git a/pkg/minhttp/listen.go b/pkg/minhttp/listen.go deleted file mode 100644 index 59eb30298..000000000 --- a/pkg/minhttp/listen.go +++ /dev/null @@ -1,69 +0,0 @@ -/* - * Minio Cloud Storage, (C) 2015 Minio, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package minhttp - -import ( - "net" - "os" - "sync" -) - -// rateLimitedListener returns a Listener that accepts at most n simultaneous -// connections from the provided Listener. -func rateLimitedListener(l net.Listener, nconn int) net.Listener { - if nconn > 0 { - return &rateLimitListener{l, make(chan struct{}, nconn)} - } - return l -} - -type rateLimitListener struct { - net.Listener - sem chan struct{} -} - -func (l *rateLimitListener) accept() { l.sem <- struct{}{} } -func (l *rateLimitListener) release() { <-l.sem } - -// File - necessary to expose underlying socket fd -func (l *rateLimitListener) File() (f *os.File, err error) { - return l.Listener.(fileListener).File() -} - -// Accept - accept method for accepting new connections -func (l *rateLimitListener) Accept() (net.Conn, error) { - l.accept() - - c, err := l.Listener.Accept() - if err != nil { - l.release() - return nil, err - } - return &rateLimitListenerConn{Conn: c, release: l.release}, nil -} - -type rateLimitListenerConn struct { - net.Conn - releaseOnce sync.Once - release func() -} - -func (l *rateLimitListenerConn) Close() error { - err := l.Conn.Close() - l.releaseOnce.Do(l.release) - return err -} diff --git a/pkg/minhttp/net.go b/pkg/minhttp/net.go deleted file mode 100644 index b317fd504..000000000 --- a/pkg/minhttp/net.go +++ /dev/null @@ -1,272 +0,0 @@ -/* - * Minio Cloud Storage, (C) 2015 Minio, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package minhttp - -import ( - "fmt" - "net" - "os" - "os/exec" - "strconv" - "strings" - "sync" -) - -// This package is a fork https://github.com/facebookgo/grace -// -// Re-licensing with Apache License 2.0, with code modifications - -// This package provides a family of Listen functions that either open a -// fresh connection or provide an inherited connection from when the process -// was started. This behaves like their counterparts in the net package, but -// transparently provide support for graceful restarts without dropping -// connections. This is provided in a systemd socket activation compatible form -// to allow using socket activation. -// - -const ( - // Used to indicate a graceful restart in the new process. - envCountKey = "LISTEN_FDS" // similar to systemd SDS_LISTEN_FDS - envCountKeyPrefix = envCountKey + "=" -) - -// In order to keep the working directory the same as when we started we record -// it at startup. -var originalWD, _ = os.Getwd() - -// minNet provides the family of Listen functions and maintains the associated -// state. Typically you will have only once instance of minNet per application. -type minNet struct { - inheritedListeners []net.Listener - activeListeners []net.Listener - connLimit int - mutex sync.Mutex - inheritOnce sync.Once -} - -// minAddr simple wrapper over net.Addr interface to implement IsEqual() -type minAddr struct { - net.Addr -} - -// fileListener simple interface to extract file pointers from different types of net.Listener's -type fileListener interface { - File() (*os.File, error) -} - -// getInheritedListeners - look for LISTEN_FDS in environment variables and populate listeners accordingly -func (n *minNet) getInheritedListeners() error { - var retErr error - n.inheritOnce.Do(func() { - n.mutex.Lock() - defer n.mutex.Unlock() - countStr := os.Getenv(envCountKey) - if countStr == "" { - return - } - count, err := strconv.Atoi(countStr) - if err != nil { - retErr = fmt.Errorf("found invalid count value: %s=%s", envCountKey, countStr) - return - } - - fdStart := 3 - for i := fdStart; i < fdStart+count; i++ { - file := os.NewFile(uintptr(i), "listener") - l, err := net.FileListener(file) - if err != nil { - file.Close() - retErr = err - return - } - if err := file.Close(); err != nil { - retErr = err - return - } - n.inheritedListeners = append(n.inheritedListeners, l) - } - }) - if retErr != nil { - return retErr - } - return nil -} - -// Listen announces on the local network address laddr. The network net must be -// a stream-oriented network: "tcp", "tcp4", "tcp6", "unix" or "unixpacket". It -// returns an inherited net.Listener for the matching network and address, or -// creates a new one using net.Listen() -func (n *minNet) Listen(nett, laddr string) (net.Listener, error) { - switch nett { - default: - return nil, net.UnknownNetworkError(nett) - case "tcp", "tcp4", "tcp6": - addr, err := net.ResolveTCPAddr(nett, laddr) - if err != nil { - return nil, err - } - return n.ListenTCP(nett, addr) - case "unix", "unixpacket": - addr, err := net.ResolveUnixAddr(nett, laddr) - if err != nil { - return nil, err - } - return n.ListenUnix(nett, addr) - } -} - -// ListenTCP announces on the local network address laddr. The network net must -// be: "tcp", "tcp4" or "tcp6". It returns an inherited net.Listener for the -// matching network and address, or creates a new one using net.ListenTCP. -func (n *minNet) ListenTCP(nett string, laddr *net.TCPAddr) (net.Listener, error) { - if err := n.getInheritedListeners(); err != nil { - return nil, err - } - - n.mutex.Lock() - defer n.mutex.Unlock() - - // look for an inherited listener - for i, l := range n.inheritedListeners { - if l == nil { // we nil used inherited listeners - continue - } - equal := minAddr{l.Addr()}.IsEqual(laddr) - if equal { - n.inheritedListeners[i] = nil - n.activeListeners = append(n.activeListeners, l) - return l.(*net.TCPListener), nil - } - } - - // make a fresh listener - l, err := net.ListenTCP(nett, laddr) - if err != nil { - return nil, err - } - n.activeListeners = append(n.activeListeners, rateLimitedListener(l, n.connLimit)) - return l, nil -} - -// ListenUnix announces on the local network address laddr. The network net -// must be a: "unix" or "unixpacket". It returns an inherited net.Listener for -// the matching network and address, or creates a new one using net.ListenUnix. -func (n *minNet) ListenUnix(nett string, laddr *net.UnixAddr) (net.Listener, error) { - if err := n.getInheritedListeners(); err != nil { - return nil, err - } - - n.mutex.Lock() - defer n.mutex.Unlock() - - // look for an inherited listener - for i, l := range n.inheritedListeners { - if l == nil { // we nil used inherited listeners - continue - } - equal := minAddr{l.Addr()}.IsEqual(laddr) - if equal { - n.inheritedListeners[i] = nil - n.activeListeners = append(n.activeListeners, l) - return l.(*net.UnixListener), nil - } - } - - // make a fresh listener - l, err := net.ListenUnix(nett, laddr) - if err != nil { - return nil, err - } - n.activeListeners = append(n.activeListeners, rateLimitedListener(l, n.connLimit)) - return l, nil -} - -// activeListeners returns a snapshot copy of the active listeners. -func (n *minNet) getActiveListeners() []net.Listener { - n.mutex.Lock() - defer n.mutex.Unlock() - ls := make([]net.Listener, len(n.activeListeners)) - copy(ls, n.activeListeners) - return ls -} - -// IsEqual is synonymous with IP.IsEqual() method, here IsEqual matches net.Addr instead of net.IP -func (n1 minAddr) IsEqual(n2 net.Addr) bool { - if n1.Network() != n2.Network() { - return false - } - a1h, a1p, _ := net.SplitHostPort(n1.String()) - a2h, a2p, _ := net.SplitHostPort(n2.String()) - // Special cases since Addr() from net.Listener will - // add frivolous [::] ipv6 for no ":[PORT]" style addresses - if a1h == "::" && a2h == "" && a1p == a2p { - return true - } - if a2h == "::" && a1h == "" && a1p == a2p { - return true - } - if net.ParseIP(a1h).Equal(net.ParseIP(a2h)) && a1p == a2p { - return true - } - return false -} - -// StartProcess starts a new process passing it the active listeners. It -// doesn't fork, but starts a new process using the same environment and -// arguments as when it was originally started. This allows for a newly -// deployed binary to be started. It returns the pid of the newly started -// process when successful. -func (n *minNet) StartProcess() (int, error) { - listeners := n.getActiveListeners() - // Extract the fds from the listeners. - files := make([]*os.File, len(listeners)) - for i, l := range listeners { - var err error - files[i], err = l.(fileListener).File() - if err != nil { - return 0, err - } - defer files[i].Close() - } - - // Use the original binary location. This works with symlinks such that if - // the file it points to has been changed we will use the updated symlink. - argv0, err := exec.LookPath(os.Args[0]) - if err != nil { - return 0, err - } - - // Pass on the environment and replace the old count key with the new one. - var env []string - for _, v := range os.Environ() { - if !strings.HasPrefix(v, envCountKeyPrefix) { - env = append(env, v) - } - } - env = append(env, fmt.Sprintf("%s%d", envCountKeyPrefix, len(listeners))) - - allFiles := append([]*os.File{os.Stdin, os.Stdout, os.Stderr}, files...) - process, err := os.StartProcess(argv0, os.Args, &os.ProcAttr{ - Dir: originalWD, - Env: env, - Files: allFiles, - }) - if err != nil { - return 0, err - } - return process.Pid, nil -} diff --git a/server-main.go b/server-main.go index a9617dcaa..9911671d0 100644 --- a/server-main.go +++ b/server-main.go @@ -17,7 +17,6 @@ package main import ( - "crypto/tls" "errors" "fmt" "net" @@ -30,7 +29,6 @@ import ( "github.com/minio/cli" "github.com/minio/mc/pkg/console" - "github.com/minio/minio/pkg/minhttp" ) var serverCmd = cli.Command{ @@ -86,15 +84,6 @@ func configureServer(srvCmdConfig serverCmdConfig) *http.Server { MaxHeaderBytes: 1 << 20, } - // Configure TLS if certs are available. - if isSSL() { - var err error - apiServer.TLSConfig = &tls.Config{} - apiServer.TLSConfig.Certificates = make([]tls.Certificate, 1) - apiServer.TLSConfig.Certificates[0], err = tls.LoadX509KeyPair(mustGetCertFile(), mustGetKeyFile()) - fatalIf(err, "Unable to load certificates.", nil) - } - // Returns configured HTTP server. return apiServer } @@ -319,6 +308,13 @@ func serverMain(c *cli.Context) { } // Start server. - err := minhttp.ListenAndServe(apiServer) + var err error + // Configure TLS if certs are available. + if isSSL() { + err = apiServer.ListenAndServeTLS(mustGetCertFile(), mustGetKeyFile()) + } else { + // Fallback to http. + err = apiServer.ListenAndServe() + } errorIf(err, "Failed to start the minio server.", nil) }